properties-maven-plugin:加载属性文件时出错

Ing*_*her 9 maven-2 maven-plugin

我想从我的pom.xml中提取属性文件中的所有属性.这些是常见的属性,如依赖版本,插件版本和目录.我正在使用properties-maven-plugin,但它不能像我想要的那样工作.

我的pom.xml的基本部分:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0-alpha-1</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>read-project-properties</goal>
      </goals>
      <configuration>
       <files>
          <file>${basedir}/pom.properties</file>
        </files>
      </configuration>
    </execution>
  </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

现在,当我运行"mvn属性:read-project-properties"时,我收到以下错误:

[INFO] One or more required plugin parameters are invalid/missing for 'properties:read-project-properties'

[0] Inside the definition for plugin 'properties-maven-plugin' specify the following:

<configuration>
  ...
  <files>VALUE</files>
</configuration>.
Run Code Online (Sandbox Code Playgroud)

pom.properties文件与pom.xml位于同一目录中.我该怎么做才能让properties-maven-plugin读取我的属性文件?

编辑

我在http://jira.codehaus.org/browse/MOJO-1523提交了一个问题.它已被关闭为"不是一个bug",原因是:

这是设计的.项目定义必须是自包含的,否则如果从别处作为传递deps的一部分引用它则不再完整.

Pas*_*ent 6

您的configuration元素在一个内部定义execution,因此仅适用于此execution.

因此要么调用mvn initialize(或后面的一个阶段initialize)来使用configuration当前execution绑定.

或使用全局configuration:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <configuration>
     <files>
        <file>etc/config/dev.properties</file>
      </files>
    </configuration>
    ...
  </plugin>
Run Code Online (Sandbox Code Playgroud)

然后打电话

mvn properties:read-project-properties
Run Code Online (Sandbox Code Playgroud)

但是在这个插件的特定情况下这没有多大意义(你希望在构建过程中可以使用这些属性),这样就可以得到第一个解决方案.


更新:我在我身边进行了测试,实际上,我使用了以下POM:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q2664362</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>etc/config/dev.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

运行mvn test不起作用:maven将尝试下载junit:jar:${junit.version}(即它不使用属性的值),这显然会失败.

$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building SO - Q2664362 - maven-properties-plugin demo
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [properties:read-project-properties {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/main/resources
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.pom
[INFO] Unable to find resource 'junit:junit:pom:${junit.version}' in repository central (http://repo1.maven.org/maven2)
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/test/resources
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.jar
[INFO] Unable to find resource 'junit:junit:jar:${junit.version}' in repository central (http://repo1.maven.org/maven2)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
...

奇怪的是,依赖项的下载发生在之后properties:read-project-properties.我不确定,但这听起来像一个错误,你应该打开一个问题.


Ing*_*her 0

编辑2

请参阅此处了解使用 Ant Tasks 的解决方法,这使得此用例成为可能