I am trying to implement a new XPath PMD rule based on the example from the book "Jenkins Continuous Integration Cookbook".
My pom file's relevant section:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<targetJdk>1.6</targetJdk>
<format>xml</format>
<rulesets>
<ruleset>password_ruleset.xml</ruleset>
</rulesets>
</configuration>
</plugin>
</plugins>
</reporting>
I have the file 'password_ruleset.xml' sitting in the root of my maven project and it looks as follows:
<?xml version="1.0"?>
<ruleset name="STUPID PASSWORDS ruleset"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
Lets find stupid password examples
</description>
<rule name="NO_PASSWORD"
message="If we see a PASSWORD we should flag"
class="net.sourceforge.pmd.rules.XPathRule">
<description>
If we see a PASSWORD we should flag
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId[#Image='PASSWORD']
]]>
While executing i got the following error:
Failure executing PMD: Couldn't find the class net.sourceforge.pmd.rules.XPathRule
Checking which libraries contains this class i realized it's 'pmd' itself. I tried to add the dependency to the dependencies section without luck.
Where and what should i change to overcome this?
Please see the whole setup in github:
https://github.com/dave00/pmdcustomrule
A friend of mine resolved my problem via github:
https://github.com/mnyeste/pmdcustomrule/commit/ad2f04e33d2a5a04ef95d059d64a258ebca5b7be
Summary:
PMD API change 4.3 -> 5.0 Class net.sourceforge.pmd.rules.XPathRule
has been renamed to net.sourceforge.pmd.lang.rule.XPathRule
Maven PMD plugin version 3.2 is using PMD 5.1.2
Anyone interested can now pull my example project from github to see this working.
Related
I have a running maven project with JUnit tests. The Maven Surefire Plugin drops xml files after the tests. These xml files include, besides properties and logprints, following information:
<testcase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="methodNameForTest"
classname="com.my.test.project.Testclass"
time="20.931">
<error type="java.lang.NullPointerException">java.lang.NullPointerException</error>
</testcase>
Can someone explain me the usual way how data gets written from JUnit into these brackets and how more data can be added to it?
File search with these words doesn't help btw. Can't find anything in my project.
Thank you in advance
You cannot add custom messages into the XML report generated by the maven-surefire-plugin. The closest you can get is by configuring this plugin to redirect your System.out.println() calls to a text file. This is configured in your pom.xml with the help of redirectTestOutputToFile configuration, as shown below:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
By default, this would create target/surefire-reports/<test-name>-output.txt. Any calls to System.out.println(...) from your tests would get redirected to this file.
There is lots of duplicates codes including:
Duplicates within file
Duplicates within package
Duplicates around multiple packages
Duplicates around separate Maven Modules
Is there any way to find duplicate codes using Netbeans 8+ or Netbeans Plugins or Maven Plugin with Netbeans or Standalone tools similar to Intellij?
I am unable to find documentation of same.
I have found PMD in Maven but unable to link with Netbeans 8.2 and We are only supposed to use Netbeans
You can find duplicated code for a NetBeans Maven project using the Apache Maven PMD Plugin. Simply add the following to your project's pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.7.1</version>
</plugin>
</plugins>
</reporting>
When you build your project PMD's copy paste detector (CPD) will be run, and write its results to file {project directory}\target\cpd.xml. If no duplication is found that file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<pmd-cpd>
</pmd-cpd>
However, if duplication is found the file looks similar to this:
<?xml version="1.0" encoding="UTF-8"?>
<pmd-cpd>
<duplication lines="162" tokens="608">
<file line="95" path="D:\NetBeansProjectsJava9\checkduplication\src\main\java\com\unthreading\checkduplication\ParseJavadoc.java"/>
<file line="258" path="D:\NetBeansProjectsJava9\checkduplication\src\main\java\com\unthreading\checkduplication\ParseJavadoc.java"/>
<codefragment>
<![CDATA[
...details of the duplicated code....
]]>
</codefragment>
</duplication>
</pmd-cpd>
After updating the POM as described above, a very simple way to verify that duplication detection is working is to copy/paste a large method within some class, and then give the pasted method another name so that the code still compiles. I tried that and got the following result in the Output window:
maven-pmd-plugin:2.7.1:cpd-check (default) # checkduplication ---
BUILD FAILURE
Total time: 2.689 s Finished at: 2018-02-15T17:06:23-05:00 Final
Memory: 20M/70M
Failed to execute goal
org.apache.maven.plugins:maven-pmd-plugin:2.7.1:cpd-check (default) on
project checkduplication: You have 1 CPD duplication. For more details
see:D:\NetBeansProjectsJava9\checkduplication\target\cpd.xml -> [Help
1]
A couple of final points:
Specifying <goal>cpd-check</goal> (as I did) causes the build to fail when duplication is detected, but alternative goals can be specified.
There are more recent versions of maven-pmd-plugin than 2.7.1, but that is the most recent version where copy/paste detection works out of the box. More recent versions may work after playing with the configuration, but I didn't try that.
I am reading an interesting tutorial here: http://www.avajava.com/tutorials/lessons/how-do-i-generate-pmd-and-cpd-reports-for-a-site.html?page=1
This tutorial shows how to use Maven to run the open-source static-analysis tool, PMD, and to see the generated output on a Maven created website. Maven can easily create websites with mvn site command, but this tutorial shows how to use PMD for more helpful metrics on the source code.
The instructions were followed to the best of my ability. Here is my pom.xml file that came from reading the tutorial:
<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.name.bookstore</groupId>
<artifactId>bookstore</artifactId>
<packaging>jar</packaging>
<version>1</version>
<name>bookstore</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<!-- start for deploying using webdav -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav</artifactId>
<version>1.0-beta-2</version>
</extension>
</extensions>
</build>
<distributionManagement>
<!-- start -location where site is deployed -->
<site>
<id>site.deployments</id>
<name>Site deployments</name>
<url>dav:localhost/${basedir}</url>
</site>
</distributionManagement>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
</plugin>
<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-javadoc-plugin</artifactId>
<version>2.10.1</version>
</plugin>
</plugins>
</reporting>
</project>
When I run the command: mvn clean site I get a website built by Maven with a bunch of different pages but none of them show anything in regards to PMD. What am I missing here? Why am I not seeing anything in regards to PMD in the generated website?
Also, when I run mvn pmd:pmd there is a successful build but I do not obtain any helpful PMD metrics. I even coded in some unused variables and methods in one of my Java source files as illustrated in the above linked tutorial and there is no helpful output.
The mvn pmd:pmd command does appear to create some files though. A couple are files of rules for the engine it looks like and the others are empty. Please see the screen-shots of this below:
Figure 1: Files created by pmd:pmd command
Figure 2: Empty pmd file - even though there are obvious errors in Java source file
Does anyone out there know what is up? Why is PMD not working with Maven for me?
Thanks for reading this.
Also, from what I have read around the Internet on PMD's website and Maven's website there should be some information in the "Project Reports" section. There is no data here though from PMD. Please see the below screen-shot.
Figure 3: No PMD Data found in Project Reports
Regards
UPDATE
When I change the PMD section of the pom.xml file to the below snippet I obtain some CPD results via PMD but still nothing from PMD on code bugs. I even coded in an NullPointerException and PMD said nothing even when issuing the mvn pmd:check command.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>1</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
</plugin>
In the snippet I changed the sourceEncoding tag to be utf-8 because everything I see in regards to this is utf-8. I also changed the minimumTokens value to 1 to try to get more output from this plug-in. I also put this snippet in the <build> section to try and get results but still nothing... :/
Thanks for studying this...
The maven-pmd-plugin by default skips nowadays empty reports (property skipEmptyReport). You'll need to set this to false, to get in your site always a PMD/CPD report:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
<configuration>
<skipEmptyReport>false</skipEmptyReport>
</configuration>
</plugin>
</plugins>
</reporting>
This applies for both PMD and CPD. I assume, this is your problem, as in Figure 2, you show, there are no PMD violations detected (pmd.xml file is empty).
The property minimumTokens configures CPD and defines how long a code snipped at a minimum must be to be declared as a duplicate. The lower the number, the more duplicates are detected, but the duplicates can also be much shorter and therefore maybe more often false positives.
Without further configuring maven-pmd-plugin it uses by default these three PMD rulesets: java-basic, java-imports, java-unusedcode. See also property rulesets. If you want to detect specific problems, then you'll need to enable these rules. See also How to make a ruleset.
I am attempting to learn maven on a small project of my own, using eclipse. I converted the existing project to the maven standard directory configuration, and it was building; now I'm trying to get maven to produce the jar for the application. The following is 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.rc </groupId>
<artifactId> SpaceCheck </artifactId>
<version> 0.9.1-SNAPSHOT </version>
<name> SpaceCheck </name>
<packaging> jar </packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId>maven-jar-plugin </artifactId>
<version> 2.3.2 </version>
<configuration>
<includes>**/src/*</includes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>spacecheck.SpaceCheck</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
I didn't use to have the 'includes' clause; as near as I can tell, that's what the example I got pointed to told me to do to fix the problem. It does not.
When I attempt to build, I get:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-jar-plugin:2.3.2:jar (default-jar) on
project SpaceCheck: Unable to parse configuration of mojo
org.apache.maven.plugins:maven-jar-plugin:2.3.2:jar for parameter
includes: Cannot assign configuration entry 'includes' with value
'*/src/' of type java.lang.String to property of type
java.lang.String[] -> [Help 1]
The "Help 1" link points me to the tutorial that I followed to get this far.
The problem I have with the tutorial is that it doesn't explain how things work -- it gives you an example and expects you to extract general workings from the example. There's nothing wrong with examples, but they rarely match exactly what one wants to do.
I'm sure many people can tell me what's wrong, and I would appreciate it. But it would be even better if they could ALSO tell me where this is explained, not just where there is an example that does something similar. The explanation, to be complete, would explain what element needs to be added, just where in the XML that goes, and what the various options are for the thing that goes there.
Instead of
<includes>**/src/*</includes>
try
<includes>
<include>**/src/*</include>
</includes>
And if you are learning Maven you definitely want to check out The Complete Reference.
Since you're asking for the documentation (and not the direct answer), have a look at http://maven.apache.org/guides/mini/guide-configuring-plugins.html
The problem is maven version
Following format for list/set is support in maven 3.3.9 and onward versions but in lower versions it not supported.
<includes>**/src/*</includes>
But in maven version less than 3.3.9, above is not supported
To fix make it backward compatible
<includes>
<include>**/src/*</include>
</includes>
PS: for more details check this link
I have Jenkins job. I run maven java-executor plugin as follows:
compile exec:java
here is the 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.proj.utills</groupId>
<artifactId>db-upgrade</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.proj.db.RunMe</mainClass>
<arguments>
<argument>
${TARGET_ENV}
</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
I would like that Jenkins job fail when I got exception in Java code. For now it success even in case of exception. How can I do it?
Thanks
You can use the log parser plugin
EDIT: Excerpt from the plugin's documentation
Job Configuration
Go to menu: Jenkins -> job name -> Configure
Go to section : Post-build Actions
Check the "Console output (build log) parsing" checkbox.
"Mark build Unstable on Warning" option: check to have parsed warnings mark the build 'unstable'.
"Mark build Failed on Error" option : check to have parsed errors mark the build 'failed'.
"Select Parsing Rules" : select the set of rules to use for parsing the logs of this job's builds
(Notice that this list is derived from the global configuration )
I use the Log Parser Plugin for cases like this. just configure what you are looking for. In your case it might be as simple as looking for 'Exception'. I used to parse the output of shell scripts, to find sql errors (but still ignoring some that I don't care about).
You may try the Jenkins Text Finder plugin, downgrade the BUILD status from successful to UNSTABLE/FAIL when the regular expression setting is met. More detail refer to the link above.
I got exactly the same problem and did not like this parsing log solution.
For me a -B (to enable the batch mode) did the work nicely
build:
- mvn -B deploy