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

时间:2010-04-18 23:04:29

标签: 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>

现在当我运行“mvn properties: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>.

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

修改

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

  

这是设计的。项目定义   必须是独立的,否则它   如果被提及,则不再完整   从其他地方作为一部分   传递性的代表。

4 个答案:

答案 0 :(得分: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>

然后致电

mvn properties:read-project-properties

但是在这个插件的特定情况下这没有多大意义(你希望在构建期间可以使用这些属性)所以这将为你留下第一个解决方案。


更新:我在我身边做了一次测试,实际上,使用以下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>

运行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之后。我不确定但这听起来像个错误,你应该打开an issue

答案 1 :(得分:3)

尝试使用验证阶段而不是初始化为maven 3.x(link)。

答案 2 :(得分:0)

<强> EDIT2

有关使用Ant Tasks的变通方法,请参阅here,这使得此用例成为可能

答案 3 :(得分:-1)

我遇到了你的问题,但我试着在这里添加这些资源,效果很好。

 <build>
    <resources>
        <resource>
            <directory>src/config</directory> //your config folder
            <filtering>true</filtering>
        </resource>
    </resources>

    <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>src/config/config.properties</file> //your config file
                  </files>
                </configuration>
              </execution>
            </executions>
            </plugin>
    </build>
Hope you resolve this as above
相关问题