Hudson maven test after deployment done - maven

I have configured Hudson for continuous integration. in my Maven job I have define "Deploy war/ear to a container " option and " M2 Extra Build Steps -post". in "M2 Extra Build Steps -post" I have define run to test case. But before deployment complete maven test will run.
Is there any way to configure run the test cases after deployment completed?
Thank you.

Add the parameter -Dmaven.test.skip=true or -DskipTests=true in MAVEN_OPTS (this is present Jenkins clicking the Advanced button), depending on whether you want to skip test compilation and execution or only execution.

Related

what are the proper stages for Gitlab CI/CD for a maven deploy?

I am using Gitlab CI/CD for a Java/Maven project and am confused by the many examples which show multiple stages, where each stage calls a specific Maven phase (e.g. clean, compile, test, install)
The maven documentation is very clear that each phase implicitly invokes all prior phases. So my question is, why do the examples not just invoke the last phase listed in the stages? For example, if the last non manually-invoked stage in the yml does an 'mvn install', why not just have that be the sole stage in the yml? It seems it is just a waste of CPU and time since the install will also call 'clean, compile, test, which have already all been called as part of the previous stages in the pipeline.
The "tutorials" that advice you to first call mvn compile then mvn test etc. have not understood the Maven lifecycle.
Just call one command, like mvn install for installing or mvn deploy if you want to deploy it with the help of Maven to some Maven repository.
The main reason for running different maven goals in different stages is clarity.
If a test fails and the pipeline has one job which runs mvn deploy you need to take a look at the logs why the pipeline failed.
But if a separate job exists where mvn test is executed you see at a glance that the pipeline failed because of tests.
In the example from the gitlab documentation they use cache to cache the output of mvn compile so the maven goals in the next steps don't compile from scratch but use the cache instead.

How to configure build step for maven goal in teamcity to run unit test

I use junit in my java project (developed using intellij idea) for unit test, and I want to configure build step in team city to run my unit tests only. I also use maven to build my project. It works when I set goals for maven to "clean compile" but I dont know how to configure build step to run unit tests.
Also in command line. when i run "maven test" it runs unit tests correctly and shows the failures.
I can't comment on TeamCity, but I'll try to help anyway out of my maven knowledge.
So first of all mvn clean compile will never run your unit tests, because maven has a concept of lifecycle and testing phase is coming after compile phase.
When you run mvn test it will run all the face up to (including) the test phase so the unit tests will run as a part of maven default lifecycle.
Now you're asking about a "build step for running unit tests" from which I conclude that you need a separate step. In maven phase is nothing more than running a series of plugins. In maven plugin has goals, so you can run a plugin responsible for running unit tests directly.
In maven this plugin is called "surefire" and a goal is called "test", so you can run:
mvn surefire:test
Given the classes (production code and tests) are compiled, you will see that this only runs your unit tests. So this probably has to be configured in Team City.

Fail the Jenkins build if there is test failures in a multi-module Maven Project

At the moment I have a project, lets call it mightymouse. Mightymouse POM is a multi-module project for mightymouse-web and mightmouse-backend.
Both of the sub modules have tests. In my situation, the mightymouse-web project compiles, test, package, install, deploys first. Then then -backend project goes through, only to fail during the integration-test stage. (Yet, it still goes through to the deploy stage).
The Jenkins job is setup to execute mvn clean deploy.
Question: Is there anyway to prevent Maven from going all the way to the deploy step on any module failing it's tests?
By default, a maven multi module build also uses the fail-fast option
--fail-fast - the default behavior - whenever a module build fails, stop the overall build immediately
Moreover, by default the maven-failsafe-plugin already fails as soon as an integration test fails via the testFailureIgnore option
Set this to true to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on occasion.
User Property: maven.test.failure.ignore
Default: false
Note that the maven-surefire-plugin has the same for unit testings.
Hence, by default a multi-module build should stop as soon as a test fails (unit test or integration test), no further module would be built and not further maven phase should be invoked.
However, a Maven Jenkins job (and not a freestyle Jenkins job invoking Maven) set this option to true by default and hence the build would not fail, it will keep on and deploy, while the job would be set to UNSTABLE and not SUCCESSFUL (but still deploying).
As such, you could change your maven invocation in your Jenkins job to:
mvn clean deploy -Dmaven.test.failure.ignore=false
Overriding thus Jenkins default settings and meeting your requirements.

Is it possible to have a maven build write a file when it fails?

I have a maven build running in a Jenkins job.
I need to do some post-processing stuff after the maven build but during the job, and so I'm running mvn with the -fn (--fail-never) option (jenkins will halt the job immediately otherwise).
The catch, of course, is that if the maven build step failed, I need to fail the job in a subsequent build step.
In the past I've done this sort of thing by writing a file to a certain location if the build failed, and then using the Ant task to fail the build. So this is the approach I'm going for.
Is it possible to get maven to write a file on build failure?
Instead of using the maven runner in Jenkin, you could execute the build as a shell script, then do something like...
mvn -q -fn clean verify || touch build_failed
I would suggest to use the configuration jenkins is capable of. Running a post-build-action in case of successful execution of the build and don't use --fail-never option.

Setting up jasmine-maven-plugin failures to make jenkins unstable

I'd like to configure jasmine-maven-plugin to make jenkins unstable if a test fails but the only options appear to be:
set haltOnFailure true and have failures break the build
set haltOnFailure false and have failures reported in the logs but the build succeeds.
Is there a way to check the logs post-test and mark the build unstable?
Sam Hasler's answer only works for freestyle Jenkins jobs. We use Maven jobs and this configuration option of the JUnit Jenkins plugin is unavailable for Maven jobs. So I was looking for a more universal solution.
What we did in order to get it working is to reconfigure the Jasmine Maven plugin so as to
no longer halt the build upon test failures and
write the Jasmine test reports to target/surefire-reports where Jenkins expects to find them. This has the additional advantage that we now also see the failed Jasmine test in the build job alongside our Java tests.
<haltOnFailure>false</haltOnFailure>
<jasmineTargetDir>${project.build.directory}/surefire-reports</jasmineTargetDir>
Now our build jobs are yellow (unstable) as expected, no longer red (failed).
Found the answer myself!
I had to configure jenkins to also look at the jasmine junit report:
under Publish JUnit test result report add **/TEST-jasmine.xml to Test report XMLs, comma separated if there is something there already:
**/TESTS-TestSuites.xml,**/TEST-jasmine.xml

Resources