Maven 2 属性maven插件:加载属性文件时出错

Maven 2 属性maven插件:加载属性文件时出错,maven-2,maven-plugin,Maven 2,Maven Plugin,我想将pom.xml中的所有属性提取到属性文件中。这些是常见的属性,如依赖版本、插件版本和目录。 我正在使用properties maven插件,但它并没有像我希望的那样工作 my pom.xml的基本部分: <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>

我想将pom.xml中的所有属性提取到属性文件中。这些是常见的属性,如依赖版本、插件版本和目录。 我正在使用properties maven插件,但它并没有像我希望的那样工作

my 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>

org.codehaus.mojo
. 
它已被关闭为“非bug”,原因是:

这是设计的。项目定义 必须是独立的,否则 如果被引用,则不再完整 来自其他地方,作为 可传递的deps


您的
配置
元素在
执行
中定义,因此仅适用于此
执行

因此,可以调用
mvn initialize
(或
initialize
之后的一个阶段)来使用当前执行的
绑定的
配置

或者使用全局
配置

  <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>
但是,在这个插件的特定情况下(您希望属性在构建过程中可用),这没有多大意义,因此这就留给您第一个解决方案


更新:我做了一个测试,实际上,使用了以下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>
奇怪的是,依赖项的下载发生在
properties:readprojectproperties
之后。我不确定,但这听起来像个bug,你应该打开。

EDIT2


有关使用Ant任务的解决方法,请参阅,这使此用例成为可能

尝试使用validate phase而不是maven 3.x()的initialize。

我遇到了您的问题,但我尝试在此处添加此资源,效果很好

 <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

src/config//您的配置文件夹
真的
org.codehaus.mojo
属性maven插件
1.0-α-2
初始化
读取项目属性
src/config/config.properties//您的配置文件
希望你能像上面那样解决这个问题

非常感谢,很好的提示!错误消失了。但是我想做的仍然不起作用:pom.xml中的属性不会被我的prop文件中的属性替换。例如,当我调用“mvn properties:read project properties test”时,我会遇到诸如“Missing:------------1)junit:junit:jar:${junit.version}”之类的错误,我是否必须运行任何其他特定目标,以便maven在“runtime”时正确插入属性?谢谢您的提示。刚刚提出了一个问题。看起来,我是唯一一个需要这种“功能”的人
<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 [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. ...
 <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