Vaibhav Nigam
Vaibhav Nigam

Reputation: 1467

Maven execute only one plugin among two based on a property

I use two maven plugins among which I want to execute only one depending upon property passed as argument.

<properties>
  <global>false</global>
</properties>

<build>
  <plugins>
    <plugin>
      <groupId>..</groupId>
      <artifactId>plugin1-runs-globally</artifactId>
      <version>..</version>
      <configuration>
        <skip>!${global}</skip>
      </configuration>
      <executions>..</executions>
    </plugin>

    <plugin>
      <groupId>..</groupId>
      <artifactId>plugin2-runs-locally</artifactId>
      <version>..</version>
      <configuration>
        <skip>${global}</skip>
      </configuration>
      <executions>..</executions>
    </plugin>
  </plugins>
</build>

mvn clean install -Dglobal=true

should run only plugin1 and not plugin2 whereas

mvn clean install

should run only plugin2 and not plugin1.

But !$(global) seems symantically wrong. Can someone help me with a workaround?

Upvotes: 0

Views: 1258

Answers (1)

Jeroen
Jeroen

Reputation: 3146

As far as I know you can't use any boolean type properties, but I would advise you to use profiles for this.

What you can do is create two profiles:

<profiles> <profile> <id>non-global</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>..</groupId> <artifactId>plugin1-runs-locally</artifactId> <version>..</version> </plugin> </plugins> </build> </profile> </profiles>

This profile is used when nothing is specified (so always).

For the global plugin, you can have a profile which is either activated by a maven profile id. You can also use a property to active a profile if you like. See for instance the following configuration.

<profiles> <profile> <id>global</id> <!-- optional --> <activation> <property> <name>global</name> </property> </activation> <!-- optional --> <build> <plugins> <plugin> <groupId>..</groupId> <artifactId>plugin2-runs-locally</artifactId> <version>..</version> </plugin> </plugins> </build> </profile> </profiles>

Now when you want to execute the plugin related to the global profile all you need to do is:

mvn clean install -Pglobal

Or when using a property:

mvn clean install -Dglobal

And if you want the non-global plugin to execute, you can use

mvn clean install

See the maven profile documentation for more background information.

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Upvotes: 1

Related Questions