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>
I'd like to export url setting to some property file for example tomcat.properties with that key:
url=http://localhost:8080/manager/html
And how can I read this key back in my POM file?
Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time.
In maven pom. xml , a property is accessed by using ${property_name} . You can define your custom properties in Maven.
Maven supports a hierarchy of different properties to allow specifying defaults and overriding them at the appropriate level. The properties files in Maven are processed in the following order: Built-in properties are processed. ${basedir}/project. properties ( basedir is replaced by the directory where the project.
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>
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
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>
Complete working example available at: http://hg.defun.work/exp/file/tip/maven/properties
Here essential part of 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>
As you can see properties-maven-plugin still at alpha stage, that is why I hate Maven as build tools...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With