How to bind maven plugin execution to project snapshots only?
What I want to do is when I run the command "mvn install" I need a plugin to execute. Specifically, I need a sonar plugin that will check if there are any critical issues in the project. If so, the build will fail.
However, I only want this plugin to run on the "development" branch. The development branch project version is as follows: 1.0.5-SNAPSHOT. Master branch version of the project: 1.0.5
So how do I run the plugin only on the development branch? I thought I could do it based on version (snapshot only) but I'm not sure if this is possible
source to share
What you are trying to do is not supported out of the box - the plugin cannot automatically launch based on your project <version/>
. (Well, that's not entirely true, but you must be the plugin author and write your own logic for this if you really wanted to, but this only applies to your plugin).
I would suggest creating a Maven profile and running it via a command line variable ( -DmyParam
) or specifying the profile name ( -PmyDevProfile
).
Take a look at the following links to better understand Maven profiles:
source to share
I think you can achieve this behavior in the following way. First add the build-helper-maven-plugin plugin to your build (at the beginning).
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>bsh-property</id>
<goals>
<goal>bsh-property</goal>
</goals>
<configuration>
<properties>
<property>skip.sonar</property>
</properties>
<source>skip.sonar = project.getVersion().endsWith("-SNAPSHOT") ? false : true;</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The bsh-property target executes a beanshell script that defines the skip.sonar property to be true or false, depending on whether your version has a snapshot at the end or not.
Then you can use skip property for sonar maven module.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
...
<configuration>
<skip>${skip.sonar}</skip>
</configuration>
</plugin>
</plugins>
</build>
source to share