Access Maven Pom variables from properties file

I am developing a custom maven plugin. When I write:

${project.version} 

in my pom file I can get its value, however is there any way if I write to the properties file:

 project.version = ${project.version} 

which will set the value of project.version correctly, how can I implement it in my Java code?

PS: I do not use annotations in my Mojo, and I do not want to use variables in my Java code, because the user must define my variables as in the properties file, and I cannot change my main Java code to change the situation.

+6
source share
3 answers

You can use Maven resource filtering by simply adding <resources> inside the <build> and turning on filtering for anything.

+6
source

in your mojo if using annotations see http://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html use

 @Component protected MavenProject project; 

with doclet

 /** * The Maven project to act upon. * * @parameter expression="${project}" * @required */ private MavenProject project; 

then project.getVersion ()

0
source

You can read and save any property in your pom.xml , as I do in this function:

 /** * Save a property in a pom.xml * * @param propertyName Name of the property * @param value New value for the property */ public static void saveProjectProperty(String propertyName, String value) { Model model = null; FileReader reader = null; MavenXpp3Reader mavenreader = new MavenXpp3Reader(); try { reader = new FileReader("pom.xml"); model = mavenreader.read(reader); MavenProject project = new MavenProject(model); while (project.getParent() != null) { project = project.getParent(); } project.getProperties().put(propertyName, value); try (FileWriter fileWriter = new FileWriter("pom.xml")) { project.writeModel(fileWriter); } } catch (IOException ex) { LOG.severe("Error saving pom.xml"); } catch (XmlPullParserException ex) { LOG.warning("Error reading pom.xml"); } } 

To be able to use clans that are not part of the JVM, you must add these dependencies:

 <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-model</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-project</artifactId> <version>2.0</version> </dependency 
0
source

All Articles