Code auditing with Maven

Every time you code and promise you’ll eliminate duplicated code later, replace System.out.println with a proper logger tomorrow, refactor that 15-options-if before the weekend you get into a debt you can’t pay. Even worst, your already big debt gets bigger. So, there are not more loans for you. Here’s a code auditing profile to be run in every build.

So, the build process is going to execute a PMD check and a Findbugs check. In order for these reports to be able to navigate through the code it’s necesary to enable this cross-reference with JXR. Later I will add CheckStyle when I can import the code conventions and formatting I’m already using with Eclipse IDE.

JXR is not executed during the build because browsing the code is something you would do from a report and not from a text file or the errors in the console, but you can enable this by executing it before the PMD and Findbugs goals are executed, maybe in the process-classes and process-test-classes phases when you know the code compiles and before it’s verified by the other plugins.

PMD will run a check in the verify phase of the build and make a report in the site phase of the reporting. The difference between these two is that check will fail the build if violations occur, and the report won’t. Checking in the verify phase gives you the chance to compile, test and package without worrying yet about your pending tasks on code quality. Findbugs will run in the verify phase as well. If you have your own rules-file for PMD you can add it inside a ruleset tag.

Finally, all three plugins are included in the reporting tag so the appear in the project’s site.

<profile>
<id>auditoria-codigo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<!-- Referencia cruzada al codigo fuente -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>${plugin.jxr.version}</version>
<executions>
<execution>
<id>cross-reference-source</id>
<phase>pre-site</phase>
<goals>
<goal>jxr</goal>
<goal>test-jxr</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- PMD -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${plugin.pmd.version}</version>
<configuration>
<targetJdk>${java.jdk.version}</targetJdk>
<rulesets>
<ruleset>
rules/path/file.xml
</ruleset>
</rulesets>
<includeTests>true</includeTests>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<id>check-pmd</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
<execution>
<id>report-pmd</id>
<phase>site</phase>
<goals>
<goal>pmd</goal>
<goal>cpd</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Findbugs -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${plugin.findbugs.version}</version>
<configuration>
<includeTests>true</includeTests>
<relaxed>true</relaxed>
</configuration>
<executions>
<execution>
<id>check-4bugs</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>${plugin.jxr.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${plugin.pmd.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${plugin.findbugs.version}</version>
</plugin>
</plugins>
</reporting>
</profile>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s