【问题标题】:nest properties in properties (in properties) (ant to maven migration)在属性中嵌套属性(在属性中)(ant to maven migration)
【发布时间】:2017-09-10 01:28:06
【问题描述】:

我们使用 ant 构建(一个巨大的)项目,并且(由于遗留原因)我们将属性嵌套在属性文件中的深度。

(例如在 build.xml 中)

<ant dir="${object.build.dir}" target="deploy" />

(在一些属性文件中:)

object.build.dir=${base.dir}

(在其他一些属性文件中(有时甚至相同)):

base.dir=${env}/some/dir

然后,因为 ant 不再那么新了,我们现在要迁移到 maven,这将是一场噩梦......

问题是:您如何读取将属性嵌套在属性中的结果,有时可以达到 5 层...

(作为测试)

过滤器/top_level.properties:

env=test

filters/test/mid_level.properties:

specific=spec

filters/top_level.properties 也可以读取(对于另一个环境):

env=prod

还有过滤器/prod/mid_level.properties:

specific=spock

问题归结为:

如何读取/使用在另一个属性文件中定义路径的属性文件中定义的“特定”属性?) (而且到处都是这种嵌套?)

此外,这不涉及“资源”(这些也涉及但不是主要问题,资源是应用程序使用的属性,我谈论的属性是仅在构建时用于指定的属性构建是如何发生的)

我找到了答案,但如果有更好的答案,我很乐意选择它作为最佳答案...

S.

【问题讨论】:

  • 您的属性看起来像是针对不同环境的设置,在 Ant 和 Maven 中都不是使用属性的好主意......也许你可以看看这里:github.com/khmarbaise/multienv-maven-plugin...Furthermore I假设您每次都为每个属性构建,这意味着 dev、prod 等。这在 Maven 中不起作用......只需构建一次并在一次运行中产生所有需要的输出......
  • 嘿,谢谢,我一定会看看的,S。

标签: java maven ant properties


【解决方案1】:

在 maven 中,您有 properties:read-project-properties 插件。

如果你像这样使用它,嵌套属性将不会插入到下降的属性中: (maven 仍然会抱怨它没有找到“filters/${env}/mid_level.properties”属性文件

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <id>execution1</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>filters/top_level.properties</file>
                            <file>filters/${env}/mid_level.properties</file>
                        </files>
                    </configuration>
                </execution>
          </plugin>
    </plugins>
</build>

但是如果你像这样分开执行(+ 回显“特定”的值:)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>test</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <id>execution1</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>filters/top_level.properties</file>
                            </files>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution2</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>filters/${env}/mid_level.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.soebes.maven.plugins</groupId>
                <artifactId>maven-echo-plugin</artifactId>
                <version>0.1</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>echo</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <echos>
                        <echo>Animal: ${specific}</echo>
                    </echos>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

使用 env=prod 输出:

[INFO] --- maven-echo-plugin:0.1:echo (default) @ test ---
[INFO] Animal: spock
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.663 s
[INFO] Finished at: 2017-04-13T21:32:30+02:00
[INFO] Final Memory: 11M/164M
[INFO] ------------------------------------------------------------------------

问题解决了:)

您“只”需要执行与嵌套深度一样多的属性 + 重构属性以将它们放在分层中,但显然这个问题“很容易”解决...

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多