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>