从Maven POM文件读取属性文件 [英] Reading properties file from Maven POM file
问题描述
我有一些配置的Maven POM文件,在部分插件中,我有一些配置的maven tomcat插件,如下所示:
I have Maven POM file with some configuration and in the section plugins, I have maven tomcat plugin with some configuration like this:
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>tomcat</server>
</configuration>
我想使用该键将url设置导出到某些属性文件,例如tomcat.properties:
I'd like to export url setting to some property file for example tomcat.properties with that key:
url=http://localhost:8080/manager/html
该如何在我的POM文件中重新读取此密钥?
And how can I read this key back in my POM file?
推荐答案
Maven允许您在项目的POM中定义属性.您可以使用类似于以下内容的POM文件进行此操作:
Maven allows you to define properties in the project's POM. You can do this using a POM file similar to the following:
<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
标记中指定属性,而将命令行中的值传递为:
You can avoid specifying the property within the properties
tag, and pass the value from the command line as:
mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal
现在,如果您不想从命令行中指定它们,并且如果您需要将这些属性与项目POM进一步隔离,请放入属性文件中,那么您将需要使用初始化Maven生命周期的阶段.此处复制了插件页面中的示例:
Now, if you don't want to specify them from the command line and if you need to further isolate these properties from the project POM, into a properties file, then you'll need to use the Properties Maven plugin, and run it's read-project-properties
goal in the initialize phase of the Maven lifecycle. The example from the plugin page is reproduced here:
<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>
这篇关于从Maven POM文件读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!