从 Maven POM 文件读取属性文件

2022-09-01 06:44:04

我有一些配置的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文件中读回此密钥?


答案 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进一步隔离到属性文件中,则需要使用属性Maven插件,并在Maven生命周期的初始化阶段运行其目标。此处转载了插件页面中的示例: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>

答案 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作为构建工具的原因......


推荐