Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set maven property from plugin

I've read some questions here about how to set a property (most of them talked about the version number for an application) from a maven plugin. It seems there's no easy way of doing this and the best solution I found is to have a filter.properties file which is updated from the plugin and used by the main pom file to filter the desired resources.

I tried another solution after I read this from the Maven documentation (Maven filter plugin):

Variables can be included in your resources. These variables, denoted by the ${...} delimiters, can come from the system properties, your project properties, from your filter resources and from the command line.

I found interesting that variabled can be read from system properties. So, I modified my plugin to set a system property like this:

System.setProperty("currentVersion", appCurrentVersion);

However, filtered resources don't seem to read this value. Could anybody tell me what's wrong with this approach?

UPDATE: I'm running my plugin in the validate phase.

Thanks a lot.

like image 331
PaquitoSoft Avatar asked Sep 26 '11 10:09

PaquitoSoft


People also ask

What is properties Maven plugin?

The Properties Maven Plugin is here to make life a little easier when dealing with properties. It provides goals to read properties from files and URLs and write properties to files, and also to set system properties. It's main use-case is loading properties from files or URLs instead of declaring them in pom.

How do I set system properties in POM xml?

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!"

Where are Maven properties set?

You can also reference any properties in the Maven Local Settings file which is usually stored in ~/. m2/settings. xml. This file contains user-specific configuration such as the location of the local repository and any servers, profiles, and mirrors configured by a specific user.

How do I use Maven plugins?

Introduction. In Maven, there are two kinds of plugins, build and reporting: Build plugins are executed during the build and configured in the <build/> element. Reporting plugins are executed during the site generation and configured in the <reporting/> element.


2 Answers

Don't set it as System Property, set it as Maven Project property

// inject the project
@Parameter(defaultValue = "${project}")
private org.apache.maven.project.MavenProject project;

// and in execute(), use it:
project.getProperties().setProperty("currentVersion", appCurrentVersion);

See:

  • Mojo Developer Cookbook
  • MavenProject javadoc

An edit suggested using Properties.put() instead of Properties.setProperty(). While technically, Properties implements Map, this usage is discouraged explicitly in the Properties javadoc.

like image 80
Sean Patrick Floyd Avatar answered Oct 22 '22 20:10

Sean Patrick Floyd


Maven sets properties in initialize phase. I assume that in that phase maven loads system properties. And after that maven doesn't load system properties again. If you try to add a system property after this phase than it's not loaded.

Try to run your plugin in validate phase.

like image 23
amra Avatar answered Oct 22 '22 20:10

amra