How do I access maven properties defined in the pom in a normal maven project, and in a maven plugin project?
In maven pom. xml , a property is accessed by using ${property_name} . You can define your custom properties in Maven.
Maven Settings Properties. You can also reference any properties in the Maven Local Settings file which is usually stored in ~/. m2/settings. xml.
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 .
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!"
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");
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