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.
Related
I'm new to Java scene and was attempting to create a maven plugin.
I started with the archetype 'maven-archetype-plugin' and it comes with a default testing scaffold with artifact id 'maven-plugin-testing-harness'.
One confusing thing I've observed is that in the pom of testing directory 'project-to-test' exists a configuration over output directory
<build>
<plugins>
<plugin>
<artifactId>my-plugin-artifact-id</artifactId>
<configuration>
<!-- Specify the MyMojo parameter -->
<outputDirectory>target/test-harness/project-to-test</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
This parameter configuration didn't seem to take effect at all. The actually output path was set to default ('./target/) regardless
What did I do wrong?
After running maven commands in the project-to-test directory, I've realized that the newly created plugin was resolved to package org.apache, which is completely wrong.
With this hint I've added label to the pom build section and it now behave as expected.
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.
Im facing the following problem. I have set up my checkstyle with the following configuration:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.plugin.version}</version>
<inherited/>
<configuration>
<configLocation>${basedir}/checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
This runs fine when I run mvn site. However, when I run checkstyle through mvn checkstyle:checkstyle in order to get the XML report much more efficiently, the checkstyle plugin fails back to use the default configuration. When I move the plugin to <build> the XML is generated properly, but now the checkstyle report is not included in the generated site anymore.
What is the (current) way of setting up report plugins as Checkstyle, while perserving the ability to run the plugin separately under the same configuration?
Is it really the preferred way to defined your plugins and configuration twice?
Okay, apparently you should add the plugin with configuration to both <build> and <reporting>.
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 have a Maven 2 project and I want to configure my Checkstyle report plugin so that only some of my classes are analysed. I have found the maven.checkstyle.excludes property, but despite passing this as a command line parameter (using -D=maven.checkstyle.excludes=...) I can't get it to work. I can't find anything on the Plugin documentation page. Ideally I want to be able to set this in the <configuration> section of my POM.
If, like me, you arrived here searching for a way to exclude generated sources from checkstyle, do this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<configuration>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
</configuration>
</plugin>
By default, the checkstyle:checkstyle goal of the checkstyle plugin uses ${project.compileSourceRoots}, which apparently includes generated source directories.
If you change it to ${project.build.sourceDirectory}, it will use only the source directory, not any generated source directories.
Note that while <sourceDirectory> is deprecated, the alternative, <sourceDirectories>, does not appear to work.
Edit: In comments, #Cyrusmith, #Ben, and #Olivier Callioux report that as of 2017, <sourceDirectories> works; I cannot confirm.
If this question is about Maven 2, then the property is excludes and takes a comma-separated list of Ant patterns. So either pass this on the command line:
-Dexcludes=**/generated/**/*
Or set it up in the plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<excludes>**/generated/**/*</excludes>
</configuration>
</plugin>
Another option would be to use a suppression filter.
For example you could use the SuppressionCommentFilter to suppress audit events between a comment containing CHECKSTYLE:OFF and a comment containing CHECKSTYLE:ON (then just add these comments to the classes or parts of the code you don't want to check).
Additionally, if you want to exclude multiple independent folders, you can add multiple independent paths comma separated like this
<excludes>org/log4j/*,com/acme/**/*,com/companyb/*</excludes>
The answers above didn't work for me as I'm running code generation in maven which also adds the target/generated as a source dir.
The following solution works:
You have to use an explicit checkstyle-suppressions.xml config file and activate it from your configuration:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
[...]
The suppressions file for excluding the target folder looks like this:
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress files="[/\\]target[/\\]" checks=".*" />
</suppressions>
I'm using maven 3 and excludes does not seem to work. This plugin is a shame. They do not seem to check their own style, hence the bugs.
What worked was the sourceDirectories trick:
<configuration>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
</sourceDirectories>
</configuration>