With the Artifactory Pipeline DSL you can easily manage and run a release build for your Maven project by following the instructions below:
First, clone the code from your source control:
git url: 'https://github.com/eyalbe4/project-examples.git'
If the pom file has a snapshot version, Maven will create snapshot artifacts, because the pom files include a snapshot version (for example, 1.0.0-SNAPSHOT).
Since you want your build to create release artifacts, you need to change the version in the pom file to 1.0.0.
To do that, create a mavenDescriptor instance, and set the version to 1.0.0:
def descriptor = Artifactory.mavenDescriptor() descriptor.version = '1.0.0'
If the project's pom file is not located at the root of the cloned project, but inside a sub-directory, add it to the mavenDescriptor instance:
descriptor.pomFile = 'maven-example/pom.xml'
In most cases, you want to verify that your release build does not include snapshot dependencies. The are two ways to do that.
The first way, is to configure the descriptor to fail the build if snapshot dependencies are found in the pom files. In this case, the job will fail before the new version is set to the pom files.
Here's how you configure this:
descriptor.failOnSnapshot = true
The second way to verify this is by using the hasSnapshots method, which returns a boolean true value if snapshot dependencies are found:
def snapshots = descriptor.hasSnapshots() if (snapshots) { .... }
That's it. Using the mavenDescriptor as it is now will change the version inside the root pom file. In addition, if the project includes sub-modules with pom files, which include a version, it will change them as well.
Sometimes however, some sub-modules should use different release versions. For example, suppose there's one module whose version should change to 1.0.1, instead of 1.0.0. The other modules should still have their versions changed to 1.0.0. Here's how to do that:
descriptor.setVersion "the.group.id:the.artifact.id", "1.0.1"
The above setVersion method receives two arguments: the module name and its new release version. The module name is composed of the group ID and the artifact ID with a colon between them.
Now you can transform the pom files to include the new versions:
descriptor.transform()
The transform method changed the versions on the local pom files.
You can now build the code and deploy the release Maven artifacts to Artifactory as described in the in this article.
The next step is to commit the changes made to the pom files to the source control, and also tag the new release version in the source control repository. If you're using git, you can use the git client installed on your build agent and run a few shell commands from inside the Pipeline script to do that.
The last thing you'll probably want to do is to change the pom files version to the next development version and commit the changes. You can do that again by using a mavenDescriptor instance.