Enterprise Java

Maven Reports in Jenkins

Code quality is a sensitive topic. It affects your maintenance cost as well as your customer satisfaction. Not to mention your developers motivation to work with the code. Who wants to fix ugly code, right?

Discussing code quality always needs hard facts and numbers! So this is a short tutorial how to create some simple reports to analyze some code quality metrics.

Reports

This section will shorty explain the used reports.
 

Findbugs

FindBugs looks for bugs in Java programs. It is based on the concept of bug patterns. A bug pattern is a code idiom that is often an error

FindBugs Analysis
FindBugs Analysis

Checkstyle

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

Checkstyle Analysis
Checkstyle Analysis

Cobertura Code Coverage

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.

Cobertura Report
Cobertura Report

Surefire Test Report

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports…

Surefire Testreport
Surefire Testreport

Basic pom.xml

Starting with a basic pom configuration:

<project>

  ...
  <properties>
     <findbugs.version>2.5.2</findbugs.version>
     <checkstyle.version>2.9.1</checkstyle.version>
     <surefire.reportplugin.version>2.12.4</surefire.reportplugin.version>
     <cobertura.version>2.5.2</cobertura.version>
  </properties>

  <build>
     <plugins>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>findbugs-maven-plugin</artifactId>
           <version>${findbugs.version}</version>
        </plugin>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>cobertura-maven-plugin</artifactId>
           <version>${cobertura.version}</version>
           <configuration>
               <formats>
                   <format>xml</format>
               </formats>
           </configuration>
        </plugin>
     </plugins>
  </build>

  <reporting>
     <plugins>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>findbugs-maven-plugin</artifactId>
           <version>${findbugs.version}</version>
        </plugin>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-checkstyle-plugin</artifactId>
           <version>${checkstyle.version}</version>
        </plugin>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-report-plugin</artifactId>
           <version>${surefire.reportplugin.version}</version>
        </plugin>
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>cobertura-maven-plugin</artifactId>
           <version>${cobertura.version}</version>
           <configuration>
               <formats>
                   <format>xml</format>
               </formats>
           </configuration>
        </plugin>
      </plugins>
   </reporting>
</project>

Jenkins Plugins

You need to install a few jenkins plugins to get a nice integration with your reports.

Project Configuration

Now you need to configure your project to show the results of your reports.

Findbugs and Checkstyle

FindBugs and Checkstyle
FindBugs and Checkstyle

You can configure them in the “build configuration” tab. There are some limits to set, which influence the representation.

Cobertura

Cobertura Config
Cobertura Config

Cobertura is configured in the “post-build actions”. Same configurations as in the findbugs and checkstyle plugin.

Result

On your main page of your project you have some new graphs and links.

Jenkins Trend Graphs
Jenkins Trend Graphs

 

Jenkins Navbar
Jenkins Navbar

 

Reference: Maven Reports in Jenkins from our JCG partner Nepomuk Seiler at the mukis.de blog.

Nepomuk Seiler

Nepomuk Seiler is a information sience student at the Ludwigs-Maximilians-University Munich. He uses Scala for machine learning tasks and web development with play. Also he provided some small contributions to the Eclipse community.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Matthias Reining
11 years ago

Hi Nepomuk,
very nice article! Gives a very good overview for different reporting plugins.

Do you also know JaCoCo for code coverage? In my opinion JaCoco is very smart and works also together with Arquillian.

Ciao, Matthias

Nepomuk
Nepomuk
11 years ago

Hi Matthias,

I already tested JaCoCo. The Eclipse integration is fantastic and it works smoothly with Java 7.
The setup is as easy as Cobertura. If you have a lot of Eclipse developers this should be your
first choice.

cheers,
Muki

dleh
dleh
11 years ago

Good write-up, all it’s missing is compiler warnings from ex. Eclipse Java Compiler :)

GT
11 years ago

Good article Nipomuk…. but was curious as to why one needs to do this setup onself…. if you use Sonar…it will do this and much much more…. also, Jenkins has a Sonar plugin….so it is very easy to integrate Sonar into CI cycle….

cheers,
GT

Nepomuk
Nepomuk
11 years ago

Hi GT,

Well sometimes you don’t need all the big setup in the beginning and just want to get started fast :)
The sonar stuff is really awesome, however in my team we just needed something small, but as
we grow bigger the more we look for stuff like sonar.

cheers,
Muki

Back to top button