Maven: Only execute plugin when a command line flag is present

瘦欲@ 提交于 2019-12-01 21:20:18

问题


I want Maven to only run a certain plugin when there is a flag on the command line when I call the mvn command.

for example:

Let's say I have a plugin called maven-foo-plugin. I only want maven to run this plugin when the flag --foo is present when I call the maven command.

So, instead of saying...

mvn install

...I would say...

mvn install --foo

The first one should NOT use/call the plugin maven-foo-plugin, but the second one should. By default, I don't want this plugin to run, but if and only if, the --foo is present. Is there another parameter that I add to this maven-foo-plugin to make it do this?

The plugin I'm using is the maven-antrun-plugin that has the task that unzips a file. Of course, sometimes this file is present and sometimes not. Sometimes, it's not present and I don't need it. So, I need a way (Preferably through command line unless someone has a better idea) to turn on/off this plugin.


回答1:


As @Gab has mentioned, using Maven profiles is the way to go. Create a profile and configure your plugin in the profile. Then in the profile's activation section, reference the environment variable, with or without a value:

<profiles>
  <profile>
    <activation>
      <property>
        <name>debug</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

The above example would activate the profile if you define the debug variable when calling Maven:

mvn install -Ddebug

Please note that you have to prefix environment variables with -D in order to pass them to the JVM running Maven.

More details can be found here: http://maven.apache.org/guides/introduction/introduction-to-profiles.html




回答2:


The correct way to trigger conditional action in maven is to use profile. You can so configure a specific profile including the plugin activation, you will then trigger the execution using

mvn targetPhase -P myprofile

(you can also specify a specific property value to activate the profile)

see Maven: Using a Plugin Based On Profile



来源:https://stackoverflow.com/questions/15524500/maven-only-execute-plugin-when-a-command-line-flag-is-present

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