Automatically bumping up pom.xml with Maven release plugin - maven

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

Related

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>

Is it possible to use conditions in pom.xml

I've a maven web project and I'm using profiles in pom.xml for creating .war files for different environments. See the sample below snippet from my pom.xml
...
<profiles>
<!-- DEVELOPMENT PROFILE -->
<profile>
<id>development</id>
...
<build>
...
<plugin>
<groupId>com.google.code.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
...
<configuration>
<message>Message 1</message>
</configuration>
...
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-development.xml</webXml>
</configuration>
</plugin>
...
</build>
</profile>
<!-- PRODUCTION PROFILE -->
<profile>
<id>production</id>
...
<build>
...
<plugin>
<groupId>com.google.code.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
...
<configuration>
<message>Message 2</message>
</configuration>
...
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-production.xml</webXml>
</configuration>
</plugin>
</build>
</profile>
</profiles>
However, this makes my pom.xml pretty large - I've 5 different profiles. All I wanted to do is to display a custom message and use a custom web.xml file based on the profile.
Is there a way I could add conditions in the pom.xml so that I can simplify it some like below:
...
<profiles>
<profile>
<id>development</id>
...
</profile>
<profile>
<id>production</id>
...
</profile>
<profiles>
<build>
...
<plugin>
<groupId>com.google.code.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
<version>1.0.0</version>
<inherited>false</inherited>
<configuration>
<IF CONDITION TO CHECK IF development PROFILE>
<message>Message 1</message>
</IF CONDITION>
<ELSE CASE>
<message>Message 2</message>
<ELSE CASE>
</configuration>
<executions>
<execution>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<IF CONDITION TO CHECK IF development PROFILE>
<webXml>src/main/webapp/WEB-INF/web-development.xml</webXml>
</IF CONDITION>
<ELSE CASE>
<webXml>src/main/webapp/WEB-INF/web-production.xml</webXml>
<ELSE CASE>
</configuration>
</plugin>
...
</build>
No.
But you could experiment with defining properties in profiles, which might allow you to write things more concisely.

Executing JQAssistant with maven in a project with submodules with profiles

As the title shows i am using jqassistant with Maven. So far that worked well for small projects. Now i am using a project with multiple poms. As the guide tells (http://buschmais.github.io/jqassistant/doc/1.2.0/#_maven_plugin) i am only having this profile in the root-pom currently:
<profiles>
<profile>
<id>jqassistant</id>
<build>
<plugins>
<plugin>
<groupId>com.buschmais.jqassistant</groupId>
<artifactId>jqassistant-maven-plugin</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<goals>
<goal>scan</goal>
<goal>analyze</goal>
</goals>
<configuration>
<failOnViolations>true</failOnViolations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>com.buschmais.jqassistant</groupId>
<artifactId>jqassistant-maven-plugin</artifactId>
<version>1.2.0</version>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</profile>
</profiles>
I also tried using the command useExecutionRootAsProjectRoot
However the build never scans the whole project.
Subpoms have other profiles. Do i need the profile in every pom? Do i need to declare dependencies to jQA? Where - only in the parent pom?

JUnit - run tests in a category only if specific profile is active

I have the following tests:
FirstUnitTest.java
SecondUnitTest.java
FirstIntegrationTest.java
SecondIntegrationTest.java
The unit tests are not marked with a category.
The two integration tests are marked with #Category(IntegrationTests.class).
I want by default to run all tests EXCEPT for the integration tests.
If, however, a profile integration-tests-only is active, i want to run ONLY the integration tests.
I naively thought the following configuration would make this work:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>com.example.IntegrationTests</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>integration-tests-only</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>com.example.IntegrationTests</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
But while running the tests without a profile does exactly what I want - run only the unit tests, if I activate the integration-tests-only profile no tests run at all.
Any ideas what I'm doing wrong?
I assume that this happens because you include and exclude, and Maven merges the configurations and resolves to run nothing.
Consider this re-write of the config (did not run it so might have some minor issues):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${skipUnitTests}</skip>
<excludedGroups>com.example.IntegrationTests</excludedGroups>
</configuration>
</execution>
<execution>
<id>integ-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${skipIntegTests}</skip>
<groups>com.example.IntegrationTests</groups>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>no-tests</id>
<properties>
<skipTests>true</skipTests>
</properties>
</profile>
<profile>
<id>unit-tests</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<skipUnitTests>false</skipUnitTests>
<skipIntegTests>true</skipIntegTests>
</properties>
</profile>
<profile>
<id>integ-tests</id>
<properties>
<skipUnitTests>true</skipUnitTests>
<skipIntegTests>false</skipIntegTests>
</properties>
</profile>
</profiles>

Maven: How to print the current profile on the console?

I'm trying to print the current profile that is active running a build of a Maven Project.
I'm using the maven-antrun-plugin in order to print messages on the console, in combination with a property that refers to the current profile.
I have tried the following properties:
${project.activeProfiles[0].id}
${project.profiles[0].id}
But in both cases it prints the "string" as it is written, without resolving the variable.
This is my test:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${project.activeProfiles[0].id}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
But this is the result that I obtain:
main:
[echo] current active profile: ${project.activeProfiles[0].id}
Any suggestion will be appreciated.
Thanks.
The maven-help-plugin offers what you need. It has an active-profiles goal.
You can add it to your pom or even call it from the command line (include it in your maven build call). The How can I tell which profiles are in effect during a build? section of the Maven profile introduction page will show you how. In short:
mvn help:active-profiles
As this does not work for you (see comments) here is another solution:
I think the active profiles (there can be more than one!) are not propagated as available variables - but properties are.
So set a custom property in the profile section and use that, like
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<myProfile>default</myProfile>
</properties>
</profile>
<profile>
<id>debug</id>
<activation>
<property>
<name>debug</name>
</property>
</activation>
<properties>
<myProfile>debug</myProfile>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>current active profile: ${myProfile}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
you can add the maven-help-plugin in your pom to display always the active profile
<build>
<plugins>
<!-- display active profile in compile phase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
source: https://www.mkyong.com/maven/maven-profiles-example

Resources