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

2

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.

Daunt answered 20/3, 2013 at 13:10 Comment(0)
K
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

Keppel answered 20/3, 2013 at 13:57 Comment(4)
Within that profile, are you allowed to run two plugins? one right after another?Daunt
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.Keppel
If I do have two plugin within one profile, how do I say for which one to run first?Daunt
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: maven.apache.org/guides/introduction/…Keppel
G
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

Glanders answered 20/3, 2013 at 13:14 Comment(0)
K
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

Keppel answered 20/3, 2013 at 13:57 Comment(4)
Within that profile, are you allowed to run two plugins? one right after another?Daunt
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.Keppel
If I do have two plugin within one profile, how do I say for which one to run first?Daunt
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: maven.apache.org/guides/introduction/…Keppel

© 2022 - 2024 — McMap. All rights reserved.