Maven Deploy : How to get snapsnot version - maven

Is there a way to capture the actual snapshot version and let's say output it to file?
[INFO] Uploading to nexus: https://xxxx/repository/xxx/xxx/0.0.1-SNAPSHOT/xxx-0.0.1-2
0180809.182425-2.pom
I can see it in the console output, but I am looking for a reliable way to extract it and use it for some post processing, by example, assemble it in a docker image and deploy it.

As khmarbaise suggests, you could get the snapshot info from a Maven extension such as https://github.com/khmarbaise/deployment-recorder-extension Recording a timestamp would also give you a good approximation as normally snapshot ids are timestamp-based.
It is worth being cautious about how much value you put on the snapshot for traceability as snapshots are typically not retained in nexus indefinitely. If you're looking to trace the code in a docker image that is itself using a latest/snapshot tag then you might find more value in the last git commit (https://github.com/ktoso/maven-git-commit-id-plugin). If the image will have its own dedicated tag then it would be advisable to also create a corresponding tag in the source code.

This is very difficult to achieve during the build process as the deploy-plugin uses the very timestamp of the deployment to version the artifact and it will not share that information other than in the log. That means that any timestamp that you catch during the build process will be before that timstamp and will therefore not be suitable.
To solve the issue, you either need to parse the command output or use external tools:
Use khmarbais's Deployment recorder (as mentioned by #Ryan Dawson)
Read the maven-metadata from Nexus which contains the latest snapshot info
Search whether Nexus has an api that exposes that information (I am using Artifactory and am therefore not an expert for Nexus)

Related

Jfrog Artifactory: How to delete old snapshot artifacts

I had a task to delete old SNAPSHOT artefacts which are under many folders/directories.
We can't go and delete each and every artefact manually so I would like to go with restAPI.
For clear info:
https://artifactory.com/artifactory/maven-local/com/aa/bbb/cccc/dddd/XYZ-SNAPSHOT/abc.jar
https://artifactory.com/artifactory/maven-local/com/aa/bbb/cccc/dddd/XYZ-SNAPSHOT/xyz.jar
https://artifactory.com/artifactory/maven-local/com/aa/bbb/cccc/eeee/XYZ-SNAPSHOT/pqr.jar
https://artifactory.com/artifactory/maven-local/com/aa/bbb/dddd/eeee/XYZ-SNAPSHOT/lmn.jar
Above 4 examples have different directories.
My script needs to go each and every directory and have to verify for XYZ-SNAPSHOT, if it found then we can make a url and delete through CURL.
How can we achieve this? Or is there any other way to do it?
You should probably want to use Artifactory Query Language (AQL) which is the easiest way to find artifacts and modules according to patterns. You can find bunch of examples in the page. Moreover, to perform the deletion easily and even automate the process in the future, I advise using JFrog CLI. You can also read this interesting blog about similar use case.
Also, there is the 'Max Unique Snapshots' field in your local Maven repository settings. You can use that for Artifactory to keep a specified number of unique snapshots per artifact.

Artifactory/Jenkins Upload Spec using classifier?

I'm looking to use the upload spec to upload artifacts to artifactory from a jenkins job. I'd like to be able to use it to additionally attach a classifier to the artifact for if it is referenced through Maven.
Do you know if there's a way to do this? I'd like to use the upload spec for simplicity and ideally avoid having to get my hands too dirty with Maven.
I realised the answer to my own question. You assign a repository layout to your repo. For example maven-2-default. This describes the file format and how that relates to version number, classifier etc.
You can use file specs to upload or download from artifactory in a Jenkins Free Style job (simply check the Generic-Artifactory Integration) or a Jenkins Pipeline Job.
HTH,
Or

How to get a list of components and related assets out of Nexus3?

I am trying to export assets out of Sonatype Nexus3 so that I can refactor our software artifact persistence. Primarily I want to use Nexus3 as a mirror/grouping tool for disparate repositories and not push to it directly as we currently do. For this to work I need to extract all of our Docker, Maven, and NPM components/assets out of it. The problem is, Nexus3 uses blob stores for asset persistence and I want the raw assets out.
Docker images are easy. I can just walk each repository's Docker Registry API to get a list of images to pull and then push them to a new location.
For Maven artifacts, however (and I assume this applies to NPM artifacts as well), I am unable to export repositories using something like the Maven Wagon Plugin's copy goal or just about anything else because all solutions that I have seen rely on directory listing which has not been implemented in Nexus3.
If I was able to get a listing of all components and associated assets in the Nexus3 hosted repositories this would be tedious but good enough to script. Is there some CLI or API trick to extracting such info out of Nexus3? Is there a query I can run against the embedded OrientDB instance after logging into the Karaf OSGi console?
Looking for some pointers!
You might find some help in the answer here: Using the Nexus3 API how do I get a list of artifacts in a repository
I think this will get you where you need to be :)

Custom build tagging with Jenkins, Artifactory, Maven

I'm working on a strategy to tag builds in my continuous integration and delivery pipeline. When a fresh build is produced it then goes through multiple stages of testing; after each stage I would like to tag the build. My idea was to expand on the standard Maven system, so an example would be:
my-artifact-1.0-SNAPSHOT.jar
my-artifact-1.0-PASSED_TESTS_1.jar
my-artifact-1.0-PASSED_TESTS_2.jar
...
my-artifact-1.0-RELEASE.jar
As a build passes each stage its tag, which is part of its name/version, gets updated. Eventually, after all testing stages the build is tagged as releasable with RELEASE.
I'm using a Jenkins server and storing artifacts on Artifactory (non-Pro) in Maven repositories. My plan is to use separate Maven repositories for each tag type above, and to move an artifact from one to the other as its tag is updated.
My pseudo-code for the promotion looks like:
(1) Download artifacts that passed in Jenkins
(2) Text-replace the tag (maybe pom.xml as well?)
(3) Upload to new repository
(4) Update the associated Git commit with tag also
I'm not sure how to actually implement this cleanly - is this a common way of doing things? Am I missing something important? Can a Jenkins + Artifactory (non-Pro) + Maven (repos only, Gradle for builds) system accomplish this?
Thanks!
I can see where the idea of adding more metadata to the file names come. Usually, we don't have any other place to add this kind of information, except of the name. But with a proper artifact repository manager (as Artifactory), you don't need it anymore. You can attach any metadata to the artifacts in the repository manager, and then manipulate (e.g. promote) the artifacts based on those properties.
Here's a screencast showing what it takes to add this kind of metadata to the artifacts, and how to promote artifacts based on it.

How to preserve the timestamps generated for SNAPSHOT artifacts?

Currently a Maven build (inside Jenkins) is configured to deploy every artifact it builds (releases and snapshots) to a Nexus repository. Now I have to push some of those artifacts to application servers and thought of letting the target servers fetch them from Nexus - this is easy for releases but how can I reference the SNAPSHOT artifacts? Maven's deploy plugin adds a timestamp to make each artifact unique (which is good) but I couldn't find a way to get that generated timestamp for later use!
Quick Aside: I plan to use the promoted builds plugin to start a script on the target server(s) which then in turn ask Nexus for the new artifact to deploy.
Does anybody know how I can make Maven say the timestamp it generates? Or do I really have to parse the whole output for Uploaded: https://NEXUS_URL/content/repositories/snapshots/GROUP/ARTIFACT/VERSION/ARTIFACT-VERSION-TIMESTAMP-SUFFIX.TYPE?
As far as I know you do need to parse the output of the deploy goal.

Resources