Zhenya
Zhenya

Reputation: 6198

How to bind maven plugin execution to Snapshot versions of the project only?

What I want to do is whenever I run "mvn install" command I need some plugin to be executed. Specifically, I need the sonar plugin to be executed which would check if there are any critical issues in the project. If there are, the build would fail.

However, I want this plugin to execute on the "development" branch only. The version of the project on development branch looks like this: 1.0.5-SNAPSHOT. The version of the project on master branch: 1.0.5

So, how do I run the plugin only on development branch? I figured, I could probably do this based on the version (snapshot only), but I'm not sure if it's possible

Upvotes: 4

Views: 1185

Answers (2)

smicyk
smicyk

Reputation: 107

I think you can achieve this behaviour in the following way. First add build-helper-maven-plugin plugin to your build (on 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 goal executes beanshell script which defines skip.sonar property with true or false depending whether your version has snapshot at the end or not.

Then you can use skip property of sonar maven plugin.

<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>

Upvotes: 3

carlspring
carlspring

Reputation: 32567

What you're trying to do is not supported out of the box -- a plugin cannot automatically be triggered based on your project's <version/>. (Well, this is not exactly true, but you would have to be the author of the plugin and write your own logic for this, if you really wanted to; however this would only be applicable to your plugin).

What I would suggest would be to create Maven profile and trigger it via a command-line variable (-DmyParam), or by specifying the profile's name (-PmyDevProfile).

Have a look at the following links to gain a better understanding of Maven profiles:

Upvotes: 1

Related Questions