【问题标题】:Reading properties file from Maven POM file从 Maven POM 文件中读取属性文件
【发布时间】:2011-10-31 22:59:30
【问题描述】:

我有一些配置的 Maven POM 文件,在插件部分,我有一些配置如下的 maven tomcat 插件:

<configuration>
   <url>http://localhost:8080/manager/html</url>
   <server>tomcat</server>
</configuration>

我想使用该键将 url 设置导出到某个属性文件,例如 tomcat.properties:

url=http://localhost:8080/manager/html

我怎样才能在我的 POM 文件中读回这个密钥?

【问题讨论】:

    标签: java tomcat maven-2 pom.xml properties-file


    【解决方案1】:

    Maven 允许您在项目的 POM 中定义属性。您可以使用类似于以下内容的 POM 文件执行此操作:

    <project>
        ...
        <properties>
            <server.url>http://localhost:8080/manager/html</server.url>
        </properties>
        ...
        <build>
            <plugins>
                <plugin>
                ...
                    <configuration>
                        <url>${server.url}</url>
                        <server>tomcat</server>
                    </configuration>
                ...
                </plugin>
            </plugins>
        </build>
    </project>
    

    您可以避免在properties 标记内指定属性,并将值从命令行传递为:

    mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal
    

    现在,如果您不想从命令行指定它们,并且需要进一步将这些属性与项目 POM 隔离到一个属性文件中,那么您需要使用 Properties Maven plugin,并且在initialize phase of the Maven lifecycle 中运行它的read-project-properties 目标。插件页面中的示例在此处复制:

    <project>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
               <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
              <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>
    </project>
    

    【讨论】:

    • 两个链接都失效了。
    • 是否可以在运行时将属性值注入或提取到 java 类中。我正在尝试各种方法来获取属性文件中键的值。
    【解决方案2】:

    完整的工作示例位于:http://hg.defun.work/exp/file/tip/maven/properties

    pom.xml的重要部分:

    <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>
          </execution>
        </executions>
        <configuration>
          <files>
            <file>dev.properties</file>
          </files>
        </configuration>
      </plugin>
    
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <echo>project.build.sourceEncoding is "${project.build.sourceEncoding}"</echo>
                <echo>foo is "${foo}"</echo>
                <echo>with-spaces is "${with-spaces}"</echo>
                <echo>existent.property is "${existent.property}"</echo>
                <echo>nonexistent.property is "${nonexistent.property}"</echo>
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    

    您可以看到 properties-maven-plugin 仍处于 alpha 阶段,这就是为什么我讨厌将 Maven 作为构建工具...

    【讨论】:

      【解决方案3】:

      实际上不可能使用已接受答案中的说明从文件中加载属性,因为这些属性在 pom 文件中不可用,尽管它们可用于过滤。 最小反例:

      在 pom.xml 中:

      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
              <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
              <execution>
                <!-- Apart from this test, the phase must be initialize -->
                <phase>validate</phase>
                <goals>
                  <goal>read-project-properties</goal>
                </goals>
                <configuration>
                  <files>
                    <file>dev.properties</file>
                  </files>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
              <execution>
                <phase>validate</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <echo>Displaying value of properties</echo>
                    <echo>[foo] ${foo}</echo>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      

      dev.properties:

      foo=bar
      

      然后运行命令mvn validate 产生输出:

       [echo] Displaying value of properties
       [echo] [foo] bar
      

      【讨论】:

      • 那是因为 validateinitialize 之前执行。
      • @gavenkoa 你能解释一下我是如何在那个字符串中失败的吗?
      • 查看我的答案并阅读有关 Maven 生命周期顺序 maven.apache.org/guides/introduction/… 如果您将目标中的 phase 更改为 initialize b> 你的例子开始工作了!!
      • @gavenkoa 已修复。为什么您在报告问题时没有解决它?
      • @JeanValjean 没时间。我在雇主带薪时间发表了此评论。
      猜你喜欢
      • 2013-01-21
      • 2020-05-10
      • 2020-11-02
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2013-01-20
      • 2012-01-22
      • 2015-07-04
      相关资源
      最近更新 更多