我有带有一些配置的 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
标记,并从命令行传递值,如下所示:
mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal
现在,如果您不想从命令行指定它们,并且需要进一步将这些属性与项目 POM 隔离到属性文件中,那么您需要使用属性 Maven 插件 https://www.mojohaus.org/properties-maven-plugin/,然后运行它read-project-properties
目标在Maven 生命周期的初始化阶段 https://www.mojohaus.org/properties-maven-plugin/usage.html。插件页面的示例复制如下:
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)