6

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.

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72

2 Answers2

7

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

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68
7

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

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • Within that profile, are you allowed to run two plugins? one right after another? – Rob Avery IV Mar 20 '13 at 14:03
  • Yes, you can have as many plugins as you like in the profile. Each profile has a `build/plugins` section, where you can list plugins just like you're doing in your main build. – nwinkler Mar 20 '13 at 14:04
  • If I do have two plugin within one profile, how do I say for which one to run first? – Rob Avery IV Mar 20 '13 at 16:56
  • Each plugin execution is bound to a phase, some plugins are bound to a phase by default, e.g. the jar plugin, others not. You can use the phase tag to specify when a plugin is executed. See here for more details on phases: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html – nwinkler Mar 20 '13 at 20:16