Friday, February 5, 2016

Implement PMD, CPD, Findbugs, Java Code Coverage, JavaSources, JavaDocs and Checkstyle using maven



We can configure all the plugins (PMD,CPD,FindBugs, Jacoco, JavaSources and JavaDocs) as part of build life (default) cycle as well as site life cycle. Here we will see both.

There are 3 standard life cycles in maven. Clean, Build and Site.

In general,

  1. clean (which includes pre-clean,clean and post-clean) life cycle is used to clean the build directory.
  2. build (the default) life cycle (which includes validate, initialize, generate-sources, compile, test, package, verify, install and deploy etc.) used to build artifacts. It is the default life cycle in maven.
  3. site (which includes pre-site,site-post-site and site-deploy) lifecycle is used to create fresh documentation to create reports, deploy site etc.

If you want to know more  about maven life-cycles then refer below given link:




Prerequisites: You will need following plug-ins in order to implement PMD, CPD, FindBugs, JavaDocs, JavaSources and Java code coverage.

  JavaDocs:
  1. maven-javadoc-plugin (version 2.10.3)
  JavaSources:
  1. maven-source-plugin (version 2.2.1)

  Java code coverage:
  1. jacoco-maven-plugin (version 0.7.6.201602180812)
  PMD:
  1. maven-jxr-plugin (version 2.5)
  2. maven-pmd-plugin (version 3.6)
  FindBugs:
  1. findbugs-maven-plugin (version 3.0.3)
Checkstyles:
  1. maven-checkstyle-plugin (version 2.17)
 Other Plugins to support build and reporting:
  1. maven-resources-plugin (version 2.4)
  2. maven-project-info-reports-plugin (version 2.8.1)
  3. maven-surefire-report-plugin (version 2.19.1)
Optional Plugins:
  1. maven-compiler-plugin (version 3.3) -- we need to override the maven compiler plug-in to set JDK version if your code is written in JDK 7 and above

To execute all plugins together you can run following command: mvn install site

Note: If you are getting 'java.lang.OutOfMemoryError: PermGen space' during the build then you have to set "MAVEN_OPTS=-XX:MaxPermSize=1024m".

If you are using eclipse you can set argument in 'VM Arguments' while running the maven command.

Configuring Java Sources maven plug-in:

This plug-in will generate a jar file containing the source code.


<plugin>

  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <version>2.2.1</version>
  <executions>
 <execution>
   <id>attach-sources</id>
   <goals>
  <goal>jar-no-fork</goal>
   </goals>
 </execution>
  </executions>
</plugin>

Configuring Java Docs maven plug-in


  <!-- JavaDocs configuration-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy static resource files</id>
<phase>generate-sources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>


Configuring Java code coverage maven plug-in


Here we are configuring jacoco into build lifecycle under 'package' phage. This plug-in will throw exception if unit test cases are skipped

<!-- Java code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<!-- <outputDirectory>${project.reporting.outputDirectory}</outputDirectory> -->
</configuration>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

To configure multi module code coverage you can refer below website:


To get sample pom.xml which demonstrate java code coverage configuration you may look into below website:


https://github.com/pkainulainen/maven-examples/blob/master/code-coverage-jacoco/pom.xml



Configuring FindBugs maven plugin: 

Here we are configuring findbugs into build lifecycle under 'package' phage. See <phase> element inside <executions>/<execution> element. The <plugins> element is configured under <build> element which means the plugin will be executed during build (life cycle) process.

To run findbugs you can use following command:

mvn findbugs:findbugs 

If you want to run findbugs and want the build to fail if violations are found, then you can use following command:

mvn findbugs:check


<!-- FindBugs Configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<!-- FindBugs will show what is doing during analysis.  -->
<debug>true</debug>
<!--
Enables analysis which takes more memory but finds more bugs.
If you run out of memory, changes the value of the effort element
to 'min'. Min|Default|Max are possible values.
-->
<effort>Min</effort>
<!-- Reports all bugs (other values are High|Normal|Low|Exp|Ignore) -->
<threshold>High</threshold>
<xmlOutput>true</xmlOutput>
<!-- Optional directory to save findbugs xdoc xml report -->
<xmlOutputDirectory>${project.reporting.outputDirectory}</xmlOutputDirectory>
<!-- Indicates to analyze only given java packages, We are allowing everything here. -->
<!-- <onlyAnalyze>com.*</onlyAnalyze> -->
<failOnError>true</failOnError>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!-- This goal will check code and generate report -->
<goal>findbugs</goal>
 
<!-- Fail the build if there were any findBugs violations in the source code. Uncomment if needed -->
 <!-- This goal includes both findbugs and check -->
<!-- <goal>check</goal>-->
</goals>
</execution>
</executions>
</plugin>

To see usage details on findbugs you can refer below website:

http://gleclaire.github.io/findbugs-maven-plugin/usage.html


Configuring PMD maven plug-in

Here we are configuring pmd into build lifecycle under 'package' phage. See <phase> element inside <executions>/<execution> element. The <plugins> element is configured under <build> element which means the plugin will be executed during build (life cycle) process.

To run pmd you can use following command: mvn pmd:pmd  


To run cpd you can use following command: mvn pmd:cpd

If you want to run pmd and want the build to fail if violations are found, then you can use following commandmvn pmd:check

If you want to run cpd and want the build to fail if violations are found, then you can use following command: mvn pmd:cpd-check

For more details on pmd configuration options you can look here:



  <!-- PMD Integration configuration -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jxr</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.6</version>
<configuration>
<!-- Run PMD on the tests. Default: false -->
<includeTests>false</includeTests>

<!-- Skip the PMD checks. Most useful on the command line via "-Dpmd.skip=true" --> <skip>false</skip>
<!-- Print details of check failures to build output. --> <verbose>true</verbose>


<!-- Link the violation line numbers to the source xref. Links will be created
automatically if the jxr plugin is being used. Default: true -->
<linkXRef>true</linkXRef>
<!-- Set the output format type, in addition to the HTML report. Must be
one of: "none", "csv", "xml", "txt" or the full class name of the PMD renderer
to use. See the net.sourceforge.pmd.renderers package javadoc for available
renderers. XML is required if the pmd:check goal is being used. Default: xml-->
<format>xml</format>

<!-- Fail build if there are high priority warnings during pmd:check. Default value is 5 -->
<failurePriority>1</failurePriority>
<!-- A list of files to include from checking. Can contain Ant-style wildcards
and double wildcards. Defaults to **\/*.java.-->
<includes>
<include>com/**/*.java</include>
</includes>

<!-- A list of files to exclude from checking. Can contain Ant-style wildcards
and double wildcards. We are not excluding anything here. -->
<!-- <excludes>
 <exclude></exclude>
</excludes> -->
<!-- The output directory for the final HTML report.  -->
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
<!-- The output directory for the intermediate XML report. -->
<targetDirectory>${project.build.directory}</targetDirectory>
<rulesets>
<!-- Multiple rule set xmls can be included here. -->
<ruleset>${basedir}/pmd_ruleset.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!-- Creates a PMD report. -->
<goal>pmd</goal>
    <!-- Fail the build if there were any PMD violations in the source code. Uncomment if needed -->
 <!--<goal>check</goal>-->
<!-- Creates a report for PMD's copy paste detector tool -->
<goal>cpd</goal>
<!-- Fail the build if there were any CPD violations in the source code. Uncomment if needed -->
<!-- <goal>cpd-check</goal> -->
</goals>
</execution>
</executions>
</plugin>

Ruleset configuration (pmd_ruleset.xml):


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ruleset xmlns="http://pmd.sf.net/ruleset/1.0.0" name=""
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
PMD Plugin preferences rule set
Priority means: 1 – error, high priority, 2 – error,
normal priority, 3 – warning, high priority, 4 – warning, 
                normal priority and 5 – information.
</description>
<rule ref="rulesets/java/typeresolution.xml/LooseCoupling">
<priority>3</priority>
</rule>
<rule
ref="rulesets/java/typeresolution.xml/CloneMethodMustImplementCloneable">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/typeresolution.xml/UnusedImports">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/typeresolution.xml/SignatureDeclareThrowsException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/braces.xml/ForLoopsMustUseBraces">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/braces.xml/IfStmtsMustUseBraces">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/braces.xml/IfElseStmtsMustUseBraces">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/braces.xml/WhileLoopsMustUseBraces">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/ShortVariable">
<priority>1</priority>
<properties>
<property name="xpath">
<value>
//VariableDeclaratorId[string-length(@Image) &lt; 3]
</value>
</property>
</properties>
</rule>
<rule ref="rulesets/java/naming.xml/LongVariable">
<priority>4</priority>
<properties>
<property name="xpath">
<value>
//VariableDeclaratorId[string-length(@Image) > 40]
</value>
</property>
</properties>
</rule>
<rule ref="rulesets/java/naming.xml/ShortMethodName">
<priority>1</priority>
<properties>
<property name="xpath">
<value>
//MethodDeclarator[string-length(@Image) &lt; 2]
</value>
</property>
</properties>
</rule>
<rule ref="rulesets/java/naming.xml/VariableNamingConventions">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/MethodNamingConventions">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/ClassNamingConventions">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/AbstractNaming">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/AvoidDollarSigns">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/MethodWithSameNameAsEnclosingClass">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousHashcodeMethodName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousConstantFieldName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousEqualsMethodName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/AvoidFieldNameMatchingTypeName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/AvoidFieldNameMatchingMethodName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/NoPackage">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/PackageCase">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/MisleadingVariableName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/naming.xml/BooleanGetMethodName">
<priority>1</priority>
</rule>

<rule ref="rulesets/java/design.xml/UseUtilityClass">
<priority>1</priority>
   <!-- Ignore check for spring boot application -->
   <properties>
      <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration/preceding-sibling::Annotation/MarkerAnnotation/Name[@Image='SpringBootApplication']" />
  </properties>
</rule>

<rule ref="rulesets/java/design.xml/SimplifyBooleanReturns">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/SimplifyBooleanExpressions">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/SwitchStmtsShouldHaveDefault">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/AvoidDeeplyNestedIfStmts">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/AvoidReassigningParameters">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/design.xml/SwitchDensity">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/ConstructorCallsOverridableMethod">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/AccessorClassGeneration">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/FinalFieldCouldBeStatic">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/CloseResource">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/NonStaticInitializer">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/DefaultLabelNotLastInSwitchStmt">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/NonCaseLabelInSwitchStatement">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/OptimizableToArrayCall">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/BadComparison">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/EqualsNull">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/InstantiationToGetClass">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/IdempotentOperations">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/SimpleDateFormatNeedsLocale">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/ImmutableField">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/UseLocaleWithCaseConversions">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/AvoidProtectedFieldInFinalClass">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/AssignmentToNonFinalStatic">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/design.xml/MissingStaticMethodInNonInstantiatableClass">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/AvoidSynchronizedAtMethodLevel">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/design.xml/MissingBreakInSwitch">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/UseNotifyAllInsteadOfNotify">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/AvoidInstanceofChecksInCatchClause">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/design.xml/AbstractClassWithoutAbstractMethod">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/SimplifyConditional">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/CompareObjectsWithEquals">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/PositionLiteralsFirstInComparisons">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/PositionLiteralsFirstInCaseInsensitiveComparisons">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/UnnecessaryLocalBeforeReturn">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/NonThreadSafeSingleton">
<priority>1</priority>
</rule>

<rule ref="rulesets/java/design.xml/UncommentedEmptyMethodBody">
<priority>1</priority>
</rule>

<rule ref="rulesets/java/design.xml/UncommentedEmptyConstructor">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/AvoidConstantsInterface">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/UnsynchronizedStaticDateFormatter">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml">
<exclude name="ConfusingTernary" />
</rule>
<rule ref="rulesets/java/design.xml/UseCollectionIsEmpty">
<priority>1</priority>
</rule>

<rule ref="rulesets/java/design.xml/PreserveStackTrace">
<priority>1</priority>
</rule>

<rule
ref="rulesets/java/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal">
<priority>1</priority>
</rule>

<rule
ref="rulesets/java/design.xml/EmptyMethodInAbstractClassShouldBeAbstract">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/SingularField">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/ReturnEmptyArrayRatherThanNull">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/AbstractClassWithoutAnyMethod">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/design.xml/TooFewBranchesForASwitchStatement">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/design.xml/FieldDeclarationsShouldBeAtStartOfClass">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingThrowable">
<priority>2</priority>
</rule>
<rule
ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/ExceptionAsFlowControl">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingNPE">
<priority>3</priority>
</rule>
<rule
ref="rulesets/java/strictexception.xml/AvoidThrowingRawExceptionTypes">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/strictexception.xml/AvoidThrowingNullPointerException">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidRethrowingException">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/DoNotExtendJavaLangError">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/DoNotThrowExceptionInFinally">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/strictexception.xml/AvoidThrowingNewInstanceOfSameException">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingGenericException">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/strictexception.xml/AvoidLosingExceptionInformation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedFormalParameter">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/logging-java.xml/MoreThanOneLogger">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/logging-java.xml/LoggerIsNotStaticFinal">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/logging-java.xml/SystemPrintln">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/logging-java.xml/AvoidPrintStackTrace">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
<priority>1</priority>
<properties>
<property name="skipAnnotations">
<value>true</value>
</property>
<property name="maxDuplicateLiterals">
<value>4</value>
</property>
</properties>
</rule>
<rule ref="rulesets/java/strings.xml/StringInstantiation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringToString">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/InefficientStringBuffering">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UnnecessaryCaseChange">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseStringBufferLength">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/AppendCharacterWithChar">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/ConsecutiveAppendsShouldReuse">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/ConsecutiveLiteralAppends">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseIndexOfChar">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/InefficientEmptyStringCheck">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/InsufficientStringBufferDeclaration">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UselessStringValueOf">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringBufferInstantiationWithChar">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseEqualsToCompareStrings">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/strings.xml/AvoidStringBufferField">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceVectorWithList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceHashtableWithMap">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceEnumerationWithIterator">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/AvoidEnumAsIdentifier">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/AvoidAssertAsIdentifier">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/IntegerInstantiation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ByteInstantiation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ShortInstantiation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/LongInstantiation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/JUnit4TestShouldUseBeforeAnnotation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/JUnit4TestShouldUseAfterAnnotation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/JUnit4TestShouldUseTestAnnotation">
<priority>3</priority>
</rule>
<rule
ref="rulesets/java/migrating.xml/JUnit4SuitesShouldUseSuiteAnnotation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/JUnitUseExpected">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/UseProperClassLoader">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/MDBAndSessionBeanNamingConvention">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/RemoteSessionInterfaceNamingConvention">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/LocalInterfaceSessionNamingConvention">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/LocalHomeNamingConvention">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/RemoteInterfaceNamingConvention">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/DoNotCallSystemExit">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/StaticEJBFieldShouldBeFinal">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/j2ee.xml/DoNotUseThreads">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/MethodArgumentCouldBeFinal">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/LocalVariableCouldBeFinal">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/optimizations.xml/AvoidInstantiatingObjectsInLoops">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseArrayListInsteadOfVector">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/SimplifyStartsWith">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseStringBufferForStringAppends">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseArraysAsList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/AvoidArrayLoops">
<priority>2</priority>
</rule>
<rule
ref="rulesets/java/optimizations.xml/UnnecessaryWrapperObjectCreation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/AddEmptyString">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/RedundantFieldInitializer">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/PrematureDeclaration">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyCatchBlock">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyIfStmt">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyWhileStmt">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyTryBlock">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyFinallyBlock">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptySwitchStatements">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptySynchronizedBlock">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyStaticInitializer">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyStatementNotInLoop">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/empty.xml/EmptyInitializer">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UnnecessaryConversionTemporary">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UselessParentheses">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UnnecessaryReturn">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UnnecessaryFinalModifier">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UselessOverridingMethod">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UselessOperationOnImmutable">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/unnecessary.xml/UnusedNullCheckInEquals">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/JumbledIncrementer">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/ForLoopShouldBeWhileLoop">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/OverrideBothEqualsAndHashcode">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/DoubleCheckedLocking">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/ReturnFromFinallyBlock">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UnconditionalIfStatement">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BooleanInstantiation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/CollapsibleIfStatements">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/ClassCastExceptionWithToArray">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/basic.xml/AvoidDecimalLiteralsInBigDecimalConstructor">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/MisplacedNullCheck">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidThreadGroup">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BrokenNullCheck">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BigIntegerInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidUsingOctalValues">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidUsingHardCodedIP">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/CheckResultSet">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidMultipleUnaryOperators">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/basic.xml/ExtendsObject">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/basic.xml/CheckSkipResult">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidBranchingStatementAsLastInLoop">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/basic.xml/DontCallThreadRun">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/basic.xml/DontUseFloatTypeForLoopIndices">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/sunsecure.xml/MethodReturnsInternalArray">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/sunsecure.xml/ArrayIsStoredDirectly">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/coupling.xml/CouplingBetweenObjects">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/coupling.xml/ExcessiveImports">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/coupling.xml/LooseCoupling">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/imports.xml/DuplicateImports">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/imports.xml/DontImportJavaLang">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/imports.xml/UnusedImports">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/imports.xml/ImportFromSamePackage">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/imports.xml/UnnecessaryFullyQualifiedName">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/imports.xml/TooManyStaticImports">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/JUnitStaticSuite">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/JUnitSpelling">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/JUnitAssertionsShouldIncludeMessage">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/JUnitTestsShouldIncludeAssert">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/TestClassWithoutTestCases">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/UnnecessaryBooleanAssertion">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/UseAssertEqualsInsteadOfAssertTrue">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/UseAssertSameInsteadOfAssertTrue">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/UseAssertNullInsteadOfAssertTrue">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/junit.xml/SimplifyBooleanAssertion">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/NullAssignment">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/OnlyOneReturn">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/AssignmentInOperand">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/DontImportSun">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/SuspiciousOctalEscape">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/CallSuperInConstructor">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/UnnecessaryParentheses">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/DefaultPackage">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/BooleanInversion">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/AvoidUsingShortType">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/AvoidUsingVolatile">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/AvoidUsingNativeCode">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/AvoidAccessibilityAlteration">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/controversial.xml">
<exclude name="DataflowAnomalyAnalysis" />
<exclude name="UnnecessaryConstructor" />
<exclude name="AvoidFinalLocalVariable" />
<exclude name="AtLeastOneConstructor"/>
</rule>
<rule
ref="rulesets/java/controversial.xml/DoNotCallGarbageCollectionExplicitly">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NPathComplexity">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/ExcessiveMethodLength">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/ExcessiveParameterList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/ExcessiveClassLength">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/CyclomaticComplexity">
<priority>4</priority>
<properties>
<property name="reportLevel">
<value>9</value>
</property>
<property name="showClassesComplexity">
<value>true</value>
</property>
<property name="showMethodsComplexity">
<value>true</value>
</property>
</properties>
</rule>
<rule ref="rulesets/java/codesize.xml/ExcessivePublicCount">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/TooManyFields">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssMethodCount">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssTypeCount">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssConstructorCount">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/TooManyMethods">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/EmptyFinalizer">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeOnlyCallsSuperFinalize">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeOverloaded">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeDoesNotCallSuperFinalize">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeShouldBeProtected">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/AvoidCallingFinalize">
<priority>1</priority>
</rule>
<rule
ref="rulesets/java/logging-jakarta-commons.xml/UseCorrectExceptionLogging">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/logging-jakarta-commons.xml/ProperLogger">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/javabeans.xml/BeanMembersShouldSerialize">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/javabeans.xml/MissingSerialVersionUID">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/clone.xml/ProperCloneImplementation">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/clone.xml/CloneThrowsCloneNotSupportedException">
<priority>1</priority>
</rule>
<rule ref="rulesets/java/clone.xml/CloneMethodMustImplementCloneable">
<priority>1</priority>
</rule>
</ruleset>




Configuring Checkstyle maven plug-in

Here we are configuring checkstyle into build lifecycle under 'package' phage. See <phase> element inside <executions>/<execution> element. The <plugins> element is configured under <build> element which means the plugin will be executed during build (life cycle) process.

To run checkstyle you can use following command: mvn checkstyle:checkstyle

If you want to run checkstyle and want the build to fail if violations are found, then you can use following commandmvn checkstyle:check

Refer this page for more details:

<!-- Checkstyle plugin -->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
    <!-- Fail build if violations found -->
<failOnViolation>true</failOnViolation>
    <!-- External configuration file location -->
<configLocation>${basedir}/checkstyle.xml</configLocation>
    <!-- Includes packages as per the given wild card config. -->
<includes>com/**/*.java</includes>
    <!-- Excludes packages as per the given wild card config. -->
<excludes>com/github/pojo/**/*.java,com/github/abhinavmishra14/**/*Test.java</excludes>
</configuration>
</execution>
</executions>
</plugin>

Sample Checkstyle RuleSet (checkstyle.xml)

<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
<module name="TreeWalker">
<module name="JavadocMethod" />
<module name="JavadocType" />
<module name="JavadocVariable">
<property name="scope" value="private"/>
  <property name="excludeScope" value="protected"/>
</module>
<module name="JavadocStyle">
<property name="checkFirstSentence" value="false"/>
</module>
</module>
</module>

More details about configuration can be found here:




Configuring plugins for generating reports for PMD, FindBugs, Java code coverage and JUnit test:

When we use 'mvn site' command, it generate site for the project containing all the information of project such has dependencies used, plugins used, Unit test cases success/failure report, PMD report, FindBugs report, Java code coverage report etc.

To configure reporting we will put the above configurations under the <reporting> element inside pom.xml.

<reporting>
<plugins>

<!-- Reporting plugin -->
<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-project-info-reports-plugin</artifactId>
       <configuration>
                   <argLine>-Xmx1024m</argLine>
                </configuration>
       <version>2.8.1</version>
 </plugin>

<!-- To publish JUnit test results -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
</plugin>

<!-- To publish PMD reports-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.6</version>
<configuration>
<includes>
<include>com/**/*.java</include>
</includes>  
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
<targetDirectory>${project.build.directory}</targetDirectory>
<rulesets>
<ruleset>${basedir}/pmd_ruleset.xml</ruleset>
</rulesets>
</configuration>
</plugin>

<!-- To publish findBugs report -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<debug>true</debug>
<effort>Min</effort>
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
<xmlOutputDirectory>${project.reporting.outputDirectory}</xmlOutputDirectory>
<failOnError>true</failOnError>
</configuration>
</plugin>

<!-- To publish Java code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
</plugin>

<!-- To publish JavaDocs -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
</plugin>
</plugins>
</reporting>

Once you will run 'mvn install site', a site will be created inside <project-directory>/target/site. You can open the 'index.html' generated by 'site' goal. you will be reports like (see screen below):



Sample pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abhinav.sample</groupId>
<artifactId>sample-code-analysis</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>sample-code-analysis</name>
<description>This project is to demonstrate code analysis, unit test and code coverage.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Append copyright info into java docs -->
<bottom>Copyright © 2015. Abhinav Kumar Mishra. All rights reserved.</bottom>
</properties>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<name>Abhinav Kumar Mishra</name>
<email>abhinavmishra14@gmail.com</email>
<organizationUrl>http://javaworld-abhinav.blogspot.com</organizationUrl>
</developer>
</developers>
<dependencies>
<!-- List of dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<issueManagement>
<url>https://github.com/abhinavmishra14/amazon-aws-s3-utils/issues</url>
<system>git</system>
</issueManagement>
<build>
<plugins>
<!-- Project is JDK 1.7 compliant, so source and target version should
be 1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy static resource files</id>
<goals>
<goal>resources</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<!-- JavaDocs plug-in -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Java sources plug-in-->  
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-source-plugin</artifactId>
 <version>2.2.1</version>
 <executions>
<execution>
 <id>attach-sources</id>
 <goals>
<goal>jar-no-fork</goal>
 </goals>
</execution>
 </executions>
</plugin>
<!-- Java code coverage, This plug-in will throw exception if unit test
cases are skipped -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<!-- <outputDirectory>${project.reporting.outputDirectory}</outputDirectory> -->
</configuration>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- PMD Integration, you can also run it directly using mvn pmd:pmd -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jxr</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.6</version>
<configuration>
<!-- Run PMD on the tests. Default: false -->
<includeTests>false</includeTests>

<!-- Link the violation line numbers to the source xref. Links will be created
automatically if the jxr plugin is being used. Default: true -->
<linkXRef>true</linkXRef>

<!-- Set the output format type, in addition to the HTML report. Must be
one of: "none", "csv", "xml", "txt" or the full class name of the PMD renderer
to use. See the net.sourceforge.pmd.renderers package javadoc for available
renderers. XML is required if the pmd:check goal is being used. Default: xml-->
<format>xml</format>

<!-- A list of files to include from checking. Can contain Ant-style wildcards
and double wildcards. Defaults to **\/*.java.-->
<includes>
<include>com/**/*.java</include>
</includes>

<!-- A list of files to exclude from checking. Can contain Ant-style wildcards
and double wildcards. We are not excluding anything here. -->
<!-- <excludes>
 <exclude></exclude>
</excludes> -->

<!-- The output directory for the final HTML report.  -->
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>

<!-- The output directory for the intermediate XML report. -->
<targetDirectory>${project.build.directory}</targetDirectory>

<rulesets>
<!-- Multiple rule set xmls can be included here. -->
<ruleset>${basedir}/pmd_ruleset.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!-- Creates a PMD report. -->
<goal>pmd</goal>

<!-- Fail the build if there were any PMD violations in the source code. Uncomment if needed -->
<!-- <goal>check</goal> -->

<!-- Creates a report for PMD's copy paste detector tool -->
<goal>cpd</goal>

<!-- Fail the build if there were any CPD violations in the source code. Uncomment if needed -->
<!-- <goal>cpd-check</goal> -->
</goals>
</execution>
</executions>
</plugin>
<!-- FindBugs Integration, you can also run it directly using mvn findbugs:findbugs -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<!-- FindBugs will show what is doing during analysis.  -->
<debug>true</debug>
<!--
Enables analysis which takes more memory but finds more bugs.
If you run out of memory, changes the value of the effort element
to 'min'. Min|Default|Max are possible values.
-->
<effort>Min</effort>
<!-- Reports all bugs (other values are High|Normal|Low|Exp|Ignore) -->
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
<!-- Optional directory to save findbugs xdoc xml report -->
<xmlOutputDirectory>${project.reporting.outputDirectory}</xmlOutputDirectory>

<!-- Indicates to analyze only given java packages, We are allowing everything here. -->
<!-- <onlyAnalyze>com.*</onlyAnalyze> -->
<failOnError>true</failOnError>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!-- This goal will check code and generate report -->
<goal>findbugs</goal>

<!-- Fail the build if there were any findBugs violations in the source code. Uncomment if needed -->
<!-- <goal>check</goal> -->
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

<!-- Configuring plugins for reporting purpose. Reports will be generated using mvn site command -->
<reporting>
   <plugins>
 <!-- Reporting plugin -->
 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.8.1</version>
</plugin>
 <!-- To publish JUnit test results -->
 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
 </plugin>
 <!-- To publish PMD reports-->
  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
  </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.6</version>
<configuration>
<includes>
<include>com/**/*.java</include>
</includes>  
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
<targetDirectory>${project.build.directory}</targetDirectory>
<rulesets>
<ruleset>${basedir}/pmd_ruleset.xml</ruleset>
</rulesets>
</configuration>
</plugin>
<!-- To publish findBugs report -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<debug>true</debug>
<effort>Min</effort>
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
<xmlOutputDirectory>${project.reporting.outputDirectory}</xmlOutputDirectory>
<failOnError>true</failOnError>
</configuration>
</plugin>
<!-- To publish Java code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
  <!--  <outputDirectory>${project.reporting.outputDirectory}</outputDirectory> -->
</configuration>
</plugin>
<!-- To publish JavaDocs -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
</plugin>
  </plugins>
</reporting>
</project>


You can also refer to my github project: