I have the following configuration within my pom.xml that checks for PMD violations:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
When I run a build using the command mvn clean install
, the PMD checks are run as last step of the build process. Rather, I would want the PMD checks to run as the first step of the build.
Does anybody know how could I achieve this?
A ruleset is an XML configuration file, which describes a collection of rules to be executed in a PMD run. PMD includes built-in rulesets to run quick analyses with a default configuration, but users are encouraged to make their own rulesets from the start, because they allow for so much configurability.
Is there any way to skip the PMD or CPD reports temporarily? Yes, each report supports a skip parameter which you can pass on the command line, -Dpmd. skip=true and -Dcpd. skip=true respectively.
There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's web site.
Add the phase element to your POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
The validate phase is the first phase of the maven lifecycle: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
Thanks for your answers @JamesB and @PetrMensik for letting me know about the phase element within the POM. It helped me solve my problem. I finally settled for this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
I used the phase:compile, the reason being I have plenty of tests in my project which take up a lot of time to execute. And, its quite irritating to wait for those tests to finish and be notified about a PMD violation at the end of all the tests. I needed something just before the tests. Hence, I settled for compile.
Further suggestions are welcome. :)
You need to hook execution of this plugin to a different Maven lifecycle phase (validation
comes as the first one in default lifecycle).
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
See this the list of the available Maven phases for reference.
I set build configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
<configuration>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With