How to list all activated profiles in mvn in a multimodule project - maven

mvn help:active-profiles only list the profiles activate within the project + system settings you call it in.
It does not list the profiles that have been enabled/activated from i.e. the parent pom.
Any any way to actually see the full list of activated profiles by other means than trial-and-error to look at what properties are enabled or not ?

Another option is mvn help:all-profiles, which also list inherited profiles.
Displays a list of available profiles under the current project.
Note: it will list all profiles for a project. If a profile comes up with a status inactive then there might be a need to set profile activation switches/property.
More details in Maven's help plugin page

I double-checked this and indeed, inherited profiles aren't listed when mvn help:active-profiles is being called. This is with maven-help-plugin version 2.1.1.
There is even a bug-report about this: MPH-79.
As a workaround, you can use older version:
mvn org.apache.maven.plugins:maven-help-plugin:2.0.2:active-profiles ...

Do you always want to see the active profile in your build log? Then you could add the following plugin config to the <build> section.
In this example I added the plugin to the phase 'compile'. It could easily be added to a different phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>

this works in maven 3.x
mvn help:active-profiles

mvn help:effective-profiles
Works to list the active profiles

Related

maven-release-plugin with property activated profiles

I have a project where most of profiles are activated automatically based on a combination of a file presence and a system property. For example:
<profile>
<id>when-releasing-java-generate-sources</id>
<activation>
<file>
<exists>${basedir}/src/main/java</exists>
</file>
<property>
<name>build.release</name>
</property>
Now I need to release and deploy this project's artifacts and I will use maven-release-plugin.
My problem is that my profiles are not being activated even if I'm passing -Dbuild.release in the command-line.
Also I've tried to use -Darguments=-Dbuild.release, but could not see the plugins that are set in the profiles being executed.
So, there are any way to activate my project's profiles with maven-release-plugin ?
After many hours digging into my issue I found the solution.
First thing that I discovered was that maven-release-plugin has an annoying limitation: it doesn't work when your project has an aggregator POM that is not the parent.
So the first thing that I had to solve was bring the parent POM up to the top of projects hierarchy and turn it the aggregator also.
Once that was done I needed to set the release-plugin up this way:
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<arguments>-Dc8tech.build.release -Dc8tech.build.test.coverage ${arguments}</arguments>
<tagNameFormat>v#{project.version}</tagNameFormat>
<autoVersionSubmodules>true</autoVersionSubmodules>
<releaseProfiles>when-releasing-ensure-requirements</releaseProfiles>
</configuration>
</plugin>
Then I was able to release my project properly.

How to disable jar creation in commandline in a maven project?

I have a maven project for which I'm running two separate builds.
In one build I want to save the build time by disabling the jar creation of maven modules in it.(There are 45 maven modules). There is a Maven-Jar-Plugin that is being used to create the jars.
I want to conditionally disable the jar creation at the command line, that is, looking for something similar to -Dskiptests used to skip the unit tests though there is a surefire plugin by default.
The maven-jar-plugin does not provide any skip option.
However, several ways are possible to achieve your requirement.
You may just skip the phase which brings by default (via default mappings) the jar creation, that is, the package phase, and as such simply invoke
mvn clean test
The additional phases would not make sense if you do not create a jar file anyway: package, install, deploy would not have anything to process. Moreover, the additional integration phases may also be impacted depending on your strategy for integration tests, if any.
Alternatively, you can configure your pom as following:
<properties>
<jar.creation>package</jar.creation>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>default-jar</id>
<phase>${jar.creation}</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
As such, the default behavior would still provide a jar creation, while executing maven as following:
mvn clean install -Djar.creation=false
Would instead skip the creation of the jar.
What we are actually doing:
We are re-defining the default execution of the maven-jar-plugin
We are overriding its execution id, as such getting more control over it
We are placing its execution phase binding to a configurable (via property) phase
Default phase (property value) keeps on being package
At command line time you can still change it to any value different than a standard maven phase. That is, -Djar.creation=none would also work.

maven dynamic version

I searching a way to dynamise the version of my artifact depending on the profile.
Often I use the -SNAPSHOT suffix when I build for dev or preprod. But the database connection depends on the profile and I never know if the latest SNAPSHOT version was build using the dev or preprod profile.
The idea would be having a version like this
<version>1.0${suffix}</version>
with ${suffix} =
"" when building with prod profile
"-SNAPSHOT" when building with preprod profile
"-DEV-SNAPSHOT" when building with dev profile
Is there a way of achieving this ?
thanks
edit :
My goal is when I go on jenkins to build my jar, I build the same "tagged" version of my project with the 3 profiles and it deploys 3 differents artifacts.
Actually I tag my project and go build with the prod profile, then I modify the version to add -SNAPSHOT, commit, move the tag, re build with preprod profile, and then repeat for the dev profile.
Seeing your answer to #Michael-O comments, i'd recommend to configure the maven assembly plugin to create the final name of the artifact according to a system property set on each profile. For example:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create jar according to profile</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-${project.version}_${profile}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
where ${profile} should be a property set to a different value on each profile (for doing that you can see this question). I dunno if there's a variable to get the profile being currently used to build, that would be another question :)
It is not necessary to reassemble the JAR, I would rather use a standard mech: Simply specify a classifier for your artifact in the jar plugin.
Otherwise I would filter a properties in a given properties file and read that in your app. This what I do, e.g. system.env=prod|test|localdev.

active a profile during release prepare in maven 3 does not work

I have a need to active a profile during release:prepare.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
<configuration>
<arguments>-Prelease</arguments>
<preparationGoals>clean validate</preparationGoals>
<goals>test-compile</goals>
</configuration>
</plugin>
</plugin>
</build>
But when I run:
mvn release:prepare -DdryRun=true help:active-profiles
it never show profile release in active list. and it is not show up on active profile list when I do:
mvn release:perform help:active-profiles
I could not not use <releaseProfiles> since I want this profile to be used in both prepare and perform
Thanks!
One thing that I have used in this case is not using the -P argument, but rather triggering the profile through an environment setting using -Denv=release. Then in the POM, I have the profile activation based on the value of env. This has always worked for me. So in the arguments parameter, you could put something like
<arguments>-Denv=release</arguments>
Similar questions can be found here:
Profile activation on both release:prepare and release:perform
maven release plugin ignores releaseProfile
I think there's some misunderstanding here: mvn release:prepare -DdryRun=true help:active-profiles will never show the active profiles used during the release, but it shows the current active profiles. release:prepare will start another Maven thread (separate executable), and only then the release profile is activated.
In the maven-release-plugin-2.4 a lot has been fixed with regards to profiles, especially for Maven3, since some information was not available anymore when using Maven 2 but now is with Maven 3.
See the release notes

mvn release:perform automatically specify scm tag that includes release version

I would like to setup my maven release to run in batch mode, but I'm not a fan of the default scm tag ${artifactId}-${releaseVersion}. Instead, I'd like to simply tag it with ${releaseVersion}; however, I'm unclear if such a property exists (ie. without the -SNAPSHOT suffix).
I'd like the configuration to resemble the code below. Is such a default tagging possible with the maven-release-plugin?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
<configuration>
<tag>${releaseVersion}</tag>
</configuration>
</plugin>
I just got this to work when using Hudson to do my release. I noted that Hudson (with the Maven Release Plugin) is initiating the command with a property like -Dproject.rel.com.example:my-artifact-id=1.0.1. Using the following plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<tag>REL-${project.rel.com.example:my-artifact-id}</tag>
</configuration>
</plugin>
Resulted in the tag being REL-1.0.1
I'm new to the release plugin but I would assume something similar would work from the command line.
You can pass in the properties for:
releaseVersion -- What version you want it to be released as (1.0)
developmentVersion -- The next version (2.0-SNAPSHOT)
tag -- The name of the tag
a 1.0-SNAPSHOT implies a 1.0 release version, but doesn't set it. You can set that property in your POM file as a regular property.
try this:
<configuration>
<tag>${project.version}</tag>
</configuration>

Resources