2

In our build and deploy infrastructure, we have a Jenkins job that builds our multi-module Maven projects, and recently has been deploying the resulting artifacts (including an omnibus tarball) to Nexus. Then another job (sometimes triggered by a successful build job) runs the deploy, which pulls the required artifact from Nexus, based on the POM that the build job ran (the build job archives the POM so that the tarball can be retrieved by using the dependency:copy goal). For releases, this works fine and we can even deploy an earlier version if a rollback scenario is required. However, this breaks down for SNAPSHOT builds, because the POM does not contain any version identifier beyond that the artifact is a -SNAPSHOT.

The build output does contain information that could be grepped after the build is complete, so that we could use that as the version. Those familiar with Maven will recognize the following Maven output snippet:

[INFO] Uploading: http://nexus.eng.company.com/nexus/content/repositories/snapshots/com/company/application/application-dist/4.19.3-SNAPSHOT/application-dist-4.19.3-20160819.223606-7-eng.tar.gz

[INFO] Uploaded: http://nexus.eng.company.com/nexus/content/repositories/snapshots/com/company/application/application-dist/4.19.3-SNAPSHOT/application-dist-4.19.3-20160819.223606-7-eng.tar.gz (109687 KB at 413.1 KB/sec)

But is there a better way? The situation we are specifically trying to allow is for developers to deploy earlier builds (usually to deploy feature branch builds) but that currently isn't possible unless I find a way to grab that datestamped version identifier.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
OrionRogue
  • 398
  • 4
  • 14
  • Would [this](http://stackoverflow.com/q/38163124/5606016) help? As per the answer, maven would automatically and dynamically create classifiers based on branch name and only for SNAPSHOTs, de facto making SNAPSHOT artifacts unique and much more user friendly that timestamped ones – A_Di-Matteo Aug 20 '16 at 16:33
  • @A_Di-Matteo - This did help, in my case I only needed the buildhelper plugin to create a unique identifier and used that for the classifier. We have other information stored within Jenkins that tracks what branch each build came from. If you'd like to write your answer up formally I'll accept it. – OrionRogue Sep 12 '16 at 20:42

1 Answers1

2

A possible solution would be to create a dynamic property to use a classifier. The build-helper-maven-plugin can help in this case, e.g. with the following:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <id>regex-property</id>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
            <configuration>
                <name>custom.classifier</name>
                <pattern>yyyy-MM-dd_HH-mm-ss.SSS</pattern>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <classifier>${custom.classifier}</classifier>
    </configuration>
</plugin>

Above we are creating a new property, custom.classifier, and filling it with a timestamp defined by the pattern element (crafted so that no spaces nor invalid characters would be part of it, since classifier will be part of the final file name).

Then the maven-jar-plugin case set the classifier accordingly.

You could then further wrap the behavior above in a Maven profile in order to activate it only for CI jobs.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128