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进一步隔离到一个属性文件中,则需要使用Properties
Maven插件并运行它的
read-project-properties目标在Maven生命周期的初始化阶段。此处复制了插件页面的示例:
<project> <build> <plugins> <plugin> <groupId>org.prehaus.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>



