Can Maven release:prepare update snapshot version to the release version in batch mode - maven

mvn release:prepare
It constantly asks me to resolve snapshot dependencies.
Is there a way to do this in batch mode so that maven automatically uses the associated release. i.e. if a dependency is 1.0.0-SNAPSHOT it will automatically update this dependency to the 1.0.0 release?
[INFO] Checking dependencies and plugins for snapshots ...
There are still some remaining snapshot dependencies.: Do you want to resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: : 1
Resolve Project Dependency Snapshots.: 'com.my.project' set to release? (yes/no) yes: : yes
What is the next development version? (0.0.2-SNAPSHOT) 0.0.2-SNAPSHOT: : 0.0.2-SNAPSHOT

Note that you can configure Maven ignore SNAPSHOT dependencies checking by using allowTimestampedSnapshots, according to maven-release-plugin documentation:
allowTimestampedSnapshots:
Whether to allow timestamped SNAPSHOT dependencies. Default is to fail when finding any SNAPSHOT.
Type: boolean
Since: 2.0-beta-7
Required: No
Expression: ${ignoreSnapshots}
Default: false
Or simply run the command below:
mvn release:prepare -DignoreSnapshots=true
However, it is still recommended to resolve all SNAPSHOT dependencies before doing the final release, as it is the convention used by most people. You should always consider to do it manually at developing phase rather than automatically batching at release phase, as change/upgrade project's dependencies (either your own or third party jar) may sometimes introduce bug or incompatibility and break your project, which usually need developer's attention and fix them before doing final release.
In another word, dependency resolution is not a task which should be done at release phase, moreover, it is not a task which should be done automatically without developer's attention.

You can do update of the SNAPSHOT's via the versions-maven-plugin before the release.

If your dependency is a program of you own and its lifecycle is closely linked to the one you try to release, you may think about using a multimodule project : http://maven.apache.org/guides/mini/guide-multiple-modules.html. Maven release plugin would update version for all of you dependencies modules.
If not, you are probably doing something you should not.
Just changing 1.0.0-SNAPSHOT to 1.0.0 does not ensure you app will continue to work. So does not consider that Maven should !
Further considerations
Maven release plugin checks if you are using a snapshot dependency, because Snapshot, by definition, are unstable vesions. It may change along the time. That means what was working today may not work tomorrow.
Releasing means that your version is stable, and the build can be reproduce anytime without any change. Using Snapshot version, this assertion become false.
So,

Related

How can I fail a Jenkins Maven release if it contains SNAPSHOT in the pom.xml

When we do a release we want to fail the release if any of the dependent apps are still in SNAPSHOT version in the pom. For normal builds this should be allowed.
I guess there might be 2 options:
Is there a maven switch for the jenkins maven plugin to specify such and option?
Run a bash script to check for "SNAPSHOT" string in pom.xml, but then how can I detect "if this is a release" inside a jenkins job?
Thanks.
You can use Maven Release Plugin to perform a release, it will fail the release in case of SNAPSHOT dependencies.
You also can specify checking for timestamped SNAPSHOT dependencies, by default it is false.

Maven force update only for specific dependency (mvn -U for selected dependencies)

The command mvn -U forcing all project dependencies to get updated from remote repository.
Is it possible to force such update for specific selection of dependencies, by explicit selection / by some logic?
The reasoning:
I don't want to force checking all the dependencies we have because how time consuming it is. All I need, is to verify a few of them or even specify only one dependency. So, such solution is highly desired.
There are two maven plugins that may help you here.
The first, dependency, will simply download the given version of a dependency:
mvn dependency:get -Dartifact=groupId:artifactId:version
The second, versions, offers some behaviors which you may also find helpful.
By running mvn versions:use-latest-releases -Dincludes=groupId:artifactId your project's pom will be updated with the latest release version of the dependency specified by the '-Dincludes' flag. You could then run the first command to download the version now referenced by your pom.
Both of these behaviors can be heavily customized and automated to do some quite awesome things. To get more help on a plugin goal, run: mvn plugin:help -Ddetail=true -Dgoal=goal
Example: mvn versions:help -Ddetail=true -Dgoal=use-latest-releases
For further information:
versions, dependency, and plugins

mvn release:prepare going into unending cycle

I ran
mvn release:prepare for a project and it is a multi-module build.
I entered yes for the following question :
There are still some remaining snapshot dependencies.: Do you want to
resolve them now? (yes/no) no: : yes
Dependency type to resolve,: specify the selection number ( 0:All 1:Project Dependencies 2:Plugins 3:Reports 4:Extensions ): (0/1/2/3) 1: :0
Resolve All Snapshots.: 'com.test:core-api' set to release? (yes/no) yes: : yes
What is the next development version? (0.0.6-SNAPSHOT) 0.0.6-SNAPSHOT: : 1.2.0-SNAPSHOT
and it keeps on asking me the following in an unending cycle.
What is the next development version?
I have to Ctrl+C to exit the loop.
I ran into this same issue when I refactored the groupId in my multi-module build.
The cause is exactly what the error message says, but it can be elusive to spot the snapshots dependency.
In my case, the toplevel groupId and the groupIds in the <parent> stanzas in the submodules were all correct, but also some modules in the tree have cross dependencies to other modules, and as such are included in the <dependencies> or <dependencyManagement> stanzas.
These dependencies are tied to the project version by, well,${project.version}, so they're easy to miss in a text search for 'SNAPSHOT'. Since I forgot to refactor the groupIds in these dependencies, maven sees it as a 'foreign' snapshot dependency, outside of the project itself.
From past experience it's not an un-ending cycle, but a HUGE amount of snapshots dependencies need to be confirmed (hence you get the impression it's an infinite loop). This typically happen in a maven aggregation (multi module) project.
If you know you always accept the DEFAULT answer, you can try running it in batch mode
mvn --batch-mode release:prepare
The default behavior of release plugin is to ask confirmation everytime it detects a snapshot dependencies. If you follow maven common practice, release your snapshot dependencies first and update reference to the dependencies before releasing your project.
http://maven.apache.org/maven-release/maven-release-plugin/examples/non-interactive-release.html has a good information on performing non-interactive release

Tycho resolves the wrong version of my own manifest-first artifacts

Consider the following scenario: My application has some dependencies on my own POM-first artifacts (built with pure Maven) and some dependencies on my own manifest-first artifacts (built with Tycho). For the POM-first artifacts, Tycho resolves the exact the version I specified in the POM. For the manifest-first artifacts, Tycho resolves the locally built units which may have a higher version.
In my concrete case, I specified a dependency in the pom.xml to the manifest-first artifact in version 1.2.0, but I get warning "The following locally built units have been used to resolve project dependencies" with version 1.3.0.2012xxx.
I have already found following bugs and discussions, but I don't understand why there is a difference in Tycho resolving POM-first and manifest-first dependencies.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=355367
http://dev.eclipse.org/mhonarc/lists/tycho-user/msg01673.html
Dependency-resolution is a two-step process in Tycho:
First, Tycho computes the so-called target platform, which is
the set of artifacts that are considered for dependency resolution.
In this step, Tycho evaluates the POM dependencies according to the
Maven rules and adds the result to the target platform. Also, all
Tycho artifacts you have built locally with mvn install are added
to the target platform.
Then, Tycho resolves your project's dependencies (from the
MANIFEST.MF, feature.xml, etc.) according to the OSGi rules. Unlike
in Maven dependencies, OSGi dependencies are typically specified as
version ranges. So if you write
Require-Bundle: my.bundle;bundle-version="1.2.0"
you are saying that you want version 1.2.0 or later. You typically don't want to specify an exact version here, because this would have implications on the runtime. But you do want to control what happens at build time, which is why there are various ways to control the content of the target platform.
In your particular case, you have your POM-first artifacts in the target platform in the version specified in the POM. Then, you also seem to be specifying a POM dependency to your Tycho artifacts (which is uncommon, but okay), so you will have the Tycho artifacts in the specified version in the target platform. But since you have also built a newer version of the Tycho artifacts locally with mvn install, these will also be in the target platform. The dependency resolution (step 2) hence has a choice between two versions, an will typically pick the later version.
To prevent the locally built artifacts from being added to the target platform, you can delete the file ~/.m2/repository/.meta/p2-local-metadata.properties. (I'm assuming you already know this, but just to be sure. Bug 355367 will also bring a alternative, more convenient option in 0.16.0.)
And now I finally get to your question why the behaviour is different for POM-first artifacts compared to Tycho artifacts:
Assume multiple Tycho artifacts are built together in the same reactor. Then each artifact can use the other artifacts as dependencies without needing any specific target platform configuration, e.g. you don't need POM dependencies to the artifacts in the same reactor. (Or in other words: upstream artifacts from the same reactor are automatically part of a module's target platform.) So in order to support re-builds of parts of a Tycho reactor (after a mvn install of the full reactor), locally installed Tycho artifacts need to be added to every module's target platform. Tycho can't know if they were originally part of the same reactor, so it just adds them all.
For POM-first artifacts, the reference to the artifact is always there (through the Maven configuration inheritance), even if only a part of a Tycho reactor is built. Therefore there doesn't need to be any mechanism for picking up any version of the locally built POM-first artifacts, but Tycho can add the exactly specified version to the target platform.
For those interested to force tycho to ignore local artifacts when resolving the target platform, add the CLI tycho.localArtifacts=ignore as in e.g.
mvn clean install -Dtycho.localArtifacts=ignore
More details can be found on the Tycho-Target Eclipse Wiki

Maven install:? - how to create a release candidate?

Is it possible to create a release candidate from my current maven project without changing the version number in the pom.xml?
I just want to build a new maven artifact form my project with a specific version number.
For example: my current pom.xml has the version '0.0.1'. I have called mvn install to create the artifact in my local repository. Now I would like to create a second artifact with the version '0.0.1-RC1'
Is this possible from the mvn command line without changing the version number in my pom.xml?
I would advice against your suggestion of not changing the version number. One of the Maven's benefits is to keep your releases in sequence, i.e. after you have made your '0.0.1-RC1' candidate you will continue to work on the '0.0.1-RC2-SNAPSHOT' version (which may result in a '0.0.1-RELEASE').
That said, you don't have to change the version number manually. The Maven Release Plugin offers great help with this with commands such as mvn release:prepare and mvn release:perform. Also read the Maven build versions in the Maven reference manual. You may also find the discussion about Maven version scheme interesting.
As I see it you have two options:
Embrace the release plug-in. It's been designed to address this problem.
Use the versions plug-in and issue your own source code control commands.

Resources