This question already has answers here:
Making Maven run all tests, even when some fail
(5 answers)
Closed 3 years ago.
Actuality when i run tests they fails but i need to run them to get some .class files which are very important for my jar.
By default when test results fails , the jar is not build , could i add a setting in pom.xml which ignore that, so I can build the jar ignoring results from tests ?
I read something about "Maven Surefire Plugin" but I don't know how to use it...
Please refer to surefire:test for details, but the most useful properties are:
-Dmaven.test.failure.ignore=true (or -DtestFailureIgnore=true) - will ignore any failures occurred during test execution
-Dmaven.test.error.ignore=true ( deprecated ) - will ignore any errors occurred during test execution
-DskipTests - would compile the test classes but skip test execution entirely
-Dmaven.test.skip=true - would not even compile the tests
I believe that in your case where you want to compile test classes but not fail the build due to any tests errors and still create the jar.
You should use the first option to ignore any test failures which you can still review once the build has finished.
mvn -Dmaven.test.skip=true package skips the surefire test mojo.
to ignore test failures and keep maven from stopping you can add this to the section of the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
The solution is:
mvn -fn clean install
execute mvn --help for advanced options
Here's the excerpt for -fn
-fn,--fail-never NEVER fail the build, regardless
of project result
<properties>
<maven.test.skip>true</maven.test.skip>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>
http://jira.codehaus.org/browse/SUREFIRE-319
Or from command line
http://maven.apache.org/maven-1.x/plugins/test/properties.html
maven.test.error.ignore Yes Set
this to true to ignore errors during
testing. Its use is NOT RECOMMENDED,
but quite convenient on occasion
Use -DskipTests=true instead of -Dmaven.test.skip=true in order to skip tests but compile them.
Using -Dmaven.test.failure.ignore=true will also work but is not very nice.
Use the maven option -Dmaven.test.skip=true
E.g.
mvn package -Dmaven.test.skip=true
Related
When release:clean release:prepare release:perform is executed , the tests are executed twice
Once at release:prepare , stage run-preparation-goals , task clean verify
And another time on release:perform ,stage run-perform-goals , task deploy
Is there any configuration option to make tests run only on first one and not being executed on perform?
BR
Yulian Oifa
That's because 2 full builds are run as part of the commands you issue.
The release:prepare section performs lots of checks on the code & repository, and does a full build to ensure all tests pass.
The release:perform section tags the repo, then pulls that tag. It performs a build based on that tag and the releases that build to your artefact store of choice (Nexus/Artifactory/wherever).
These steps are designed this way to ensure you don't pollute your repo with a tag on code that doesn't build, and tests are an integral part of your build. Once maven is happy your code is good to go, it creates the tag, and pulls it so it knows for sure that the code being released is the code referenced by the tag, and nothing else. Building that tag for release requires a full build, including the tests.
You can skip the tests on one or other of the steps, or on both, but you're missing the point of tests, and of the release plugin, if you do that.
You can override the goals parameter and add -DskipTests=true.
This parameter will skip the tests only on the perform part.
Eventually i had to change a logic.
I have executed mvn clean install release:clean release:prepare release:perform -Pmaven-release
and create a maven-release. As result the tests are executed only at install
BR
Yulian Oifa
<profile>
<id>maven-release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<arguments>-Dmaven.test.skip</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
You can skip running the tests (managed by surefire plugin) with an explicitly specified parameter -DskipTests:
Something like this:
mvn release:perform -DskipTests
For intergration tests (managed by failsafe plugin) you can use a similar flag: -DskipITs
If you don't want even compile the tests you can use -Dmaven.test.skip=true (for both unit and integration tests)
I have a Java maven project. I am successfully able to disable the execution of unit tests through
<properties>
<java.version>1.8</java.version>
<maven.test.skip>false</maven.test.skip>
</properties>
However, this also disables unit tests when I run the "mvn test" command
Is it possible to modify POM.xml to have unit tests execute only through the command "mvn test", and disable unit tests when "mvn clean" or other such commands are run?
No.
This is also not how it is meant to be used.
Maven has a lifecycle. When you run mvn install or mvn deploy, this always includes the test phase. You can, of course, use command line parameters to skip the tests in one case and not the other.
Sometimes I need to run custom maven commands, e.g:
mvn install -DskipTests -DfailIfNoTests=false
I would like to be able to save this command so I don't have to type it out every time, e.g.
mvn forceInstall
Of course I can create run configurations in my IDE but I'm wondering if this is possible using maven only.
In the pom.xml you can define a default goal like this:
<project>
<build>
<defaultGoal>install</defaultGoal>
...
</build>
</project>
so you can use simply mvn only instead of mvn install. But for your need it would be better to use the .mvn/maven.config file which will be part of your project which can contain any kind of Maven commands. So you create a file in .mvn/maven.config which contains:
-DskipTests
-DfailIfNoTests=false
This can be used starting with Maven 3.3.1+.
Apart from that I would really reconsider using -DskipTests as default which from my experience only makes occasionally sense. What makes often sense is to use things like -T 4 etc. in the .mvn/maven.config file...or --fail-at-end. Be careful that those options file is not taken into account by CI server like Jenkins etc.
In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?
My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.
I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?
To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.
If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.
To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>${someModule.test.excludes}</exclude>
</excludes>
<includes>
<include>${someModule.test.includes}</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<someModule.skip.tests>false</someModule.skip.tests>
<skipTests>${someModule.skip.tests}</skipTests>
<someModule.test.includes>**/*Test.java</someModule.test.includes>
<someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
</properties>
With a POM like the above you can execute tests in a variety of ways.
Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
Skip all tests across all modules
mvn -DskipTests=true test
Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test
Found a way to exclude on command line:
# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*
This is from: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/
…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use
-Darguments=-DskipTests
to get it work.
If you want to use Maven profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
you might want to make it work doing something like this:
Skipping tests in some modules in Maven
I don't know if there is a supported command line option that does the same.
You also might try using environment properties directly, something as per this doc page:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html
i.e. something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${moduleA.skipTests}</skipTests>
</configuration>
</plugin>
then using mvn -DmoduleA.skipTests=false test to test that one module.
I've got a project set up using the Maven Cargo plugin to launch Tomcat with my webapp deployed in it, along with some other webapps that are needed for support. This works great. Unfortunately, when I run "mvn cargo:run" it doesn't do a build first, but instead just actually starts Tomcat running the code the last time I did do a build.
Previously I used the tomcat7 plugin instead, and that did do a build first and always ran the current version of the source code. As such, I could change my code and run "mvn tomcat7:run" and know that the code changes had been built and were running.
I can't find any way with the Cargo plugin to make it do this, but is there some way with Maven to make it at least run the Package phase when I run a specific plugin so that it will build the WAR file correctly first?
The Tomcat plugin automatically invokes the compile phase prior to executing itself. The Cargo plugin won't do that. In order to compile your code before executing the plugin, you need to run
mvn clean compile cargo:run
If you want to start and stop the container automatically before and after your integration tests, you can also bind cargo:start and cargo:stop to Maven's lifecycle phases. See Automatically executing and stopping the container when running mvn install for details.
Here is a full example how to integrate the start via Cargo in the usual build. https://github.com/khmarbaise/maui/tree/master/src/main/resources/it-example-container. You can start the integration tests via mvn -Prun-its clean verify which might be better
A completely different approach would be to use the exec-maven-plugin to execute multiple goals with one command:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>clean</argument>
<argument>compile</argument>
<argument>cargo:run</argument>
</arguments>
</configuration>
</plugin>
<!-- more plugins... -->
</plugins>
</build>
This way, you would only have to call
mvn exec:exec
to clean, compile and run your application.