maven-pmd-plugin uses only the bundled rulesets - maven-plugin

I am using the maven-pmd-plugin on my project and this is how I have configured it
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.6</version>
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>${targetJdk}</targetJdk>
<rulesets>
<ruleset>${maven.pmd.rulesetfiles}</ruleset>
</rulesets>
</configuration>
</plugin>
</plugins>
</reporting>
Here are the properties used in the above configuration
<properties>
<spring.version>3.0.6.RELEASE</spring.version>
<basedir>C:\Users\Q4\workspace\project</basedir>
<maven.pmd.rulesetfiles>${basedir}\pmdRuleset.xml</maven.pmd.rulesetfiles>
<targetJdk>1.5</targetJdk>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
The problem is when I run mvn pmd:check, it gives me 8 violations -- only from the basic, unusedcode and imports. It simply doesn't use all the rules that I have listed in the custom ruleset file. I have even tried using the logging-java.xml and strings.xml directly in the ruleset without using the custom ruleset file and it still doesn't work.
When i run mvn pmd:pmd, i get a BUILD SUCCESS but the errors still show up in my target folder. Why do I get a build success here?

I solved this by simply adding the plugins in the build section along with the ones in the reporting section.
Somehow it needed to be in the as well to be able to run all the rulesets. Earlier I was under the impression that we put plugins in the build only if we want to run them during the build and deploy phase.

Related

pmd maven plugin does not respect exclude-cpd.properties

I have maven project with multiple modules, the structure is like this:
/myProject
pom.xml
exclude-cpd.properties
-- module1
pom.xml
exclude-cpd.properties
-- module2
pom.xml
exclude-cpd.properties
in my parent pom.xml i added pmd plugin:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<rulesets>
<ruleset>pmd_ruleset.xml</ruleset>
</rulesets>
<minimumTokens>80</minimumTokens>
<includeTests>true</includeTests>
<excludeFromFailureFile>exclude-cpd.properties</excludeFromFailureFile>
<printFailingErrors>true</printFailingErrors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
and in exclude-cpd.properties i include all files that i want to exclude from cpd-check as comma separated strings.
but as I run mvn clean compile, it still scan those excluded files and complained about duplication. seems that excludeFromFailureFile doesn't work at all, I eventually had to use excludes to exclude files from pmd check and cpd check but that's not ideal. anyone knows how to get excludeFromFailureFile working?
working pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<rulesets>
<ruleset>pmd_ruleset.xml</ruleset>
</rulesets>
<minimumTokens>80</minimumTokens>
<includeTests>true</includeTests>
<excludes>
<exclude>**/XyzService*.java</exclude>
</excludes>
<printFailingErrors>true</printFailingErrors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
I think you are misunderstanding the flags.
The excludeFromFailureFile is literally that: do not fail the build if violations are reported on these files. The files are still analyzed, but if only those files and no other have violations, the build still passes (if other files have violations the build still fails). This is not even a PMD feature, but something the Maven plugin bakes in.
It's not an "exclude from analysis", to do that you have the excludes configuration as you use on your second example.
The other good idea is using #SuppressWarnings("CPD-START") and #SuppressWarnings("CPD-END") to ignore all java code within these two annotations by CPD, e.g.:
...
//enable suppression (start)
#SuppressWarnings("CPD-START")
public Object shouldBeIgnoredFromHere(String any) throws Exception {
// any code here will be ignored for the duplication detection
}
...
//disable suppression (end)
#SuppressWarnings("CPD-END)
public void shouldBeIgnoredToHere() {
}
...
Read suppression chapter: https://pmd.sourceforge.io/pmd-5.0.5/cpd-usage.html

queue.yaml not deployed when i use mvn appengine:deploy

I updated my pom.xml to use the new mvn appengine plugin
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<project>{project_id}</project>
<devserver.host>0.0.0.0</devserver.host>
<devserver.port>1984</devserver.port>
</configuration>
</plugin>
Now when I run mvn appengine:deploy it converts my queue.xml to queue.yaml in the staging directory. However this queue configuration is not deployed.
I have tried so many ways to deploy it to google cloud but nothing worked. This setup is for my cloud endpoints project setup. The documentations do not cover this.
This is the maven plugin code i added after trying your suggestion out .
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<project>{project_id}</project>
<devserver.host>0.0.0.0</devserver.host>
<devserver.port>1984</devserver.port>
</configuration>
</plugin>
I opened a similar issue on the project board
By default, only the app.yaml file is deployed (which represents the application).
If you want (in addition, or only) the queue.yaml, or even the cron or index, you need to specify those files inside the plugin configuration.
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.maven.plugin.version}</version>
<configuration>
<deployables>
<param>target/appengine-staging/app.yaml</param>
<param>target/appengine-staging/cron.yaml</param>
<param>target/appengine-staging/queue.yaml</param>
<param>target/appengine-staging/index.yaml</param>
</deployables>
</configuration>
</plugin>
Please remember that if you specificy certain files, the app.yaml files should be added as well. It is deployed by default only if the deployabels parameter is missing.
Playing with this parameter you can choose which files to deploy

How to set version when deploying gae java?

Since Intellij Idea IDE gae deployment plugin does not work, I have to use mvn appengine:update. It always deploy to version 1, ignoring version in appengine-web.xml.
How to set version with mvn appengine:update deployment?
Another way is, don't add anything on the app engine plugin as it is hard to changes each time the pom.xml better pass the version information from the command line, like this
mvn clean package appengine:deploy -Dapp.deploy.version=your-version-here
reference document here
You can set it via a Maven property:
<properties>
<appengine.appId>my-application-id</appengine.appId>
<appengine.version>my-application-version</appengine.version>
</properties>
PS: I'm also setting the applicationId here, you don't necessarily need that.
Add the following into the plugins section in the project pom.xml file:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<deploy.projectId>java</deploy.projectId>
<deploy.version>1</deploy.version>
</configuration>
</plugin>
Set the version in plugin property
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<version>2</version>
</configuration>
</plugin>

Maven default compile not picking up custom plugin goal

I have written a custom plugin, then I installed it. Then I modified the pom.xml of the project from which I want to use the custom plugin. When I invoke my plugin goal directly the plugin goal is executed successfully, but when I try to mvn compile my custom plugin goal is not executed. What might be the reason?
My plugin's 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.plugins.maven</groupId>
<artifactId>datanucleus-enhance-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Datanucleus-enhance Maven Plugin</name>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
My project using custom plugin I added following:
<!-- enhance JDO classes -->
<plugin>
<groupId>com.sukantu.plugins.maven</groupId>
<artifactId>datanucleus-enhance-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<configuration>
<jdoClassDirList>
<param>target/${project.artifactId}-${project.version}/WEB-INF/classes/</param>
</jdoClassDirList>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
I followed section Attaching the Mojo to the Build Lifecycle from maven guide site: https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
The following command successfully calls my plugin goal:
mvn com.xxx.plugins.maven:datanucleus-enhance-maven-plugin:enhance
The following command does NOT successfully call my plugin goal:
mvn compile
Thanks for any inputs!
I came across this link:
How do I link a plugin execution to a phase in maven without forcing me to specify plugin on command line
So I removed <pluginManagement> tags so <plugins> appear directly under '<build>'. Then I tried 'mvn compile' from command line and it successfully called my custom plugin goal!
But when I checked pom.xml in Eclipse I saw another error referenced here How to solve “Plugin execution not covered by lifecycle configuration” .
Since the command line is working correctly, I think this is m2e Eclipse plugin error and so I have disabled the marked by going to 'Eclipse' -> 'Window' -> 'Show View' -> 'Markers' -> right click that marker -> 'Delete'. Now Eclipse is not showing any error and command line is also working as expected. Hope this helps someone else.

Maven Cobertura plugin not generating coverage.xml

I am trying to generate a coverage.xml so that I can reference it in Cobertura plugin of Hudson, but the file is not being created.
I've added the following to my POM
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
After running mvn cobertura:cobertura, the HTML site is generated as expected at **\target\site\cobertura, but coverage.xml is nowhere to be found. What am I missing/misunderstanding?
I am running Maven 3.0.3
Add below lines to your application Goals:(configure section of the application in jenkins)
cobertura:cobertura -Dcobertura.report.format=xml
pom.xml changes:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
I put the plugin in the build section and it works:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</build>
The reporting section and its differences to the plugin section are described here. I don't know if this is a maven [3.0.4] or cobertura-plugin issue.
I'm still quite a novice with the connections between Maven Plugins and Hudson and it's plugins - so this isn't an intelligent answer by any means, but help on Google is very few and far between for this issue - so hopefully it helps someone in the future.
After spending a few more hours of tinkering with settings, I've found that the coverage.xml simply doesn't seem to be built locally.
This is the combination that got it working:
I had changed my version to 2.2 in my POM (I was getting resource
not found errors from Apache with 2.5.1)
Added cobertura:cobertura in my Hudson goal
Set the Cobertura coverage pattern to the
recommended **/target/site/cobertura/coverage.xml
My objective was to get Cobertura to run duing mvn test with no additional command line parameters. Here's the magic XML that did the trick for me, with both the HTML and XML being generated in /target/site/cobertura.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>cobertura</id>
<phase>test</phase>
<goals>
<goal>cobertura</goal>
</goals>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I had the same issue but it's resolved right now:
Just add -Dcobertura.report.format=xml after your maven command. It should work
I have the same issue using 2.6 of the plugin.
I found that when I specify both types, I only got html.
<formats>
<format>html</format>
<format>xml</format>
</formats>
But when I specify only xml, I get an xml report.
<formats>
<format>xml</format>
</formats>
This is probably a bug in the plugin.
Another user suggested creating two executions. I tried that with no success (meaning I got html, but not xml).
Update your POM file as
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
This worked out for me: Probable reason it contanis the latest version of cobertura-maven-plugin (2.7)
The are two ways to integrate Cobertura into Maven.
Put Cobertura into the build section of the pom file, then you have to execute mvn clean cobertura:cobertura to generate the reports. If you have XML and HTML configured, then you get both reports.
Put Cobertura into the reporting section of the pom file, then you have to execute mvn clean site to generate the reports. If you have XML and HTML configured, then you get both reports. Additionally you get a generated site (open target/site/index.html) with all reports integrated e.g. Coberture, Checkstyle, ...

Resources