Maven plugin for checking coding conventions during build - java

we are a team of developers with very different experiences and coding styles. But as we are working on same projects it is necessary to have some common ground regarding code style.
Is there a maven plugin that can check for several coding rules? We already use checkstyle and spotbugs, but I'm interested if it's possible to include it in regular build.
I'd like to know if it's possible to make build fail (or at least give a severe warning) when certain coding rules are not fullfilled.
I thought about programming something by myself but would like to know if there is already a plugin that does what we want.
Thanks!

You can use maven-checkstyle-plugin
to verify coding styles and to set style do you want, and other configuration
example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

Not related to maven but you can use sonar.

Related

How to configure maven and intellij to include parameters compiler flag for both groovy and java

Hey I am writing this since I spent some time trying to configure Jackson's ObjectMapper to work without #JsonCreator and #JsonProperty annotations on my DTOs. The result I wanted to achieve is to be able to run Spock's tests (groovy) in both intellij and in console with maven.
Since this issue is resolved: https://youtrack.jetbrains.com/issue/IDEA-125737 intellij automatically picks up below maven configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<testCompilerArgument>-parameters</testCompilerArgument>
</configuration>
</plugin>
Also if you want to use Spock for testing in groovy you need following plugin configuration:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<configuration>
<parameters>true</parameters>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
plus additional information:
At the moment I was writing this there was only one version of groovy compiler that worked for me: 2.5.0-alpha-1
maven-compiler-plugin version I used was 3.7.0. Version 3.1 did not work at all. I did not try others.
I hope this saves someone some time :)

Process annotations with RetentionPolicy.SOURCE in a maven mojo

Is it possible to (in any way) process the annotations with RetentionPolicy.SOURCE (or at least RetentionPolicy.COMPILE) in a maven Mojo?
I'd like to generate an additional documentation based on my custom annotations with aforementioned retention policy – since the documentation is in no way needed during runtime, I wouldn't like these annotations to clutter the ClassLoader.
(something like swagger, but I need not to serve this documentation during runtime and swagger annotations are retained at runtime...)
The reason I don't want to use a simple javax.annotation.processing.AbstractProcessor is that I want to have the convenient configurability of a full-fledged maven plugin, with a lot of optional properties etc.
This looks like what you're after.
https://maven-annotation-plugin.googlecode.com/svn/docs/usage.html
I'm looking for the same thing, but this plugin isn't available at my workplace, so I can't really comment further. Seems to meet your use case though.
<build>
<plugins>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<!-- list of processors to use -->
<processor>org.bsc.apt.BeanInfoAnnotationProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
</plugin>
</build>

Why do my generated Javadocs look terrible?

I'm trying to generate the Javadoc HTML files for my project. I'm generating them via the Maven Javadoc plugin (maven-javadoc-plugin). I am using Maven 2.2.1. Everything generates such that all the proper information is there, but the HTML looks just awful. So bad that I don't want to publish it that way. Here is a screenshot:
(NOTE: Yes, I see the 'JavaScript is disabled on your browser' message. Even if I click the IE 8 warning about active content and enable it, it makes no difference)
There are all kinds of unnecessary line breaks, and the basic formatting is just crap. Am I missing something? I was expecting to see generated Javadocs that look similar to what I see in Eclipse if I hover over a class or method and see the popup Javadoc panel.
I've tried adding setting in my POM file, but I really don't know what I'm doing when it comes to configuring the Javadoc generator. Here's what I have at the moment (inside the <reporting> element):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<configuration>
<javadocExecutable>C:\Java\jdk1.7.0_05\bin\javadoc.exe</javadocExecutable>
<javadocVersion>1.7</javadocVersion>
<locale>en_US</locale>
<show>package</show>
<verbose />
</configuration>
</plugin>
Any suggestions?
UPDATE:
The solution provided by Paulius worked perfectly. I removed the section above from my <reporting> section, as it was totally unnecessary. I added the new <plugin> element as he specified below. My POM file now contains this new block:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Here is what the fixed output looks like:
Try to remove maven-javadoc-plugin from reporting section. If you are using Maven 3, the reporting section is deprecated and should be removed.
Try to add the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
to your maven plugins section and run it. I am using maven-javadoc-plugin like this and it generates normal javadocs.
Hope this helps.

Findbugs Maven plugin ignores threshold

I'm using the following in my project's pom.xml file. Running a findbugs:check goal still fails on all errors, even when no High priority errors are present. What am I doing wrong?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<id>failing-on-high</id>
<phase>process-test-resources</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<threshold>High</threshold>
<onlyAnalyze>com.example.-</onlyAnalyze>
</configuration>
</execution>
</executions>
Looking at the documentation and trying it out on a sample codebase, it looks like findbugs:check goal
Fail the build if there were any FindBugs violations in the source
code.
This does not seem to depend on the value of threshold.
Also look at this blog entry which seems to describe a related problem.

maven checkstyle unable to find where to insert the includes configuration

We have a maven project where I use a checkstyle plugin in the build process and we are very fond of checkstyle. Anyway recently we need to insert web services to our project. The web services classes are not compatible with checkstyle so we want to exclude them when building. So as a way of exclude we want to specify the folders to be included.
I have come across maven.checkstyle.includes and sourceDirectory of checkstyle plugin. But I could not be able to figure out how to use them in pom.xml.
Anyone have an idea?
Here is my checkstyle section of pom.xml
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<failsOnError>true</failsOnError>
<consoleOutput>true</consoleOutput>
<configLocation>sample_config.xml</configLocation>
<sourceDirectory>${maven.checkstyle.includes}</sourceDirectory>
</configuration>
</plugin>
...
</build>
<excludes>my/package/**/*</excludes>
Add this to your configuration section. These are ant style patterns and you can give the plugin multiple patterns by separating them with a comma.

Categories