How to configure maven for executing test ONLY on deploy - maven

I have maven surefire plugin on pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>org.mycompany.service/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Now I want maven to execute test ONLY on deploy goal, therefore:
When executing mvn deploy tests should run
When executing mvn package or mvn install, tests should not run because goals are prior to deploy

The only way I can think of to make this work would be to bind the surefire plugin to the deploy phase. This has drawbacks:
The tests are run after the artifacts was installed.
Maybe the tests break because they are not meant to be run that late in the lifecycle.
I am not sure how to make sure that they are executed before the deployment happens.

Do you want this behaviour by default, or do you want that just for you or in certain environments?
Adding -DskipTests to the command line is usually good enough.
I have some CLI aliases that I use on my local workstation, so I don't have to type all of it, and don't forget to add it. See e.g. for zsh: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/mvn

Related

Possible to Skip Junit tests only in Jenkins?

Currently our companies Jenkins deploy process isn't set up to read external property files for testing. I would still like to write and run tests locally and then have tests be skipped when pushed to github (where a jenkins process gets kicked of to build the app and push it into a container).
Is it possible to programmatically tell surefire pluggin to only run in a local environment but not to run in the Jenkins pipeline?
I know I can use the following config:
<configuration>
<skipTests>true</skipTests>
</configuration>
but this means I need to remember to comment and uncomment each time I push, and I would rather not do that.
Thank you
The answer of #NullPointerExeption is an option.
You can also use maven profiles for that.
e.g.
<profiles>
<profile>
<id>jenkins</id>
<activation>
<property>
<name>jenkins</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
When a developer run tests they will be executed as skipTests is false by default
From Jenkins use
mvn clean verify -Pjenkins
This will skip tests.
In general it's good to have a jenkins profile and place all the staff that it's related to jenkins environment in this profile
More on profiles here
You can specify the build in your jenkins file to skip the test during build stage.
This will ensure your project is build skipping tests
stages {
stage('Build') {
steps {
sh 'mvn clean package -DskipTests'
}
}
}

A single Junit test is running twice in Maven build

I am running junit tests on a project with mvn clean test command in Jenkins
Strangely only of the tests of a particular test class is running twice.
Other tests are only running once.
maven surefire configuration in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
</configuration>
</plugin>
i am totally clueless
Best Regards,
Saurav

SonarQube and maven

when I build my project I usually call mvn clean install.
Now I tried to integrate sonarqube analysis. Therefore I created a new Run Configuration in Eclipse where I execute the goal mvn sonar:sonar with some parameters.
Is there a way to run sonar:sonar within the mvn clean install automatially?
This should be documented in "Analyzing with SonarQube Scanner for Maven".
It will be triggered in the build phase.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.3.0.603</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Then see "Running sonar analysis with mvn sonar:sonar ignores sonar-project.properties".
To associate it with an existing lifecycle phase seems problematic.

If argument in maven Surefire plugin, skip E2E tests

I couldn't seem to find anything on this but I'm curious if I can pass an argument during runtime to skip all of our projects E2E tests.
Is there anyway for me to do something like the segregated exclude block in the following pom example?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude unless="${skip.E2E.tests}> **/*E2E*.java</exclude>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
Then I could just call mvn clean install -Dskip.E2E.tests=true. Anybody seen anything like this?
I suppose I could do something like...
<exclude>${name.of.tests.to.exclude}</exclude>
and then mvn clean install -Dname.of.tests.to.exclude=**/*E2E*.javabut I would prefer to get an easy true or false argument to set rather than this in case some of the tests I want to skip do not include E2E and I need to add them to a list.
It's hard to tell just from the snippet of your pom that you are showing, but it looks like you are using surefire for both your unit and your e2e tests. Instead, you should consider using the failsafe plugin for e2e.
One benefit is that the e2e tests will run in a different stage so you get the behavior looking for by default. They are run during the verify stage of the project build. So, you can run mvn test to run unit tests only.
You can configure your project to use fail-safe like this:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Run them using: mvn verify
Running mvn install -DskipITs will skip only integration tests, while still running unit tests.
And running mvn install -DskipTests will skip both integration and unit tests.
If you want to implement such a condition, you could use Maven profiles and have two configuration:
The default one as part of the normal build, not skipping the E2E tests
the profiled one skipping them
The profile could be then activated upon property or direct activation.
As an example you could have:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>skip.E2E.tests</id>
<activation>
<property>
<name>skip.E2E.tests</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine}</argLine>
<excludes>
<exclude>**/*E2E*.java</exclude>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Note: the default Maven Surefire Plugin applying to normal build and then a profiled one.
Running:
mvn clean install
Will not activate the profile and your build will skip the tests. While running:
mvn clean install -Pskip.E2E.tests
or
mvn clean install -Dskip.E2E.tests=true
Will activate the profile and as such add the exclusion to the tests execution.
So this is exactly the scenario you were looking for, I presume.
Alternatively and as suggested by #AndrewEisenberg in the another answer, you could use the Maven Failsafe Plugin for different type of tests. The main two differences are that: it has different phase bindings AND when it fails, it does it in a safer way. As from official documentation:
If you use the Surefire Plugin for running tests, then when you have a test failure, the build will stop at the integration-test phase and your integration test environment will not have been torn down correctly.
The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute

disable maven release plugin defined in parent pom

I am new to maven. I have product structure as following
myWebProduct
pom.xml
coreModule
webModule
htmlTestModule
The maven release plugin is defined at the company level of pom.xml file which is parent of myWebProduct. It has set release plugin run default goals of deploy and default preparationGoals clean verify install.
I want to release product in myWebProduct level which works fine except I would like to skip release the htmlTestModule. Because deploy life cycle on htmlTestModule will cause deployment of war file to remote Tomcat servers and I don’t want this happening during release.
I tried to add following in pom.xml of the htmlTestModule.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
<configuration>
<skip>true</skip>
<preparationGoals>clean validate</preparationGoals>
<goals>testCompile</goals>
</configuration>
</plugin>
But when running 'mvn release:perform' at myWebProduct. I have seen the deploy goal was still executed on htmlTestModule. Could anyone help with this?
And I also tried following on htmlTestModule:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>donotRunMe</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
Still, the deploy goal always executed in htmlTestModule.
Thanks
I think you're taking the wrong approach here. Let me explain.
The deploy phase of maven is not meant to mean deploy to a remote server or anything like this. This is meant to be deploy to a remote repository.
Hence I believe you shouldn't try to skip the deploy phase in your module, but un-tie the deployment to a remote Tomcat server from the deploy phase, by making it a specific goal for example.

Resources