【问题标题】:properties-maven-plugin: Error loading properties-fileproperties-maven-plugin:加载属性文件时出错
【发布时间】:2011-02-09 11:57:34
【问题描述】:

我想将 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 提交了一个问题。 它已被关闭为“不是错误”,原因是:

这是设计使然。项目定义 必须是独立的,否则它 如果被引用则不再完整 作为其他地方的一部分 传递的部门。

【问题讨论】:

    标签: maven-2 maven-plugin


    【解决方案1】:

    您的configuration 元素在execution 中定义,因此仅适用于execution

    因此,要么调用mvn initialize(或initialize 之后的阶段)来使用当前execution 绑定的configuration

    或者使用全局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 测试 [INFO] 正在扫描项目... [信息] --------------------------------------------- ------------------------- [信息] 构建 SO - Q2664362 - maven-properties-plugin 演示 [INFO] 任务段:[测试] [信息] --------------------------------------------- ------------------------- [信息] [属性:读取项目属性 {执行:默认}] [信息] [资源:资源{执行:默认资源}] [INFO] 使用 'UTF-8' 编码复制过滤的资源。 [INFO] 跳过不存在的资源目录 /home/pascal/Projects/stackoverflow/Q2664362/src/main/resources 下载:http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.pom [信息] 在存储库中心 (http://repo1.maven.org/maven2) 中找不到资源“junit:junit:pom:${junit.version}” [信息] [编译器:编译{执行:默认编译}] [INFO] 无需编译 - 所有类都是最新的 [信息] [资源:testResources {执行:默认-testResources}] [INFO] 使用 'UTF-8' 编码复制过滤的资源。 [INFO] 跳过不存在的资源目录 /home/pascal/Projects/stackoverflow/Q2664362/src/test/resources 下载:http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.jar [信息] 在存储库中心 (http://repo1.maven.org/maven2) 中找不到资源“junit:junit:jar:${junit.version}” [信息] --------------------------------------------- ------------------------- [错误] 构建错误 [信息] --------------------------------------------- ------------------------- [INFO] 无法解析工件。 ...

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

    【讨论】:

    • 非常感谢,好提示!错误消失了。但是我想做的仍然不起作用:我的 pom.xml 中的属性不会被我的 prop-file 中的属性替换。例如。当我调用“mvn properties:read-project-properties test”时,我收到类似“Missing: ---------- 1) junit:junit:jar:${junit.version}”之类的错误我必须运行任何其他特定目标,以便 maven 在“运行时”正确插入属性?
    • 感谢您的提示。刚刚提交了一个问题。看来,我是唯一需要这种“功能”的人;)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      EDIT2

      请参阅 here 了解使用 Ant 任务的解决方法,这使得该用例成为可能

      【讨论】:

        【解决方案4】:

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

         <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
        

        【讨论】:

          猜你喜欢
          • 2014-06-22
          • 1970-01-01
          • 1970-01-01
          • 2015-12-29
          • 2015-09-12
          • 1970-01-01
          • 2014-04-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多