【问题标题】:Maven Circular property definitionMaven 循环属性定义
【发布时间】:2017-02-04 06:27:11
【问题描述】:

刚接触 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:我在这里误会了什么?

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

【问题讨论】:

  • 为什么需要一个可以读写的属性文件?
  • 对于这个项目,我有一个由最终产品安装读取的属性文件。它被加载到多个服务器上以进行各种测试和配置,但安装程序文件是相同的。当属性发生变化时,我们必须去修改所有服务器上的属性文件以反映新的配置。我将其作为所有服务器都可以获取和使用的单点更改。

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


【解决方案1】:

您遇到了 properties-maven-plugin 的错误,今年早些时候已经是 reported,但修复尚未包含在新版本中:

无根据的“循环属性定义”#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 sn-p:

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

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

mvn clean package -DA=1 -DB2

我们将在生成的.jar 文件中包含build.properties 文件的内容:

C = 1 + 2 + 2

【讨论】:

  • 感谢您的回答。我修改了我的项目,一切都很好!
  • @david 如果对您有帮助,请考虑accepting it。谢谢。
【解决方案2】:

当变量具有如下值时,也会出现此循环属性定义

A = ${A}
B = ${B}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多