Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading properties file from Maven POM file

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?

like image 799
Arek Avatar asked Aug 22 '11 08:08

Arek


People also ask

How read Pom properties file in Java?

Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time.

How do I access property in POM XML?

In maven pom. xml , a property is accessed by using ${property_name} . You can define your custom properties in Maven.

What is maven property file?

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.


2 Answers

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> 
like image 123
Vineet Reynolds Avatar answered Oct 06 '22 00:10

Vineet Reynolds


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...

like image 20
gavenkoa Avatar answered Oct 06 '22 01:10

gavenkoa