How to run a maven goal when there is tests failures? - maven

I would like to know if there is a way to execute a goal when there is test failures?
Since maven stops its execution (fail fast mode) after encountering a test failure, is there any options to launch a goal when there is test failures?
Regards.

I've been searching for a way to do this as well, with not much success.
However, there is the following question which might provide some general hints:
Maven reporting plugins do not execute if a unit test failure occurs
The idea is that you would run mvn install (or whatever) first, and then run:
mvn -Dmaven.test.skip=true your-plugin:your-goal
This will let you run the build again without running tests, preserving the results for your perusal. Of course, this is only useful if your plugin is parsing the test results...

Though not recommended, by setting the surefire property testFailureIgnore to true, you can continue maven execution even when there are test failures.
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
...
<configuration>
<testFailureIgnore>true</testFailureIgnore>
...
</configuration>
</plugin>
...

Just do mvn clean install -DskipTests

I added this plugin in pom.xml and it worked well.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

If you want to the build to run KNOWING beforehand that there are going to be failures, you can use:
mvn <goal> -Dmaven.test.skip = true

Related

Tests run slower with Maven than with IDE

I'm facing some test time execution issue related to local test execution with Maven. I have around 250 tests which run about 15 min on the IDE test runner and about an hour (55min to be exact) when executing them with maven locally. I tried a lot of configurations to make test execution in parallel but neither of them work for me, the time is still the same...probably I'm doing something wrong. Can anyone help on this? The last maven surefire plugin configuration that I tried is the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
And for test execution command I've used:
mvn test
mvn surefire:test

geb passing test case as a parameter from maven commandline

How do I pass the test case to geb via maven commandline:
I have a test case class named GebishOrgSpec2.groovy
I want to pass this test case maven and let geb run it over the command line.
I am having successful build but no run of the test case
How do I solve this problem?
This is my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<includes>
<include>${inc.TestCase}</include>
</includes>
<systemPropertyVariables>
<geb.build.reportsDir>target/test-reports/geb</geb.build.reportsDir>
</systemPropertyVariables>
</configuration>
</plugin>
I have tried the following codes and they are all not running the testcase
mvn -Dinc.TestCase=GebishOrgSpec2.groovy test
mvn -Dinclude=GebishOrgSpec2.groovy test
mvn install -Dinc.TestCase=GebishOrgSpec2.groovy
mvn install -Dinclude=GebishOrgSpec2.groovy
I have tried these links
How to run Geb TestCases from windows command line?
Grails geb getting parameters from commandline
Geb exception when running tests in maven
they are all not working
System.getProperty("inc.TestCase") is returning null as well
I want to add that if I add the test case directly into the pom it does run successfully
can you please help
Don't use in-/excludes in your POM for something you want to do from the command line. It is much easier, both for
Surefire (unit tests) via mvn -Dtest=TestCircle test and
Failsafe (integration tests) via mvn -Dit.test=ITCircle verify
This works beautifully for Spock/Geb tests.

How to build EAR subproject and deploy it with Jenkins?

My Maven project has a bunch of subprojects like this:
proj/
projEAR/
projCommon/
How can I compile and build the EAR project + deploy it to my web server at the same time?
The way I do it now is:
proj$ mvn clean install
[... builds everything ... ]
proj$ cd projEAR
projEAR$ mvn weblogic:deploy
[... deploys the EAR file ... ]
I'd like to do this with one command. Something like
proj$ mvn clean install projEAR/pom.xml weblogic:deploy
This fails of course, but I hope you get the idea...
Update:
The reason for all this is that jenkins only accepts one pom-file and command. So the problem is really how to configure Jenkins to run Maven twice.
How about the weblogic-deployer-plugin of Jenkins. It will deploy your ear file to a weblogic instance. See WebLogic Deployer Plugin.
Quick and easy workaround
As a workaround, I can advise you to use some Jenkin's Plugins, like "M2 Extra Steps". It allow you to perform extra actions pre or post one. They are often use after a build to perform stuff like generating doc, or deploying something.
I know this is working well ... because I often use this trick :)
Suggestion, never tried
At this moment, I don't have a straight answer. I don't really know how to do it in only one maven command. What I would try is to attach weblogic deploy phase to install.
ear submodule --> pom.xml
<build>
[...]
<plugins>
[...]
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>weblogic-maven-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<configuration>
[...]
</configuration>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
<plugins>
[...]
<build>
It should work, but once again, I never tried it.
Don't hesitate to give feed back
I couldn't get it to work with Maven. But the way I solved it (in Jenkins) was
Create a pre-build step in Jenkins with the command mvn clean install using the parent pom: proj/pom.xml
Configure the main build as weblogic:deploy using projEAR/pom.xml.
This results in two commands being run: First mvn clean install followed by mvn weblogic:deploy.

Maven generate-source ALWAYS Successful

I am trying to run a ant task in maven generate-sources phase.
However, after many non productive "successes" i've realised, that the build always succeeds regardless of anything i enter.
Here is the plugin config for the pom.xml of my module.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<configuration>
<tasks>
<fail message="Something wrong here."/>
</tasks>
</configuration>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
It still succeeds. If i place a bad ant file. Still succeeds.
Does anyone know what i could be doing wrong here?
The Compile/Clean/Install/Deploy phases all work fine. Just "generate-source" doesnt work at all.
My settings.xml file only contains Repo information
Thanks for any advice
Edit:
I've been able to narrow the error down a little.
<configuration>
<target>
<echo message="hello ant, from Maven!"/>
<echo>Maybe this will work?</echo>
</target>
</configuration>
If place that within the configuration of the plugin, Not in the nested configuration in the execution tag. and run "mvn antrun:run" I see the echos. However If i place it in the nested configuration in the execution element, it doesnt show... Is there some link missing between my mvn goal and the antrun instruction?
I don't get it. The same configuration runs outside of the executions/execution tags, but not within.
Solution *Solution* Solution
AHHH I found it!
In my pom.xml I had all my 'plugins''plugin' configured under the 'pluginManagement' .. This 'pluginManagement' configures the plugins not for this project, but for all children project. Effectively a parent default config file for all children implementing the plugin.. I simply removed the 'pluginManagement' tags and it works...
Thank heavens. I've been looking at this for a full day...
Try adding <failOnError>false</failOnError> to the execution.
Per the documentation, this parameter "specifies whether a failure in the ant build leads to a failure of the Maven build. If this value is 'true', the Maven build will proceed even if the ant build fails. If it is 'false', then the Maven build fails if the ant build fails."
This is counterintuitive to me. I would think that the default value of "true" would cause the Maven build to fail if the ant build failed, but that isn't what the docs appear to be saying.

How can I get maven-release-plugin to skip my tests?

How can I get the maven-release-plugin to run without triggering the tests?
I have tried
-Dmaven.test.skip=true
and
-DskipTests
and
-DpreparationGoals=clean
...yet none work.
Yes, I know I shouldn't release if the tests don't pass, but I don't have control over making my coworkers write reliable tests.
-Darguments="-DskipTests" is what you want, or explicitly configuring the forked executions in the pom.
-Darguments="..." passes arguments to the forked maven process, but it is important to realise that there are two different switches being used here. The -DskipTests forces maven to not run any tests, but the tests are still compiled (this is important if you have any dependencies on a test-jar type). The -Dmaven.test.skip=true forces maven to not even compile the tests, which means that any test-jars will not be generated.
So, you must use -Darguments, but to skip tests running use only skipTests, to stop them compiling use maven.test.skip.
If you just want to skip integration tests, this will do it:
-Darguments="-DskipITs"
you have too differents choices to avoid and skip tests with the release plugin
The first is to pass as argument on cli to the release goal or phases by providing a -Darguments:
exemple: mvn -X -Darguments="-Dmaven.javadoc.skip=true -Dmaven.test.skipTests=true -Dmaven.test.skip=true" -P release-mode release:prepare
-The second is to perform thoses arguments on your pom.xml in the build like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<configuration>
<skip>true</skip>
<skipTests>true</skipTests>
<preparationGoals>clean validate</preparationGoals>
<arguments>-Dmaven.javadoc.skip=true -Dmaven.test.skipTests=true -Dmaven.test.skip=true</arguments>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release-mode</releaseProfiles>
<tagNameFormat>TEST-#{project.version}</tagNameFormat>
</configuration>
</plugin>
Note that the second method override the first.
I recommanded you to prepare release first on a single action and then you can edit the release.properties file on the working directorie and look the exec.additionalArguments properties if your arguments are there. It will look like: exec.additionalArguments=-Dmaven.javadoc.skip\=true -Dmaven.test.skipTests\=true -Dmaven.test.skip\=true -P release-mode.
After you can perform the release.
I have managed to avoid running the verify goal by simply adding the configuration preparationGoals to clean:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<preparationGoals>clean</preparationGoals> <!-- See here -->
</configuration>
</plugin>
Use the following argument to skip test
-Darguments="-DskipTests"
or
alternatively skipping by default
[...]
<properties>
<skipTests>true</skipTests>
</properties>
[...]

Resources