How to enable maven profile when the embedded version is not -SNAPSHOT?

I am trying to use the gitflow-helper-maven-plugin extension for my maven collectors.

Therefore, I would like to set up my project to follow some additional steps when creating the release version and skip them when compiling SNAPSHOT, but I cannot find a way to enable the profile if ${project.version} contains -SNAPSHOT .

Any suggestion?

+6
source share
2 answers

Below is a possible approach, which is how you can always model the if in the Maven assembly:

  • Use the buid-helper-maven-plugin and its regex-property target to parse the default value of ${project.version} property and create a new $ {only.when.is.snapshot.used} with a value of true or ${project.version} in case of SNAPSHOT suffix found.
  • Configure the maven-source-plugin to fulfill the jar target with a special configuration using the skipSource option and passing it a new (dynamic) ${only.when.is.snapshot.used} property: in case of snapshot it will have value true hence skip the execution, otherwise will have value ${project.version} which will be used as false` and, therefore, do not miss this execution
  • Configure the same as above for maven-javadoc-plugin using the skip parameter

A sample of the above approach would be as follows:

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.10</version> <executions> <execution> <!-- sets the only.when.is.snapshot.used property to true if SNAPSHOT was used, to the project version otherwise --> <id>build-helper-regex-is-snapshot-used</id> <phase>validate</phase> <goals> <goal>regex-property</goal> </goals> <configuration> <name>only.when.is.snapshot.used</name> <value>${project.version}</value> <regex>.*-SNAPSHOT</regex> <replacement>true</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>create-sources</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <!-- skip when version is SNAPSHOT --> <skipSource>${only.when.is.snapshot.used}</skipSource> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.4</version> <executions> <execution> <id>create-javadoc</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <!-- skip when version is SNAPSHOT --> <skip>${only.when.is.snapshot.used}</skip> </configuration> </execution> </executions> </plugin> 

That is, there is no need for profiles, this configuration will be enabled only when the version without SNAPSHOT is used, dynamically and without any additional configuration (command line parameters or something else).


As a side reminder, you can also look at the maven-release-plugin , which will effectively refer to the source and javadoc plugins only when they release without the additional (minor) complexity of the above approach.

Otherwise, you can simply use the default profile coming from the Maven Super POM , which will actually reference the same source plugin and javadoc and can be activated using the performRelease property set to true . That is, in any Maven project, you could call the following:

 mvn clean package -DperformRelease=true 

or

 mvn clean package -Prelease-profile 

And you will automatically gain an advantage over the default super-profile, and you have sources and javadoc jars, although this approach should be used as the last option, since the profile may be removed from super-pom in future releases.

+10
source

I offer you another solution.

Why do not you install the version in the profile, and do not decide to activate the profile in the version? Like here:

 <version>${projectVersion}</version> <profiles> <profile> <id>snapshot</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <projectVersion>1.0-SNAPSHOT</projectVersion> </properties> </profile> <profile> <id>release</id> <properties> <projectVersion>1.0-RELEASE</projectVersion> </properties> </profile> </profiles> 
0
source

All Articles