Are*_*rek 31 java tomcat maven-2 pom.xml properties-file
我有Maven POM文件的一些配置,在插件部分,我有maven tomcat插件,其中有一些配置如下:
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>tomcat</server>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我想将url设置导出到某个属性文件,例如使用该键的tomcat.properties:
url=http://localhost:8080/manager/html
Run Code Online (Sandbox Code Playgroud)
如何在POM文件中读取此密钥?
Vin*_*lds 36
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>
Run Code Online (Sandbox Code Playgroud)
您可以避免在properties
标记中指定属性,并将命令行中的值传递为:
mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal
Run Code Online (Sandbox Code Playgroud)
现在,如果您不想从命令行指定它们,并且如果您需要将这些属性从项目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>
Run Code Online (Sandbox Code Playgroud)
gav*_*koa 10
完整的工作示例: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>
Run Code Online (Sandbox Code Playgroud)
正如你可以看到properties-maven-plugin仍处于alpha阶段,这就是为什么我讨厌Maven作为构建工具......
小智 7
实际上不可能使用接受的答案中的指令从文件加载属性,因为这些属性在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>
Run Code Online (Sandbox Code Playgroud)
进入dev.properties
:
foo=bar
Run Code Online (Sandbox Code Playgroud)
然后runnin命令mvn validate
产生输出:
[echo] Displaying value of properties
[echo] [foo] bar
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
68331 次 |
最近记录: |