Maven:属性未替换为路径(以阅读第二个其他属性文件)



我已经尝试了类似努力的一些答案,但与我要实现的目标没有什么相适应的...

这就是我要做的:

我正在尝试基于(其他(属性文件构建(过滤(属性文件。

我们有一个包含一般属性的属性文件。该文件(目前为default.properties(被读取并像魅力一样解析。

然后,我们还希望根据环境具有特定的属性。(我知道,不是很好的做法,但这是最好的解决方案,因为我们在不同的机器上具有独立的构建,并且在各地生成所有属性都不方便(

那是我们陷入困境的地方...

指定包含属性的路径(在这种情况下为Targetenvironment(并不会填充其值。

  • 我已经尝试切换阶段(生成 - 资源/初始化/流程资源(;
  • 使用以下方式使用CLI属性:mvn clean install -DtargetEnvironment=NT
  • 使用称为:
  • 的配置文件

(在POM内(

<profiles>
    <profile>
        <id>nt</id>
        <properties>
            <targetEnvironment>NT</targetEnvironment>
        </properties>
    </profile>
</profiles>

这是当前POM的(仅显示相关部分(:

<project>
(some maven code)
    <build>
    (other maven code)
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <id>execution1</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>src/env_properties/env/${targetEnvironment}/specific.properties</file>
                            </files>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution2</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>src/env_properties/env/default/default.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>initialize</phase>
                        <goals>
                            <goal>echo</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <echos>
                        <echo>Animal: src/env_properties/env/${targetEnvironment}/specific.properties</echo>
                        <!-- prints: src/env_properties/env/NT/specific.properties -->
                    </echos>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/env_properties/base</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

奇怪的是

如果我用字面的" nt"替换$ {targetenvironment},则该机制有效,只有替代品...

我还尝试更改插件版本,但有些版本甚至使其更糟...

有什么想法?

谢谢,

s。

编辑:另一个奇怪的事情,如果我没有-DtargetEnvironment=NT参数构建,我会构建失败,因为:

[INFO]
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (execution1) @ batch3 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.644 s
[INFO] Finished at: 2017-04-21T15:55:11+02:00
[INFO] Final Memory: 41M/459M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties (execution1) on project batch3: Properties could not be loaded from File: C:pathtosrcenv_propertiesenv${targetEnvironment}specific.properties -> [Help 1]
[ERROR]

我不知道如何解决原始问题,但我发现了做我想做的事情:

如果您有一个属性文件(具有在其他属性文件中替换值的值;例如:(

(top level.properties(

ejb.server.url=not yet defined
ejb.server.user=not yet defined
ejb.server.pwd=not yet defined

和一个特定的属性文件(基于在构建时间提供的属性,致电mvn clean install -DtargetEnvironment=NT(

(src env_properties env nt exter.properties(

garrafra.doesntbestaat=anotherTest

然后可以像这样读取第一个属性:

    <build>
        (some maven code)
        <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>src/env_properties/env/default/top_level.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        (other maven code)
    </build>

此文件中的属性可用于检索更特定属性文件的(例如路径(。

...和(当然是同一标签中(可以读取(并应用于过滤(特定属性文件:

    <build>
        (some maven code)
        <filters>
            <filter>src/env_properties/env/${targetEnvironment}/specific.properties</filter>
        </filters>
        (other maven code)
    </build>

在属性中提供此此事,因此 - 马文 - plugin由于某种原因不起作用...

作为特定(环境依赖性环境(的一般(最高级(属性都被填写:

(vanilla.properties(之前(:(

java.naming.provider.url=${ejb.server.url}
java.naming.security.principal=${ejb.server.user}
java.naming.security.credentials=${ejb.server.pwd}
blie=${garrafra.doesntbestaat}

(之后(

java.naming.provider.url=not yet defined
java.naming.security.principal=not yet defined
java.naming.security.credentials=not yet defined
blie=anotherTest

最新更新