[英]Reading properties file from Maven POM file
我有一些配置的 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 文件中读回这个密钥?
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
标签中指定properties
,而是从命令行传递值:
mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal
现在,如果您不想从命令行指定它们,并且如果您需要进一步将这些属性与项目 POM 隔离到一个属性文件中,那么您将需要使用Properties Maven 插件,并运行它的read-project-properties
目标在Maven 生命周期的初始化阶段。 插件页面中的示例在此处复制:
<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>
完整的工作示例可在: 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 作为构建工具......
实际上不可能使用已接受答案中的说明从文件加载属性,因为这些属性在 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.