How to included bundled rulesets in maven pmd plugin - maven

I have a custom ruleset definition that refers to a bunch of rules from bundled rulesets. However, I have problems with resolving these bundled rulesets in the maven pmd plugin. Here is a related question with a quick'n'dirty solution for this problem. However, I was wondering whether there might be a clean solution for this problem (because I think that this is a rather common usecase, or?).
In the following I'm describing the attempts that I tried so far:
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven-pmd-plugin.version}</version>
<configuration>
<linkXref>true</linkXref>
<rulesets>
<ruleset>my_pmd_ruleset.xml</ruleset>
</rulesets>
<failsOnError>false</failsOnError>
<source>${java.source.version}</source>
<target>${java.source.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
I defined the maven pmd plugin (version 3.0.1) with my custom ruleset (and Java 1.6) in build/plugin management section of my parent pom as well as in the reporting section. I got many of the following warnings:
[WARNING] Failure executing PMD: Unable to find referenced rule UnusedModifier;
perhaps the rule name is mispelled?
I tried to refer to all utilised bundled rulesets:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven-pmd-plugin.version}</version>
<configuration>
<linkXref>true</linkXref>
<rulesets>
<ruleset>rulesets/java/coupling.xml</ruleset>
<ruleset>rulesets/java/design.xml</ruleset>
<ruleset>rulesets/java/controversial.xml</ruleset>
<ruleset>rulesets/java/codesize.xml</ruleset>
<ruleset>rulesets/java/clone.xml</ruleset>
<ruleset>rulesets/java/braces.xml</ruleset>
<ruleset>rulesets/java/basic.xml</ruleset>
<ruleset>rulesets/java/finalizers.xml</ruleset>
<ruleset>rulesets/java/imports.xml</ruleset>
<ruleset>rulesets/java/unit.xml</ruleset>
<ruleset>rulesets/java/logging-jakarta-commons.xml</ruleset>
<ruleset>rulesets/java/logging-java.xml</ruleset>
<ruleset>rulesets/java/migrating.xml</ruleset>
<ruleset>rulesets/java/naming.xml</ruleset>
<ruleset>rulesets/java/optimizations.xml</ruleset>
<ruleset>rulesets/java/strictexception.xml</ruleset>
<ruleset>rulesets/java/strings.xml</ruleset>
<ruleset>rulesets/java/sunsecure.xml</ruleset>
<ruleset>rulesets/java/typeresolution.xml</ruleset>
<ruleset>rulesets/java/unusedcode.xml</ruleset>
<ruleset>my_pmd_ruleset.xml</ruleset>
</rulesets>
<failsOnError>false</failsOnError>
<source>${java.source.version}</source>
<target>${java.source.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
There I'm getting always the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.0.1:pmd (pmd) on project my_project: An error has occurred in PMD Report report generation. Could not find resource 'rulesets/java/unit.xml'
The maven pmd plugin depends on pmd, so the bundled rulesets should be part of the classpath, or? The Maven pmd plugin manual says that bundled rulesets should be defined with a relative path and custom rulesets with an absolute path. However, I think that it can also read my custom ruleset via a relative path, because it is part of the classpath.
I also tried other thing, e.g., explicit additional dependency definition of pmd in the maven pmd plugin. However, without any success so far :\

Related

Maven Findbugs plugin not executing with mvn site command

I'm trying to get Findbugs working with an existing/mature Maven project.
I added the following to the <plugins> tag in my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
I then see that the Findbugs plugin runs when you run mvn site. Since the build invocation for my project is aleady:
mvn clean jacoco:prepare-agent test site jxr:jxr -Dkey1=123 -Dkey2=456 -Denvironment=DEV
...I just run it like I normally do. The build succeeds and I go to my normal site summary in my browser:
No where from here can I find any "Findbugs" reports or anything that mentions Findbugs at all. When I do a grep for Findbugs (grep -rl "findbugs" .) I do see that many of my ./target/surefire-reports/TEST-*.xml files have the term "findbugs" mentioned in them.
Worth mentioning that I do not see a target/site/findbugs.html file after the successful build...
Any ideas as to where I can find HTML Findbugs output under my Site summary (or anywhere else)? Looking for a nice HTML report showing which (if any) Findbugs checks failed.
You should add the FindBugs plugin in the <plugins> section of the <reporting> section of your pom.xml:
<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
</plugin>
</plugins>
</reporting>
...
</project>
The question just states <plugins>, not sure if that could be the issue.

when to use <build> plugin in maven pom.xml?

In our project, we have configured jetty inside build plugin in pom, i want to understand configuration settings in pom/ what and all we can configure in pom.
what is <build><plugin> section in pom, when to use.
difficult to understand from tutorials because lot of different examples which is making confuse.
Please can somebody explain for the above in detail?
Plugins defined in your buildsection plugins tag will be executed during the build of your project.
There are many plugins that do something with your build.
For example the maven-compiler-plugin which allows you to set the Java version for your project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
a list of maven build plugins supported by maven itself

Maven Compiler Plugin

I know Maven compiler plugin by DEFAULT is bind to :
compile
test compile
life cycles, in general without specifying addition configuration, we don't have to
explicitly define it in our POM, but I still seen experienced developer putting things like
this in their POM, e.g
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
I wander what's the point? and why is he doing this?
For the shade plugin, he is probably using POM inheritance. Look in the parent POM hierarchy for a pluginManagement section, there is probably shade plugin configuration there that he is pulling into this module.
For the compiler plugin, I do not know. You are correct, for jar/war/ear/ejb projects Maven will pull in the compiler config automatically, even if he has defined specific configuration in a parent POM's pluginManagement section.
If they put such things in their pom's they don't understand Maven. You should define the version of the plugins your are using in your build. This is done by:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin<artifactId>
<version>3.1</version>
<configuration>
<target>1.6</target>
<source>1.6</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin<artifactId>
<version>2.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>
This should be usually located into a pom file known under Company POM which defines versions of plugins for projects within a company.
Furthermore based on the life-cycle definition in Maven the Maven Super POM which contains the default bindings there you could see that particular plugins versions are defined.This means if you upgrade your Maven version you start using different plugin versions which is fact not the best related to build reproducibility. So the best practice is to define all used plugins like here as an example.. Based on the definition you shouldn't need to mention anything in your build-tag area if you have a defined packaging type (This is one of those Convention Over Configuration paradigm hints).

How to configure FindBugs to run only on one project in a multi-module maven project

I use the findbugs-maven-plugin to check for bugs with maven. My maven project is a multi-module project that roughly looks as follows:
java-module
pom.xml
src/ ...
pom.xml
scala-module
pom.xml
src/ ...
I use Jenkins to build and test the project, and Jenkins runs goal findbugs:findbugs in the top-most directory. Since FindBugs reports many spurious warnings for code that is generated by the Scala compiler, I would like to tell FindBugs not to analyze the code in scala-module. However, when I run findbugs:findbugs in the top-most directory, it always analyzes all classes in java-module and scala-module. How can I tell maven to ignore scala-module as a whole? I know about FindBugs exclude filters but I would to have a configuration option for FindBugs that tells it to simply not analyze the code in a certain submodule.
FindBugs is configured in pom.xml in subdirectory java-module as follows:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${version.plugin.codehaus.findbugs}</version>
<configuration>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
</plugins>
</reporting>
Despite the configuration being done only for the java-module, FindBugs will always also analyze scala-module.
Add a configuration the scala-module pom.xml that explicitly instructs findbugs to skip the module, i.e.
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</reporting>
Note that Maven often requires you to repeat boilerplate XML for cases like this.
Noahlz's answer did not work for me, but adding the following snippet to the sub-module's POM.xml did the trick.
<properties>
<findbugs.skip>true</findbugs.skip>
</properties>

Running customized PMD ruleset in Maven

I would like to run my customized pmd rule set which is in my local disk D:\rulesets\java. I am using maven to build my application. I put the below entry in maven POM.xml
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<rulesets>
<ruleset>D:\rulesets\java\basic.xml</ruleset>
<ruleset>D:\rulesets\java\braces.xml</ruleset>
<ruleset>D:\rulesets\java\clone.xml</ruleset>
<ruleset>D:\rulesets\java\codesize.xml</ruleset>
</rulesets>
</configuration>
</plugin>
</plugins>
When I run my PMD report (mvn pmd:pmd) it's not taking my customized pmd ruleset defined in pom file. How to solve this issue ?
Thanks in advance.

Resources