Maven surefire tests - includes and excludes - maven

I have a class A.java and two test classes, ATest.java and AITests.java. The ITest is for integration. The tests must be performed as:
When no Maven profile is selected only the Atest must be tested.
When the itests profile is activated the both tests (ATest and AITest) must be tested.
The problems is, when I use the command
mvn -P itests test
then only the ATest is tested, without the AITest. But I have no idea what I am missing here. Any hint?
My pom.xml is:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<excludes>
<exclude>**/*ITest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>itests</id>
<activation>
<property>
<name>itests</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
...

For integration tests, please use the Maven failsafe plugin:
https://maven.apache.org/surefire/maven-failsafe-plugin/
You can skip it on the command line if you prefer.

Related

Does Maven support multiple variants?

I have a pom for multiple clients, the client specific code is in com.foo.custom. So, for customer A there are classes in com.foo.custom.customerA, for customer B some classes in com.foo.custom.customerB etc. The set of classes in com.foo.custom.customerA is different from those in com.foo.custom.customerB.
When I do mvn package, the code for all the customers shows up in the jar, as expected. Is there a way of selecting just one customer, e.g. mvn package -Dcust=customerB, and then have that one customer in the package and the others not?
You can construct a multi-module project with modules like
common
customerA
customerB
...
The customerX modules use common as dependency.
Here is a pratical solution:
pom.xml fragment
<profiles>
<profile>
<id>ncustomerA</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<includes>
<include>src/main/java/customerA/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>ncustomerB</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<includes>
<include>src/main/java/customerA
B/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles
Build commands
mvn package -P customerA
mvn package -P customerB
Here https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html further information.

Springboot remove class only when deploy

I have one springboot project but I wanna to deploy in my nexus to use with component in another project, so I try to remove some classes just like this:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>br/com/lumera/balcaoonline/api/BalcaoonlineApiApplication.class</exclude>
<exclude>br/com/lumera/balcaoonline/api/central/controller/rtdpj/*.class</exclude>
<exclude>**/application-*.yml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
I need to remove the controllers and the main class, but now when I try to run the project the springboot dont find the main class
how can I fix this?
tks
You can use profiles to create one jar for nexus and another not for nexus.
Maven command:
mvn install -DwithNexus=true
Example:
<profiles>
<profile>
<id>nexus</id>
<activation>
<property>
<name>withNexus</name>
</property>
</activation>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>br/com/lumera/balcaoonline/api/BalcaoonlineApiApplication.class</exclude>
<exclude>br/com/lumera/balcaoonline/api/central/controller/rtdpj/*.class</exclude>
<exclude>**/application-*.yml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>no-nexus</id>
<activation>
<property>
<name>withNexus</name>
<value>!true</value>
</property>
</activation>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>br.com.lumera.balcaoonline.api.BalcaoonlineApiApplication.class</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>

How rerun failed test cases of cucumber-jvm in jenkins

How to rerun failed test cases of cucumber-jvm in jenkins?
According to answers mentioned in this thread:
How to rerun failed test cases in cucumber-jvm?
There is different maven command to move and run scenarios for rerun.txt. How to execute them in Jenkins with separate maven command for rerun?
I use cucumber-serenity framework, which uses cucumber-jvm in the background to run everything. Here are the relevant parts of my pom.
I have everything in a separate project, not mixed with any other code. If this is not your case, the following might break your build!
I turn off unit tests:
<build>
<plugins>
<!-- no unit tests: skip anything named *Test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
This does not relate to your question, but I manage all my selenium browser drivers with:
<!-- docs: https://ardesco.lazerycode.com/testing/webdriver/2012/08/12/introducing-the-driver-binary-downloader-maven-plugin-for-selenium.html -->
<plugin>
<groupId>com.lazerycode.selenium</groupId>
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
<version>${driver-binary-downloader.plugin.version}</version>
<configuration>
<rootStandaloneServerDirectory>${project.basedir}/selenium/bin</rootStandaloneServerDirectory>
<downloadedZipFileDirectory>${project.basedir}/selenium/zip</downloadedZipFileDirectory>
<customRepositoryMap>${project.basedir}/RepositoryMap.xml</customRepositoryMap>
<overwriteFilesThatExist>true</overwriteFilesThatExist>
</configuration>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>selenium</goal>
</goals>
</execution>
</executions>
</plugin>
I use the failsafe-plugin to run my integration tests:
<!-- integration tests: run everything named *IT -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<systemPropertyVariables>
<!-- set by driver-binary-downloader-maven-plugin -->
<webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
<webdriver.gecko.driver>${webdriver.gecko.driver}</webdriver.gecko.driver>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The above will not rerun any failed tests, which is what you probably want when you run stuff locally on your machine.
On Jenkins only, I turn on the rerunning of failed tests:
<profiles>
<profile>
<id>jenkins</id>
<activation>
<property>
<name>env.JENKINS_HOME</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<dependencies>
<!-- docs: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/rerun-failing-tests.html#Re-run_execution_in_Cucumber_JVM -->
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.plugin.version}</version>
</dependency>
</dependencies>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I pasted the following in my Pom.xml file and I do not see any failed test case rerunning in Jenkins. Can you explain how to configure this Jenkins please
<profiles>
<profile>
<id>jenkins</id>
<activation>
<property>
<name>env.JENKINS_HOME</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<dependencies>
<!-- docs: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/rerun-failing-tests.html#Re-run_execution_in_Cucumber_JVM -->
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.22.2</version>
</dependency>
</dependencies>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Automatically bumping up pom.xml with Maven release plugin

I am playing with the Maven release plugin and I am trying to bump up the pom.xml version automatically. I noticed when I do:
mvn --batch-mode release:prepare
it will automatically bump up the z version, i.e. if it is x.y.z it will be x.y.(z+1). Is there a way to bump up the y or x version without having to specify before hand what those versions should be?
I stumbled upon some blogposts on how to achieve this, and made this maven-setup which makes it possible to specify if you want to do a major, minor or patch-release:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven.build.helper.plugin.version}</version>
<executions>
<execution>
<id>parse-versions-for-release</id>
<phase>initialize</phase>
<goals>
<goal>parse-version</goal>
</goals>
<configuration>
<propertyPrefix>parsedVersion</propertyPrefix>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>major</id>
<activation>
<property>
<name>major</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<tagNameFormat>#{project.artifactId}-#{project.version}</tagNameFormat>
<useReleaseProfile>false</useReleaseProfile>
<releaseVersion>${parsedVersion.nextMajorVersion}.0.0</releaseVersion>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>minor</id>
<activation>
<property>
<name>minor</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<tagNameFormat>#{project.artifactId}-#{project.version}</tagNameFormat>
<useReleaseProfile>false</useReleaseProfile>
<releaseVersion>${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0</releaseVersion>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Here we use the build-helper-maven-plugin to figure out what the current (and next) version is, and we use maven profiles to specify if we want to do a major, minor or patch (default) release.
Source code can be found here: https://github.com/mortenberg80/maven-release-example
The blog posts I got inspired from were:
https://thihenos.medium.com/maven-release-plugin-a-simple-example-of-package-management-9926506acfb9
Maven release: next development version in batch mode

Maven Plugin Surefire no accepting outputDirectory

In Maven pom tried to have a single profile only for tests and their reports. Also all test related reports should end up in a single folder per project.
Acording to the documentation this should do the trick
<profiles>
<profile>
<id>integration</id>
<activation>
<property>
<name>integration</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<excludes>
<exclude>**/*$*</exclude>
<exclude>**/*_closure*</exclude>
<exclude>**/support/**</exclude>
</excludes>
<outputDirectory>${basedir}/target/reports</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Still all reports end up in ..\target\surefire-reports. Any idea why the ouputDirectory settings doesn't work
According to the documentation you have to use the reportsDirectory instead of outputDirectory which does not exist.

Resources