Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 490
Rob Avery IV Avatar asked Mar 20 '13 13:03

Rob Avery IV


People also ask

What is P option in Maven?

A reference tells us that -P specifies which profile Maven is to run under. Projects can define multiple profiles which may pull in different dependencies, so this is required if you have a project that can do that.

How do I run a specific goal in Maven?

Run a Maven goal from the context menu In the Maven tool window, click Lifecycle to open a list of Maven goals. Right-click the desired goal and from the context menu select Run 'name of the goal'. IntelliJ IDEA runs the specified goal and adds it to the Run Configurations node.

What is -- batch mode in Mvn?

-B, --batch-mode. Run in non-interactive (batch) mode. Batch mode is essential if you need to run Maven in a non-interactive, continuous integration environment.


1 Answers

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

like image 67
nwinkler Avatar answered Oct 11 '22 14:10

nwinkler