Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access maven properties defined in the pom

Tags:

java

maven

How do I access maven properties defined in the pom in a normal maven project, and in a maven plugin project?

like image 487
SparedWhisle Avatar asked Jul 16 '12 08:07

SparedWhisle


People also ask

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.

Where are Maven properties defined?

Maven Settings Properties. You can also reference any properties in the Maven Local Settings file which is usually stored in ~/. m2/settings. xml.

How do you use properties in POM?

Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time. That worked, but made Eclipse fall into infinite build / deploy-to-Tomcat loop (build -> generate my. properties -> resource changed -> build), so I had to change phase to compile .

How do I set system properties in Maven POM?

To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline. Run Single Test with Maven : $ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"


1 Answers

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 your pom.xml:

<properties>      <name>${project.name}</name>      <version>${project.version}</version>      <foo>bar</foo> </properties>  <build>     <plugins>         <plugin>             <groupId>org.codehaus.mojo</groupId>             <artifactId>properties-maven-plugin</artifactId>             <version>1.0.0</version>             <executions>                 <execution>                     <phase>generate-resources</phase>                     <goals>                         <goal>write-project-properties</goal>                     </goals>                     <configuration>                         <outputFile>${project.build.outputDirectory}/my.properties</outputFile>                     </configuration>                 </execution>             </executions>         </plugin>     </plugins> </build> 

And then in .java:

java.io.InputStream is = this.getClass().getResourceAsStream("my.properties"); java.util.Properties p = new Properties(); p.load(is); String name = p.getProperty("name"); String version = p.getProperty("version"); String foo = p.getProperty("foo"); 
like image 158
Leif Gruenwoldt Avatar answered Sep 18 '22 13:09

Leif Gruenwoldt