Displaying custom test results in Jenkins Maven job - maven

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.

Related

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

Why isn't mvn resources:resources picking up buildNumber?

I've got a maven project that uses the buildnumber-maven-plugin. If I run mvn validate I see it's working:
[INFO] --- buildnumber-maven-plugin:1.3:create (default) # myproject ---
[INFO] Executing: /bin/sh -c cd /Users/rob/Workspace/myproject && git rev-parse --verify HEAD
[INFO] Storing buildNumber: 5d315d8d1a43c3289fbf114c379fa1a3d3787044 at timestamp: 1477059166424
But if I run mvn resources:resources the filtered file does not pick it up:
[INFO] --- maven-resources-plugin:2.6:resources (default-cli) # myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
The pom.xml has:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>version.txt</include>
</includes>
</resource>
version.txt has:
${buildNumber}
But after maven runs, no filtering:
> cat target/classes/version.txt
${buildNumber}
The build number config in pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>validate</phase>
<goals><goal>create</goal></goals>
</execution>
I don't know enough Maven. Shouldn't running the resources "goal" also get the buildNumber property?
There is a difference in the commands that you execute:
mvn validate executes the maven phase "valdate": meaning all phases that come before (in this case none)
mvn resources:resources is a shortcut for executing the goal "resources" on the resources plugin. Actually its a shortcut for executing: org.apache.maven.plugins:maven-resources-plugin:3.0.1:resources. These short names are resolved by maven and very typical for plugins in the Apache namespace.
As you can see on the maven life-cycle page the goal you may look for is: "mvn process-resources". That phase has a default plugin binding to "resources:resources" which will run the resource plugin. Since you execute a phase all phases before that will be run too, including the build number plugin.
The ":" indicates the difference for the maven command line.

Maven Install plugin: parameter file is missing or invalid

I have a local jar and I want to use it in my project. There are plenty ways to do it: just install manually into local repo, do it with script in parent pom, use system scope, use local repo declaration.
I decided to use Maven Install plugin to install jar into the repo. Here is a configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>jacob</groupId>
<artifactId>jacob</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${basedir}\lib\jacob.jar</file>
</configuration>
</execution>
</executions>
</plugin>
I tried to run it in many ways:
mvn initialize install:install-file
mvn initialize
mvn install:install-file
But all the time I have an exception:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file
(default-cli) on project MYPROJECTNAME: The parameters 'file' for goal
org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file are
missing or invalid ->
I just want to find out why it doesn't work? Looks like I am messed with phases and goals..
Finally I went another way: I placed it into my parent POM, but why it works with parent and doesn't work with child?
The best way to deal with a separated jar is to start using a repository manager and install this jar via the UI into the repository manager. From that point you can use as a usual dependency. Makes life easier.
If you like to install it as part of the build what you have tried that means you need to call maven like this:
mvn package
You should not mix up calling goals without life cylce like install:install-file with life-cycle parts as initialize.
That is the reason why you got the described error message, cause the command line part needs required parameters which you didn't gave on command line.
Combination like:
mvn install:install-file initialize
Make in Maven no sense (rarly). You have bound the maven-install-plugin to the life-clycle in your pom so you should simply call the life cylce:
mvn initalize
And you will get something like the following:
[INFO] ------------------------------------------------------------------------
[INFO] Building test 0.6-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default) # test ---
[INFO] Installing /home/mvntest/lib/jdom-1.0.jar to /home/.m2/repository/com/soebes/test/jacob/1.1/jacob-1.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.780s
[INFO] Finished at: Sat Oct 25 12:19:30 CEST 2014
[INFO] Final Memory: 6M/91M
[INFO] ------------------------------------------------------------------------
BUT: I have to say that installing an artifact like you did is bad practice. Best is to use a repository manager like Nexus, Artifactory or Archiva or if really don't like repository managers (I don't understand why, but this is a different story) you can use Stephen Connolly's non-maven-jar-maven-plugin
Apart from all the above you should use more up-to-date versions of maven-install-plugin which current version number is 2.5.2

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

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

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>

Resources