Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PMD run at the start of the maven build rather than at the end of it?

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?

like image 705
abbasdgr8 Avatar asked Jul 18 '14 11:07

abbasdgr8


People also ask

What is PMD rule set?

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.

How do I skip a PMD violation?

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.

What are the 3 build lifecycle of Maven?

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.


4 Answers

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

like image 195
JamesB Avatar answered Oct 14 '22 11:10

JamesB


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. :)

like image 31
abbasdgr8 Avatar answered Oct 14 '22 11:10

abbasdgr8


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.

like image 23
Petr Mensik Avatar answered Oct 14 '22 10:10

Petr Mensik


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>
like image 33
Yan Khonski Avatar answered Oct 14 '22 09:10

Yan Khonski