Maven 循环属性定义

标签 maven maven-3 pom.xml circular-reference

maven 新手,但克服了令人沮丧的学习曲线....

我有一个简单的 pom 文件,它读取一个属性文件并写出一个配置文件。我将 'properties-maven-plugin' 与

一起使用
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
    <execution>
    <id>read-install-properties</id>
        <phase>generate-resources</phase>
        <goals>
           <goal>read-project-properties</goal>
        </goals>
    </phase>
    </execution>
</executions>

.... .... 当我在我的属性文件中添加这样一行时:

C = ${A} + ${B}

然后运行 ​​mvn mvn install -f pom2.xml -DA=1 -DB=2

我在我的输出文件中看到了这个:

C = 1 + 2

如我所料。

当我把行改成

C = ${A} + ${B} + ${B}

我希望看到这个:

C = 1 + 2 + 2

但是我得到了一个

Circular property definition: C=${A} +  ${B} + ${B} -> A=1 -> B=2 -> B=2 -> [Help 1]

**问题*8:我在这里误解了什么?

我目前正在更仔细地查看插件的文档,看看我是否遗漏了一些明显的东西。

最佳答案

你遇到了properties-maven-plugin的bug,已经reported了今年早些时候,但修复还不是新版本的一部分:

Unwarranted "Circular property definition" #27

确实,有一个包含以下内容的简单 pom.xml:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
            <execution>
                <id>read-install-properties</id>
                <phase>validate</phase>
                <goals>
                    <goal>read-project-properties</goal>
                </goals>
                <configuration>
                    <files>
                        <file>src/main/resources/build.properties</file>
                    </files>
                </configuration>
            </execution>
            <execution>
                <id>write-install-properties</id>
                <phase>validate</phase>
                <goals>
                    <goal>write-project-properties</goal>
                </goals>
                <configuration>
                    <outputFile>target/build.properties</outputFile>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

src/main/resources/build.properties 具有简单的内容:

C = ${A} + ${B} + ${B}

并调用 Maven 作为:

mvn clean validate -DA=1 -DB=2

会导致

[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties (read-install-properties) on pr
oject sample: Circular property definition: C=${A} + ${B} + ${B} -> A=1 -> B=2 -> B=2 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

相关组件是 CircularDefinitionPreventer类,尚未通过插件的最新 SNAPSHOT 版本修复。

您可以按如下方式再次测试它:将以下部分添加到您的 pom 文件中:

<pluginRepositories>
    <pluginRepository>
      <id>apache.snapshots</id>
      <name>Maven Plugin Snapshots</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
      <releases>
        <enabled>false</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
</pluginRepositories>

并将插件版本更改为1.0.1-SNAPSHOT。这将允许您使用插件的最新 SNAPSHOT 版本,但尚未提供修复(在撰写本文时)。


通过 Maven filtering 却没有出现同样的问题.具有以下示例 POM 片段:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

使用与上面相同的文件并执行

mvn clean package -DA=1 -DB2

我们会将 build.properties 文件作为生成的 .jar 文件的一部分,其内容为:

C = 1 + 2 + 2

关于Maven 循环属性定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39710828/

相关文章:

java - 如何修复 Eclipse 项目的结构?

java - 如何将非jar文件添加到Maven项目的类路径中?

maven - IntelliJ新项目-maven原型(prototype)列表为空

java - Maven 多模块 : error assembling WAR: webxml attribute is required

java - Maven 无法识别我的用户配置文件并在其他位置查找 .m2

maven - 要求在子模块中定义 Maven 属性

maven - 将 Spotbugs 集成到 Maven Pom - 不会生成报告?

java - Ant - 如何在 javac 任务的类路径中使用文件集 id?

java - 无法构建 CloudHopper (SMPP)

android - Maven编译错误: package R does not exist