Sonar Maven Plugin: How do I exclude test source directories? - sonarqube

I have a Maven project with Java sources and Scala test sources. I generate code coverage using Jacoco during the verify stage. When I try to run the sonar goal either during the verify phase by adding an execution, or by running mvn verify sonar:sonar, I end up with the test directory being added twice by Sonar:
[INFO] [11:15:34.756] Test directories:
[INFO] [11:15:34.756] /Users/xxx/Documents/workspace/misc/xxx/src/test/scala
[INFO] [11:15:34.756] /Users/xxx/Documents/workspace/misc/xxx/src/test/scala/../scala
which results in the analysis failing with the following error:
Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.1:sonar (default-cli) on project kv-mapper: Can not execute SonarQube analysis: Unable to read and import the source file : '/Users/xxxx/Documents/workspace/misc/xxx/src/test/scala/../scala/xxx/xxxxx/xxx/xxx/xxxxx/xxxxxx.java' with the charset : 'UTF-8'. Duplicate source for resource
My pom.xml (for Sonar) looks like this.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.plugin.version}</version>
<!-- no default executions -->
<configuration>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.language>java</sonar.language>
<sonar.jacoco.itReportPath>
${basedir}/target/jacoco.exec
</sonar.jacoco.itReportPath>
<sonar.exclusions>
**/test/*
</sonar.exclusions>
</configuration>
</plugin>
How do I configure Sonar to either:
exclude test/scala directory entirely?
or
remove the duplicate directory?

sonar.test.exclusions should be used instead of sonar.exclusions
<sonar.test.exclusions>
**/test/*
</sonar.test.exclusions>

Either add a step to remove the folder before running the SonarQube analysis.
Or set exclusions on test files. See http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-IgnoreFiles

Related

Add Confguration in pom.xml at runtime in dockerfile

I am trying to build docker image.
Steps in dockerfile
1.pull code from gitlab
2.Maven Build code from gitlab**
However maven build is failing, when i am building docker image and reason is surefile couldn't fork
Below is printed in logs
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test
(default-test) on project DFDMWeb: Execution default-test of goal
org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: The
forked VM terminated without properly saying goodbye. VM crash or
System.exit called?
However I know the workaround for above i.e to add below line in pom.xml in configuration for surefire plugin
false
pom.xml contents for surefire plugin is like below after that
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
I have tested the above workaround by manually copying source code in aws EC2 instance and modifying the pom.xml file with useSystemClassLoader as false in pom.xml for surefire plugin
After doing so when i ran below command
docker build -t test -f Dockerfile .
Image was created successfully.
But now the problem is i am cloning repo from gitlab and I don't know how can I add false in surefire plugin configuration in pom.xml at run time using dockerfile ?
Anyone having any leads would be helpful.
Thanks in advance.
Try to set the useSystemClassLoader parameter with mvn command option -Dsurefire.useSystemClassLoader=false i.e. change your dockerfile to run mvn command like this mvn install -Dsurefire.useSystemClassLoader=false
The option is mentioned as a "User Property" in surfire plugin's docs
https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#useSystemClassLoader

Exclude log traces while TEST execution of maven surefire plugin

My maven project executes maven-surefire-plugin v2.22.0 TEST while building the application. By default the log level of surefire execution is INFO, and this plugin uses [org.apache.logging.slf4j.Log4jLoggerFactory].
I don't want my build process to log these traces while executing : maven-surefire-plugin:2.22.0:test (default-test)
Please could anyone help how to skip logging in this plugin execution?
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.22.0</version>
I have tried exclusions and verbose=false, but no luck :(
Put alog4j.properties under src/test/resources with the following content:
log4j.rootLogger=OFF

Displaying custom test results in Jenkins Maven job

I just added some Python unit tests to an existing Maven POM but I can't seem to get Jenkins to report the results of the tests when it runs the build.
I'm running nose tests from Maven with the exec-maven-plugin during the "test" phase. The tests run successfully from the Jenkins job and generate an xUnit compatible test report to target/surefire-reports/TEST-nosetests.xml, but Jenkins doesn't pick up on the results.
Interestingly, Maven also reports no tests run before executing the test suite:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- exec-maven-plugin:1.1.1:exec (nosetests) # server ---
[INFO] ................
[INFO] ----------------------------------------------------------------------
[INFO] Ran 16 tests in 194.799s
[INFO]
[INFO] OK
[INFO] Registering compile source root /Volumes/Data/workspace/myProject/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
So is this a problem with Jenkins not seeing the results, or with Maven not treating my test suite as actual tests?
I worked through this problem by using a "free-style software project" in Jenkins rather than the "maven2/3" project.
Under Build, choose Add build step and configure the project to Invoke top-level Maven targets. I'm using the test target.
Finally, under Post-build Actions choose Add post-build action of Publish JUnit test result report and point it at the xUnit output from your tests. This option is not available for Maven 2/3 jobs for some reason.
To build upon the answer by bpanulla, if you have tests in more than one sub-directory of your project, you can use a wildcard in the "Test report XMLs" field, such as: **/target/surefire-reports/*.xml
If you have a more modular Jenkins setup using a free project will not correctly build submodules. If the maven-plugin that generates the reports execution id is e2eTests, then add the following snippet to your pom.xml and the jenkins maven plugin will take care of the rest!
<properties>
    <jenkins.e2eTests.reportsDirectory>target/protractor-reports</jenkins.e2eTests.reportsDirectory>
</properties>
See https://wiki.jenkins-ci.org/display/JENKINS/Building+a+maven2+project
Try after adding the maven compiler plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>ISO-8859-1</encoding>
<source>1.8</source>
<target>1.8</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
As you can see in the Tests run output that Maven didn't recognized the tests as tests. Furthermore to see the results in Jenkins you need to check if nope can create junit compatible output result files (which i assume) which can be read by jenkins and of course displayed.

In PlayN, how do you exclude a directory from being compiled by Maven?

I want to build the html version of my game from the command line using maven. However, when I run the package command for the core folder:
mvn clean package -pl core,html
I get the following errors because of some unit tests in my source path:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/klenwell/projects/mygame/playn/mygame/core/src/main/java/mygame/playn/tests/unit/UserDataTest.java:[3,23] package org.junit does not exist
[ERROR] /home/klenwell/projects/mygame/playn/mygame/core/src/main/java/mygame/playn/tests/unit/UserDataTest.java:[7,16] package org.junit does not exist
...
How can I exclude the directory with these test files from being included in the compilation?
It is not a good idea to mix source and test classes. As per maven convention, you should move the tests from src/main/java to src/test/java.
You should add the dependency for junit so that the tests can be compiled.
You can choose to skip the tests (if they are broken) by using the -DskipTests or similar while running maven.
Adding the following block to the plugins section of my core pom.xml file excluded tests from compilation and allowed the build to succeed:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*Test*.java</exclude>
</excludes>
</configuration>
</plugin>

Netbeans reloads no class when "Apply Code Changes" in a maven project with remote jpda debugging

So I have a maven project which produces a jar package containing some ant tasks.
When I run my ant build script somewhere else with jpda open, and debug the tasks, say MyTask with NetBeans, the Apple Code Changes button doesn't work. Here is the output of the netbeans console:
cd /trunks/tasks; JAVA_HOME=/opt/jdk /opt/netbeans-7.0/java/maven/bin/mvn -Djpda.stopclass=com.abc.ant.MyTask compile
Scanning for projects...
------------------------------------------------------------------------
Building tasks 1.0-SNAPSHOT
------------------------------------------------------------------------
[resources:resources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 1 resource to com/abc/ant
[compiler:compile]
Compiling 1 source file to /trunks/tasks/build/classes
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.548s
Finished at: Fri Mar 09 17:45:24 CST 2012
Final Memory: 11M/149M
------------------------------------------------------------------------
NetBeans: classes to reload: []
NetBeans: No class to reload
So Netbeans does successfully tell Maven what class needs to be compiled. However, NetBeans won't reload the compiled class. Is it because my ant process is using the jar package produced by the Maven project, or because of other reasons?
Note: I have some custom configurations, like where to output the compiled classes, and where to put the jar package. Could that be a reason?
Update 2:
OK I found the reason by myself.
It's because I added the following line under <build> in the pom.xml:
<directory>${my.custom.work.dir}/build</directory>
So maven will output the compiled class files to this directory, rather than the default ${basedir}/target. However, Netbeans seems to be too stupid to recognize that -- it just tries the default directory.
Now the question could be much easier: is there any way to make the IDE recognize that by adding configuration in the pom?
In [your_home_path]/.netbeans/7.0/maven/conf you will find a setting.xml file.
Here you can set ${my.custom.work.dir} in <profiles> tag
You can find examples here (in section Properties)
Edit :
It works for me with this kind of POM (in Netbeans 7.0.1) :
<build>
<plugins>
<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>
<directory>${my.custom.work.dir}/build</directory>
</build>
<properties>
<my.custom.work.dir>/home/alain/Bureau</my.custom.work.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Resources