Output failed test details to stdout using maven surefire - java

When I run my build using maven 2
mvn clean install
my tests are run by surefire plug-in. In case test failed I get the following output:
Results :
Failed tests:
test1(com.my.MyClassTest)
Tests run: 3, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
Please refer to /home/user/myproject/mymodule/target/surefire-reports for the individual test results.
To get the details about the problem I have to go and check surefire reports folder. Doing this every time my tests fail becomes annoying. Is there any way I can get those details (assert message + exception + stack trace) right here on the stdout ?

I find there's way too much output produced on stdout to be useful. Try leaving the HTML report open in your browser. After running your tests just refresh the page. Have a look at target/surefire-reports/index.html.
To output test results to stdout rather than a file use the following command:
mvn test -Dsurefire.useFile=false
Or to configure in your pom.xml add the following to your plugins section.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<useFile>false</useFile>
</configuration>
</plugin>

It's possible you may be using an older version of Surefire. I have found that newer versions produce more useful output on the console.
If you only want to see failing tests or tests with errors and only see errors in the build, you can pass the -q argument to your Maven build command.

please check your version of jacoco plugin is compatible with jdk or not:
i was getting similar issue , test cases were passing but it was failing while building project. so upgraded version worked for me:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

try using bellow maven command
mvn clean install -DskipTests
-DskipTests compiles the tests, but skips running them

Related

Maven ignoring tests on build

Currently trying to test a project and for some reason, it won't run the tests when using mvn clean install. Strangely enough though, right-clicking on the project and do Run 'All Tests' on Intellij, runs all the tests well.
The folder is called test, with the tests having a Test added after the name of the class they are testing. I have this configuration in the POM file set up:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
with the java version being set as such: <java.version>11</java.version>
and the junit dependency being set like so:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<type>jar</type>
</dependency>
This will result in:
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) # ******* ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Usually I don't need to even add the maven-surefire-plugin under the plugins tag, although I did this to specify the version, maybe that might help but it's of no use.
Any idea what the issue might be?
UPDATE 1:
Added some configuration for the surefire plugin: <useModulePath>false</useModulePath>
Logs from mvn -X test: https://controlc.com/0e9faba9
The version 3.0.0-M6 has been deployed to the Maven Central repository.
This issue is fixed.
Pls check the POM and the configuration useModulePath=false as a workaround.
http://quabr.com:8182/66637732/maven-ignoring-tests-on-build
Now the workaround can be removed.
Yes we have a bug in surefire 3.0.0-M5 (when JDK 9+ and no JPMS is used) but we published the workaround on the Stackoverflow several times.
Pls check it out with a temporal workaround
mvn test -Dsurefire.useModulePath=false
The fix is already done and it will be released in the version 3.0.0-M6. There the workaround useModulePath=false would not be needed anymore.

build number maven plugin for Git repo

A project that was earlier on svn was recently moved to github after which i am facing this issue while creating a build. It fails to generate the build number with the below error
Failed to execute goal org.codehaus.mojo:buildnumber-maven-plugin:1.4:create (default) on project line-management: An error has occurred while checking scm status. Exception while executing SCM command. Error while executing command. Error while executing process. Cannot run program "svn" (in directory "D:\workspace\project_git\project_management"): CreateProcess error=2, The system cannot find the file specified -> [Help 1]
code of pom.xml
<scm>
<connection>scm:svn:http://some.svn.url/</connection>
</scm>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
<timestampFormat>{0,date,"yyyyMMddHHmmss"}</timestampFormat>
<timestampPropertyName>buildNumVar</timestampPropertyName>
<shortRevisionLength>10</shortRevisionLength>
</configuration>
</plugin>
Your scm tag still contains the the connection string to your old svn repository.
You should provide the url of the new git repository here, for example <connection>scm:git:https://github.com/your-git-repo.git</connection>. For the details of the connection string, please check here.
When you install the svn program (tortoise for example) you need to check the command line option. By default, that option is disabled. That worked for me.

Maven JAR Plugin 3.0.2 Error: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them

Maven JAR plugin (version 3.0.2) keeps throwing the following error, even for a single invocation of the jar goal:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar (default) on project test: You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
Here's a (minimal?) pom.xml which demonstrates the problem:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The invocation is just mvn package.
It doesn't seem to matter whether there are any classes/resources at all — the above error message appears anyway.
The problem also appears if two goals are specified (jar and test-jar).
The problem does NOT appear if no goals are specified. But this is not an option, since I really need both jar and test-jar.
According to the documentation, classifier only needs to be specified on multiple invocations of the same goal, and there's a reasonable default for the test-jar goal which I don't intend to change.
Also, the problem doesn't seem to appear on the 2.x line of the JAR plugin.
Did I miss something?
Could anybody please suggest what I am doing wrong?
P.S. The Maven version is 3.3.9.
The Jar Plugin is actually getting executed twice with the configuration:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
If you check the logs with such a configuration, you will have something like:
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) # test ---
[INFO] Building jar: ...\test\target\test-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default) # test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
meaning that the plugin was in fact executed twice. What happens, is that the Jar Plugin, in a project that has a packaging of jar has a default execution bound to the package phase. This default execution is the one mentioned in the logs with the ID of default-jar.
When you configured an <execution> in the plugin, you actually configured a new execution, where the jar goal of the plugin is to be invoked. Since the jar goal binds by default to the package phase, that execution is getting executed at that phase, after the default binding inherent to the jar packaging. And since the plugin ran already, it is failing because running it again would actually replace the main artifact already produced during the first run. This error was added in version 3.0.0 of the plugin in MJAR-198, because such a thing happening is very likely a mis-configuration which should be detected early.
As such, the fix is simple: don't have an execution that specifies the goal jar, and let the default one (coming from the jar packaging) do the work. The JAR will still be created, even without the explicit configuration of the jar goal, thanks to the default execution. If you want a test JAR as well, you will still need to configure the plugin to do that with:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
But note that the goal jar is not specified.
In my case, I have set the execution ID to default-jar, overriding the default execution. Then the error is gone.
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
If your logs shows something like:
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) # test --
[WARNING] JAR will be empty - no content was marked for inclusion!
Adding a single useless class in src/main/java seems to solve the issue see:
http://maven.40175.n5.nabble.com/quot-mvn-clean-verify-deploy-quot-causes-jar-plugin-to-execute-twice-td5877166.html
Since the above link might be broken as of 2021-09 you might want to try http://mail-archives.apache.org/mod_mbox/maven-users/201608.mbox/thread as an alternative
One reason to have this error may be that the package goal is mistakenly executed twice:
mvn ... package ... package
May happen when the command is built by imperfect scripts :)
A little late but you should add <phase>none</phase> to default-jar execution. this will skip it.
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
This will skip the default-jar but others are executed. Make sure you put <phase>package</phase> in the rest of the execution threads.

If I run a plugin from the command line at the root pom, what does it do when traversing modules?

I'm trying to use the plugin: http://gatling.io/docs/2.0.1/extensions/maven_plugin.html
I'm trying to run this command at the root pom:
mvn package gatling:execute
This multi module project only has this plugin defined in one child pom like so:
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<args>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
But when I run this command at the root, it gives this error:
[INFO] ------------------------------------------------------------------------
[INFO] Building root-pom 2.5.210-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- gatling-maven-plugin:2.1.7:execute (default-cli) # root-pom ---
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at io.gatling.mojo.MainWithArgsInFile.runMain(MainWithArgsInFile.java:50)
at io.gatling.mojo.MainWithArgsInFile.main(MainWithArgsInFile.java:33)
Caused by: java.lang.RuntimeException: Can't find the jar matching (.*scala-library.*\.jar)$
at io.gatling.compiler.ZincCompiler$$anonfun$jarMatching$1$2.apply(ZincCompiler.scala:88)
at io.gatling.compiler.ZincCompiler$$anonfun$jarMatching$1$2.apply(ZincCompiler.scala:88)
at scala.Option.getOrElse(Option.scala:120)
at io.gatling.compiler.ZincCompiler$.jarMatching$1(ZincCompiler.scala:88)
at io.gatling.compiler.ZincCompiler$.setupZincCompiler(ZincCompiler.scala:91)
at io.gatling.compiler.ZincCompiler$delayedInit$body.apply(ZincCompiler.scala:106)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at io.gatling.compiler.ZincCompiler$.main(ZincCompiler.scala:35)
at io.gatling.compiler.ZincCompiler.main(ZincCompiler.scala)
... 6 more
This is not a scala project, so this is definitely related to the plugin. Running mvn package without gatling:execute works fine.
I do not know enough about maven to be able to troubleshoot this problem because I can't really tell what maven is trying to do here. If it is trying to run gatling at the root, why? That plugin is not defined in the root pom anywhere. My question is:
When you run a plugin at the root of a multi-module project, how does it traverse my project, look for plugins and run them? I'm asking this question abstractly. You don't have to answer in terms of gatling. I just want to understand maven plugins better.
How do I troubleshoot this issue? I think I could add scala to the root pom, but since it would be the first dependency at that level, it seems like it's the wrong approach.
I could also cd into the one child pom that has this plugin and run the command there, but that means I'll only be running package on a piece of the project. Ideally I'd like to run package on the whole project before I run the plugin.
Running maven with a plugin goal like that will run that goal for all project modules, the same as mvn install will install each module. You don't have to define the plugin anywhere - if you specify it on the commandline, it is run.
You correctly bound the execution of that plugin to one or more lifecycle phases, in this case process-resources and process-test-resources. If you run mvn compile, the the process-resources phase will be executed and the add-sources and compile goals will be run for the gatling plugin. Similarly, running mvn test (which is also run when you mvn install) will run the testCompile goal of the gatling plugin.
What you'll want to do is to also bind the execute goal as is exemplified
on the page you linked. If you want everyting packaged first, bind it to the
integration test phase:
<execution>
<phase>integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
....
</configuration>
</execution>
Note that integration-test comes after package (and somewhere before install), so to run it, execute mvn integration-test or mvn install.

How to make SonarQube module analyze the project only once when sonar analysis is bound to maven lifecycle in a multi-module project?

What I am trying to achieve is integrate SonarQube analysis into the build process, so that whenever mvn clean install is run, the code is analyzed with SonarQube. We want to use it for local analysis and also for build on Jenkins. If new issues are found, than the build should fail (we want to use build breaker plugin for that). This way the developer would know that by his code his is going to introduce new issues, and will have to fix them for the build to work.
When I run mvn sonar:sonar, the analysis takes 30 seconds, which is OK.
However, the problem occurs when I am trying to bind sonar goal to maven build phases. I bind sonar to verify phase. The build takes 5 minutes now, which is too long. It should take about 1 minute. The build itself, without SonarQube analysis takes 30 seconds.
Note (may help to figure out what the problem is): the project on which the build is run has multiple modules in it, and I guess that is the problem. It looks like sonar:sonar goal is executed multiple times, once for each submodule, and the whole project is analyzed multiple times (not only the submodules). So, we have 4 submodules, and the report is generated 5 times during the build.
Instead, we want to analyze the whole project only once, not 5 times. It's also important for this 1 analysis to be run at the end of the build, after the cobertura reports are generated for all modules.
So, how do I integrate SonarQube analysis into the build, so that it analyzes my multi-module project only once, in the end, after cobertura reports are generated for all the submodules?
SonarQube plugin properties in parent pom:
<!-- Sonar plugin properties -->
<sonar.jdbc.url>jdbc:url</sonar.jdbc.url>
<sonar.analysis.mode>preview</sonar.analysis.mode>
<sonar.issuesReport.html.enable>true</sonar.issuesReport.html.enable>
<sonar.issuesReport.console.enable>true</sonar.issuesReport.console.enable>
<sonar.host.url>sonar.host:9000</sonar.host.url>
<sonar.language>java</sonar.language>
<sonar.buildbreaker.skip>false</sonar.buildbreaker.skip>
<sonar.qualitygate>Sonar%20way%20with%20Findbugs</sonar.qualitygate>
<sonar.preview.includePlugins>buildbreaker</sonar.preview.includePlugins>
<sonar.exclusions>file:**/target/**</sonar.exclusions>
<branch>development</branch>
Plugins configuration in the project pom:
<!-- Run cobertura analysis during package phase -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Run sonar analysis (preview mode) during verify phase. Cobertura reports need to be generated already -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
IMO, this is just a Maven configuration issue, you're missing the <inherited>false</inherited> element on the execution of sonar:sonar:
<!-- Run sonar analysis (preview mode) during verify phase. Cobertura reports need to be generated already -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
</plugin>

Categories