Maven循环属性定义

3

我刚开始接触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:我在这里有什么误解?

我目前正在仔细查看插件的文档,以查看是否错过了一些显而易见的东西。


为什么你需要一个属性文件来读取或写入它? - khmarbaise
针对该项目,我有一个属性文件,最终产品安装时会读取它。它被加载到多个服务器上进行各种测试和配置,但安装程序文件是相同的。当属性发生变化时,我们必须修改所有服务器上的属性文件,以反映新的配置。我要将其作为单一的更改点,供所有服务器获取和使用。 - david
1个回答

3
您遇到了 properties-maven-plugin 的一个缺陷,这个问题已经在今年早些时候被 报告,但修复还未成为新版本的一部分:

无法解释的“循环属性定义”#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类,目前最新的插件快照版本尚未修复。

您可以按照以下步骤再次测试:将以下内容添加到您的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过滤,就不存在同样的问题。以下是一个POM代码片段示例:
<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
@david 如果这对你有帮助,请考虑接受它。谢谢。 - A_Di-Matteo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接