Set maven property from plugin

怎甘沉沦 提交于 2020-01-13 07:46:08

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/7553530/set-maven-property-from-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!