Maven Snapshot in pom.xml - maven

There are a easy way to make Maven put "-SNAPSHOT" on my versions? We have a project with many POMs.
We usualy create branchs from TAGs that haven't SNAPSHOTs.

Use the release plugin:
mvn --batch-mode release:update-versions -DdevelopmentVersion=1.2.0-SNAPSHOT
You can use the versions-plugin to do that:
mvn versions:set -DnewVersion=1.2.0-SNAPSHOT
Alternatively, you could use release:branch to even create the branch as well (provided the scm settings in your pom are correct):
(check out tag)
mvn release:branch -DbranchName=my-branch -DupdateBranchVersions=true -DupdateWorkingCopyVersions=false -DreleaseVersion=1.0.1-SNAPSHOT
Yes, the property is named releaseVersion, although it is really the version for the branch. (see: http://maven.apache.org/maven-release/maven-release-plugin/examples/branch.html for details)

Define new property in parent pom.xml:
<properties>
<myprojectversion>0.0.1-SNAPSHOT</myprojectversion>
</properties>
and use it whenever needed: ${myprojectversion}.
If this solution is too rough. Consider to define more properties for minor, major, postfix etc. parts of intended version code.

Related

Can I refer to POM properties from the command line?

Given a property defined inside the pom, can I refer to that property from the command line?
This is what I would like to achieve:
# instead of:
mvn versions:set -DnewVersion=x.y.z
# something like this:
mvn versions:set -DnewVersion=properties:library.version
This way I can manage my multi-modular project version in one place and update all modules at the same time.
Is something like this possible? Perhaps by preconfiguring the version plugin inside the pom itself and completely omit the newVersion property from the command line?
You actually have a better way to reach you goal:
https://maven.apache.org/maven-ci-friendly.html
Idea is to use property ${revision} instead of version value.
So in all you POMs you do something like that:
Parent POM:
<groupId>my.group</groupId>
<artifactId>my.artifact</artifactId>
<version>${revision}</version>
Sub-modules:
<parent>
<groupId>my.group</groupId>
<artifactId>my.artifact</artifactId>
<version>${revision}</version>
</parent>
Now at the root of the project (where you project parent POM) you provide /.mvn/maven.config file (FYI: https://maven.apache.org/docs/3.3.1/release-notes.html (JVM and Command Line Options)):
/.mvn
maven.config
/submodule-one
/submodule-two
pom.xml
maven.config contains setting of the version to the property:
-Drevision=1.1.10-SNAPSHOT
And do not forget to provide in the parent POM flatten-maven-plugin configuration with flattenMode=resolveCiFriendliesOnly, exactly like described in the documentation by link above.
As result, maven.config is the only place where you need set version for all you modules.
And you not need to change anything in POMs when you want to change version.
But to make it works you need at least maven 3.5.0-beta-1.
P.S. you can see all of that in my maven testing project:
https://github.com/Gmugra/net.cactusthorn.maven
It's actually very easy, just provide the newVersion property in the pom:
<properties>
<library.version>1.0.0</library.version>
<newVersion>${library.version}</newVersion>
</properties>
Then simply execute versions:set without providing the property and it will pick up on the property from the POM instead:
mvn versions:set
Alternative solution:
You can also skip the property and have maven dynamically update the current version:
The following increases patch version:
mvn build-helper:parse-version versions:set -DnewVersion=${parsedVersion.nextMajorVersion}.${parsedVersion.minorVersion}.${parsedVersion.IncrementalVersion} versions:commit
This is built into Maven and works out-of-the-box.

version property dynamic in pom.xml

I have a Maven pom.xml, I build project and executable jar deploy in Nexus with Jenkins.
But I want changes of version name according to branch name.
For example: I have in pom.xml
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>iin-parent</name>
<packaging>pom</packaging>
I need set like this : (Branch- Master/Test1/Test2/..)
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>BranchName_0.0.1-SNAPSHOT</version>
<name>iin-parent</name>
<packaging>pom</packaging>
How can this be done?
I was using MVN build like -Drevision=BranchName-SNAPSHOT clean compile package deploy. But I want dynamically fetch the branch name.
enter code here
If you use clean compile package deploy you are duplicating several parts..only clean deploy is needed. Please read the documentation about Maven Life cycle.
Furthermore if you like to change the version dynamically you can do that by using the correct properties (Starting with Maven 3.5.0+) which are ${revision}, ${sha1} and ${changelist} so for example:
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>${revision}</version>
<properties>
<revision>1.0-SNAPSHOT</revision>
</properties>
This can be done in Maven like this:
mvn -Drevision=2.0-SNAPSHOT clean package
or if you like to do this for a branch:
mvn -Drevision=2.0-BranchName-SNAPSHOT clean package
You have to be aware if you like to do mvn clean deploy please read carefully the docs and follow them.

Change version of Maven project without manipulating the POM file

Is it somehow possible to change the version of a Maven project without manipulating the POM file?
Let's say I have a Maven project with version 1.5.0-SNAPSHOT but I want to build it as 1.5.46.
The Versions Maven Plugin unfortunately modifies the POM files.
Since Maven 3.5.0 this is possible using a special predefined property: ${revision}. Define the property with a default value (e.g. 1.5.0-SNAPSHOT) and when needed, set it during execution to a specific version (e.g. 1.5.46).
For example, define the following in your pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<name>Foo Module</name>
<version>${revision}</version>
...
<properties>
<revision>1.5.0-SNAPSHOT</revision>
</properties>
</project>
Build it using the default value:
mvn clean install
This will produce an artifact identified as org.example:foo:1.5.0-SNAPSHOT.
In order to build a specific version, set the revision property, for example:
mvn clean install -Drevision=1.5.46
This will produce an artifact identified as org.example:foo:1.5.46.
For further details, see the Maven CI Friendly Versions page.
Try to override project version with
mvn versions:set -DnewVersion=<version>
in your particular case:
mvn versions:set -DnewVersion=1.5.46
You can
Make a copy of your pom as temppom.xml
Replace the version in temppom.xml
Build with mvn -f temppom.xml.
Delete temppom.xml.
Maven supports delivery friendly versions, see https://issues.apache.org/jira/browse/MNG-5576 .
For more details I would suggest to talk with #khmarbaise
The plugin provides a goal to revert the changes made by it:
mvn versions:revert

Maven: Selecting Parent Project Based On Profile

I have a maven project - it is a plugin for jenkins. It's parent should be a:
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.414</version>
</parent>
But at the same time this plugin can be also used for hudson, without changing any line of code. But the parent project for it should be:
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>hudson-plugin-parent</artifactId>
<version>2.0.1</version>
</parent>
Can I specify 2 different profiles for that and use them to build plugin for jenkins or hudson accordingly? So that I call something like that:
mvn package -P jenkins
or
mvn package -P hudson
I have tried to specify properties in profiles, but those are not replaced by their values inside the <parent> tag. So is there any other possibility to build plugin for both, but with as much as possible common code and files?
Added: So, if I cannot do that, what should I do then? How to refactor? What the new structure should be?
As already mentioned, this is not possible.
Also, it is not possible to set a property for the parent's version as the interpolation for that happens a lot earlier than the handling of the profiles.
I would suggest that you create a masterbuild project as follows:
master
|-plugin-jenkins
|-plugin-hudson
|-plugin-assembly
The master should build all three as usual. However, in the assembly, you could add each of the two plugins as dependencies in separate profiles. And... each of these plugins can have the parent you like.
This is obviously somewhat a deviation from the Maven convention, but I believe it is a solution to your problem.
It's not possible because the tag "parent" is not available in the profiles section of the pom.
Currently we decided to stick with 1 repository and 2 separate pom.xml files, giving maven key which pom.xml use to build the project.
mvn package -f pom-jenkins.xml
mvn package -f pom-hudson.xml
No you cannot do that. you will have to refactor somehow to avoid the necessity.
As mentioned already not possible. I would suggest to make separate projects for jenkins plugin and hudson plugin. I assume that in not that far future that will not work anymore cause Hudons and Jenkins will diverge.
In general, you should be able to set the {group,artifact}Id and version of the parent POM via Java System Properties or Environment Variables, but it seems there is a Bug in Maven which will only be fixed in 4.x:
https://issues.apache.org/jira/browse/MNG-624
Another solution is to delegate the inclusion of the parent POM to your own parent POMs which you reference in the relativePath element, and change the content of the target e.g. via a symlink or cp command.
So in the main POM you would write:
<parent>
<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>
<relativePath>./my-parent.pom</relativePath>
</parent>
And in my-parent-jenkins you would just put:
<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.414</version>
</parent>
The same project information with the block for hudson you put in my-parent-hudson.pom.
No you can either use
ln -s my-parent-jenkins.pom my-parent.pom
or
ln -s my-parent-hudson.pom my-parent.pom
to include the respective parent POM without the need to maintain two different main POM files for your project.
In case POM does not exist at the place referenced in relativePath, Maven will look up the POM in the remote repository[1], which is also an easy way to overwrite a parent POM locally.
[1] http://maven.apache.org/components/ref/3.3.9/maven-model/maven.html#class_parent

How can I deploy only the pom file to my snapshot repository in Maven?

I would like to be able to deploy only the POM artifact (file) without the main artifact (JAR, WAR, etc), when running mvn deploy and version is a SNAPSHOT version.
Why?
We several developers working on multiple Maven projects. We have a Hudson server with a job per Maven project and version (e.g. foo-1.2, foo-1.3). Each job builds the project and deploys it to a Nexus server (upon success). Maven projects under development are marked as such by using -SNAPSHOT postfix in the version. For example: 1.2-SNAPSHOT, 1.3-SNAPSHOT.
Here's a sample scenario how a developer work is damaged due to this architecture.
Assume two Maven projects: foo-core and foo-webapp, both at version 1.2-SNAPSHOT.
Developer A is working on foo-core, made several changes and compiled it.
Developer A continues to work, but on foo-webapp.
Developer B started working and changing foo-core. It commits his work and pushes it to the SCM.
Hudson is triggered by SCM; Builds foo-core and deploys it to the snapshot repository in Nexus.
Developer A is running mvn install on foo-webapp. Maven is checking with Nexus, and finds that there is a newer version of foo-core in Nexus. It downloads it (filled with developer B changes) and than it fails compilation, since the changes made by developer A are not in the jar located in the local repository. The download overrides the file installed there by developer A.
Existing Solutions
I looked into maven-deploy-plugin, but this plugin deploys all artifacts attached to the project. If they had a way to configure which artifacts to deploy, it would have been great.
Question: Is there any way to solve this without resorting to writing my own deploy plugin, based on maven-deploy-plugin?
Basically to the -Dfile parameter, instead of the artifact, pass the pom.xml. Run the command and yay! mvn deploy won't give you any issues now. Here's a sample deploy command :
$ mvn deploy:deploy-file -DpomFile=pom.xml -Dfile=./pom.xml -DgroupId=my.group.id -DartifactId=artifact-id -DrepositoryId=bigdata-upload-snapshots -Durl=http://maven.mymaven.com/content/repositories/snapshots/
A prerequisite for this is that the repository be added in your settings.xml
[Edit]: I have supplied the parameters -DgroupId and -DartifactId of the project in the sample deploy command but they're not required (refer to Zac's comment below)
I never heard of such a possibility and also would be very astonished if that would be possible. As the pom and the resulting artifact are some kind of unit it would make no scence (to me) to deploy only parts of them.
Nevertheless you should consider to make a separate pom project which specified dependencies and plugins you might want to use on your JAR/WAR projects like this:
<groupId>foo.bar</groupId>
<artifactId>my-pom</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
and then inherit that pom project by your JAR/WAR projects like this:
<parent>
<groupId>foo.bar</groupId>
<artifactId>my-pom</artifactId>
<version>1.0.0</version>
</parent>
This is called project inheritance. You can change and deploy your pom project independent of the "child" artifacts.
EDIT after reading motivation:
As I understand you want to prevent maven to resolve SNAPSHOT artifacts from a repository (so that local version won't be overwritten). Have you ever tried to use the mvn -nsu option (see mvn -help)?
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
I never tried it but found this reported issue. Nevertheless I would give it a try (as the issue is not commented yet).
This works for me for deploying a pom file only (e.g next to an existing jar):
(Note: you need to specify packaging also, otherwise it will be uploaded as an .xml file which is not what you want.)
mvn deploy:deploy-file \
-Dfile=pom.xml \
-Dpackaging=pom \
-DgroupId=com.mycompany.package \
-DartifactId=my-artifact \
-Dversion=2.0.1 \
-DrepositoryId=serverIdFromSettingsXMLForCredentials \
-Durl=http://repositoryserver/myrepo/
Not exactly the answer these folks were asking for. My situation was I wanted to deploy only the parent pom. I'm using the spring-boot-thin-layout in a child module. This requires the parent module be deployed into artifactory. I added the following into my project. It enables skipping of install and/or deploy phase.
In my parent pom:
<properties>
<disable.install>true</disable.install>
<disable.deploy>true</disable.deploy>
<enable.deployAtEnd>true</enable.deployAtEnd>
</properties>
<profiles>
<profile>
<id>deploy-parent</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<disable.install>true</disable.install>
<disable.deploy>true</disable.deploy>
<deployAtEnd>${enable.deployAtEnd}</deployAtEnd>
</properties>
<build>
<finalName>${project.version}</finalName>
</build>
</profile>
</profiles>
And the in my child pom(s) or any module you don't want deployed with parent:
<properties>
<maven.install.skip>${disable.install}</maven.install.skip>
<maven.deploy.skip>${disable.deploy}</maven.deploy.skip>
<deployAtEnd>${enable.deployAtEnd}</deployAtEnd>
</properties>
So effectively when I run mvn deploy on the parent pom, it will compile all the modules, not run install on anything, and then at the end deploy any module not having `

Resources