-
Notifications
You must be signed in to change notification settings - Fork 1
Home
- automatization of controlling java source best practises and convetions ( checkstyle, PMD )
- automatization of controlling test coverage ( Jacoco )
- controlling version of java in source and target
on verify maven phase
Maven ends build with FAILURE and write all violations to stdout :
[INFO] Starting audit...
/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java:20:13: error: Must have at least one statement.
/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java:20:14: error: '{' is not followed by whitespace.
/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java:20:14: error: '}' is not preceded with whitespace.
/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java:20:21: error: 'catch' is not followed by whitespace.
/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java:20:37: error: '{' is not followed by whitespace.
/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java:20:37: error: '}' is not preceded with whitespace.
Audit done.
Checkstyle can generate also reports but in my opinion this is unnecessary because checkstyle already end the build if there are some violations and every violations puts write on stdout.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>checkstyle-validate</id>
<phase>validate</phase><!-- when we need to run checkstyle::check -->
<configuration>
<configLocation>src/main/resources/checkstyle.xml</configLocation>
<encoding>${project.build.sourceEncoding}</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<goals>
<goal>check</goal><!-- what from checkstyle is used // checkstyle::check -->
</goals>
</execution>
</executions>
</plugin>
on verify maven phase
Maven ends build with FAILURE and write all violations to generated file {your-project-path}/target/pmd.xml:
<?xml version="1.0" encoding="UTF-8"?>
<pmd version="5.3.5" timestamp="2015-12-27T13:04:35.894">
<file name="/home/nue/nue/dev/web/2016/MavenProjectStarter/src/main/java/maven/project/starter/App.java">
<violation beginline="20" endline="20" begincolumn="16" endcolumn="37" rule="EmptyCatchBlock" ruleset="Empty Code" package="maven.project.starter" class="App" method="main" externalInfoUrl="https://pmd.github.io/pmd-5.3.5/pmd-java/rules/java/empty.html#EmptyCatchBlock" priority="3">
Avoid empty catch blocks
</violation>
<violation beginline="20" endline="20" begincolumn="13" endcolumn="14" rule="EmptyTryBlock" ruleset="Empty Code" package="maven.project.starter" class="App" method="main" externalInfoUrl="https://pmd.github.io/pmd-5.3.5/pmd-java/rules/java/empty.html#EmptyTryBlock" priority="3">
Avoid empty try blocks
</violation>
</file>
</pmd>
Ruleset in maven config pom.xml is optional. You can use it like a customization of PMD.
PMD can generate also reports but in my opinion this is unnecessary because PMD already end the build if there are some violations and every violations write to xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.6</version>
<executions>
<execution>
<id>pmd-validate</id>
<phase>validate</phase><!-- when we need to run .... -->
<configuration>
<linkXRef>false</linkXRef>
<rulesets>
<ruleset>src/main/resources/pmd.xml</ruleset>
</rulesets>
</configuration>
<goals>
<goal>check</goal><!-- what from pmd is used // .... -->
<goal>pmd</goal><!-- what from pmd is used // .... -->
</goals>
</execution>
</executions>
</plugin>
There are more code coverage tools. FOr example :
- Emma : is no longer supported. http://stackoverflow.com/questions/9531608/which-emma-maven-plugin-should-i-be-using
- Cobertura : looks fine, i need define percentage of coverage. cobertura did it
- Clover : under license ( Atlasian product )
So I decided to use Jacoco because is under development and have more functions and can define percentage and fail if they dont meet.
For first this is code coverage tool. It gets to test phase. Plugin have 3 executions.
- default-prepare-agent : prepare agent that is used for analysis (without it its impossible do anything).
- jacoco-default-report : report generating on site phase.
- jacoco-check : main test analysis on test phase. Test code coverage is here on 0.55 = 55 percent.
- ...
With Jacoco you need to run mvn test and if you would like to have also reports than run mvn test site (Create for it custom maven command.).
<!-- JACoCo code coverage plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<!-- preparing agent that is used for analysis -->
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- creates Jacoco reports on site phase
is better keep generating reports on site phase and
for developer better life create please custom mvn command with site in it.
-->
<execution>
<id>jacoco-default-report</id>
<phase>site</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- running analysis on test phase -->
<execution>
<id>jacoco-check</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.55</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
jacoco-default-report generates for you something like this :
For this is used maven-compiler-plugin. I controll target and source java version with it.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
On verify maven phase.
After verify it create 2 files. You can find in it errors.
- {your-project-path}/target/findbugsXml.xml
- {your-project-path}/target/findbugs.xml
After this maven tell you if you have errors and end build with FAILURE.
[INFO]
[INFO] >>> findbugs-maven-plugin:3.0.3:check (findbugs-verify) > :findbugs @ maven.project.starter >>>
[INFO]
[INFO] --- findbugs-maven-plugin:3.0.3:findbugs (findbugs) @ maven.project.starter ---
[INFO] Fork Value is true
[java] Warnings generated: 1
[INFO] Done FindBugs Analysis....
[INFO]
[INFO] <<< findbugs-maven-plugin:3.0.3:check (findbugs-verify) < :findbugs @ maven.project.starter <<<
[INFO]
[INFO] --- findbugs-maven-plugin:3.0.3:check (findbugs-verify) @ maven.project.starter ---
[INFO] BugInstance size is 1
[INFO] Error size is 0
[INFO] Total bugs: 1
[INFO] Return value of String.isEmpty() ignored in maven.project.starter.App.main(String[]) [maven.project.starter.App, maven.project.starter.App, maven.project.starter.App, maven.project.starter.App, maven.project.starter.App] At App.java:[line 21]Another occurrence at App.java:[line 22]Another occurrence at App.java:[line 23]Another occurrence at App.java:[line 24]Another occurrence at App.java:[line 25] RV_RETURN_VALUE_IGNORED
[INFO]
To see bug detail using the Findbugs GUI, use the following command "mvn findbugs:gui"
Than you can run mvn findbugs:gui or see in generated files.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<effort>Max</effort>
<threshold>Max</threshold>
<xmlOutput>true</xmlOutput>
</configuration>
<executions>
<execution>
<id>findbugs-verify</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>