0

I have the following pom config. I added the cobertura plugin, and now pmd, cpd, findbugs, and test are running twice.

I understand that is because of my "phases" config, but I don't understand how can I achieve the following:

What I want is before I commit to the repo, build my app and check for pmd errors, findbugs errors, check my tests, and check my cobertura.

How can I achieve this? I am used to run "mvn clean package" before commit. It's that ok?

Here's my config:

...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.8</version>
        <configuration>
            <linkXref>false</linkXref>
            <rulesets>
                <!-- Custom Ruleset -->
                <ruleset>codequality/pmd.xml</ruleset>
            </rulesets>
        </configuration>
        <executions>
            <execution>
                <phase>compile</phase>
                <goals>
                    <goal>pmd</goal>
                    <goal>cpd</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>3.0.4</version>
        <executions>
            <execution>
                <phase>compile</phase>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.7</version>
        <configuration>
            <check>
                <haltOnFailure>true</haltOnFailure>
                <branchRate>70</branchRate>
                <lineRate>70</lineRate>
                <totalBranchRate>70</totalBranchRate>
                <totalLineRate>70</totalLineRate>
                <packageLineRate>70</packageLineRate>
                <packageBranchRate>70</packageBranchRate>
            </check>
        </configuration>
        <executions>
            <execution>
                <phase>compile</phase>
                <goals>
                    <goal>clean</goal>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>
Fernando Fradegrada
  • 973
  • 1
  • 9
  • 26

1 Answers1

0

Any maven plugin has a default execution phase. In this case, the plugins that you apply are executed in the verify phase (pmd-plugin). But you are defining it to run also in the compile phase. Removes the phase tag and lets it run in the verification phase.

<execution>
   <!--<phase>compile</phase>-->
   <goals>
       <goal>...</goal>
       ...
   </goals>
</execution>

Finally, the good practice to validate your project before a commit is to run:

mvn clean verify

Sergio Gragera
  • 786
  • 7
  • 10
  • And which should be the maven command tu run in Jenkins (after my commit is done)?? Nowadays is "mvn clean package" too. – Fernando Fradegrada Jun 01 '17 at 18:22
  • you should run mvn verify after (or before commit if you are not using jenkins). surefire, failsafe, jshint... and other plugins are executed at verify phase. If you runs mvn package those plugins are not executed unless you define a execution with previous phase in plugin config (but when you execute install you run the plugin twice if your execution phase is not default phase for the plugin). – Sergio Gragera Jun 01 '17 at 20:44
  • Thanks, but I did not understand what to execute in the Jenkins config? Same as in my dev machine? – Fernando Fradegrada Jun 01 '17 at 20:57
  • Yes. You should execute mvn verify -s settings.xml on jenkins (settings.xml is optional but needed in case you use own repo). – Sergio Gragera Jun 01 '17 at 21:00
  • So I've tried removing the phase line of all my plugins, and when I run "mvn clean verify", now test are being triggered twice. – Fernando Fradegrada Jun 01 '17 at 21:09
  • Maven prints on shell the execution phase and goal for each plugin. You can see before test the plugin that runs (and you see that first is executed by one plugin and second bu other). – Sergio Gragera Jun 01 '17 at 21:16
  • Yes but if I have a set of 500 tests, that will result in running twice.. Will be 1000 tests... – Fernando Fradegrada Jun 01 '17 at 21:34