Reputation: 187
I want to pass parameters from the pom.xml to the java files.
I tried to set a properties in the pom.
The java files didn't read them.
Upvotes: 0
Views: 1975
Reputation: 70909
---- Here's the 2 minute introduction to filtering resource files ----
First you need to specify that you want your resource filtered, by adding a "filtering" element, containing the "true" value.
In this example, I reconfigured the default resource directory:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.edwinbuck.examples</groupId>
<artifactId>template-value</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Template example</name>
<description>
A simple pom file that copies a value into a propertie file.
</description>
<licenses>
<license>
<name>The MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.project.outputEncoding>UTF-8</project.project.outputEncoding>
<property.to.be.copied>some-value</property.to.be.copied>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
This implies that the default resource directory exists, which (to match the example) should be at
${basedir}/src/main/resources
Inside that directory, there should be a resource (a file). For this example, the file's contents look like
maven.build.number=${project.version}
customValue=${property.to.be.copied}
And the file is named
${basedir}/src/main/resources/example.properties
Upon a mvn build
this will create a copy of the file into
${basedir}/target/classes/example.properties
but the contents of the file under the target directory will be
maven.build.number=1.0-SNAPSHOT
customValue=some-value
I hope this example (tested on Maven 3.5.4) will provide you with enough of a template to make better use of the documentation.
Cheers!
---- Original posts follow ----
Maven strives to have reproducible builds. That means that if the build required input, you would have to provide exactly the same input to get a proper reproduction, each time.
Effectively this won't happen, as you might be unavailable, or might not type in the same item. So, I would look to the src/main/resources directory and create a properties file. Put the settings in there, and then rewrite your program to use settings from there.
When reading the properties file, there are a number of options. Look to the technique that loads the properties file from the ClassLoader, as it is the approach that has the best maintainability, and offers the most amount of flexibility in the placement of the properties file.
In the event you want to support more than one kind of build, and all the builds can be done without human input, you might create a maven profile for each build. That said, each profile will still be a reproducible build.
--- I see you've changed your wording, here's an update ---
Use a template, and the copy-resources target of the resources plugin. You can leverage it to replace items in the resources as they get moved into the target directory
Upvotes: 2