4

I have a specific use case for Continuous Integration with Jenkins in mind for this, but the answer could be a general Maven solution.

I would like to copy the latest version (could be a SNAPSHOT) of a Maven artifact to a local directory. Some more details:

  • I have a Jenkins build plan that builds a Java project using Maven, resulting in an artifact installed into the local Maven repo (mvn clean install).
  • In a separate build plan (which can't be triggered by the first one for separate reasons), I want to use the latest result of the first build plan and copy it into a local directory (from where I deploy it into a Docker container).

This means that a) the first build plan installs an artifact (could be a SNAPSHOT version) into the local Maven repo, and b) I don't know the exact version number of the latest artifact.

What I've tried is to use the Maven Dependency Plugin, but I haven't found a good solution for this yet:

mvn dependencies:copy -Dartifact=foogroup:artifact:1.2.3-SNAPSHOT:jar -DoutputDir=.

This copies version 1.2.3-SNAPSHOT of the specified artifact into the current directory, but the version number needs to be known in this case.

I've also tried to use the LATEST identifier as in

mvn dependencies:copy -Dartifact=foogroup:artifact:LATEST:jar -DoutputDir=.

but it only tries to resolve the artifact from the remote repos, not from my local repo on the same machine. Using this approach, I can get the latest artifact that someone uploaded, but not the one that was just built on the machine.

Is there a way to get the latest version from the local Maven repo, not from the remote one?

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • http://stackoverflow.com/questions/30571/how-do-i-tell-maven-to-use-the-latest-version-of-a-dependency for the context of using `LATEST`. Since the question is marked with Maven-3. – Naman Mar 20 '17 at 13:18
  • **Is there a way to get the latest version from the local Maven repo, not from the remote one?** If local repo means .m2 here, that is what Maven does. It fetches the specified version of artifact from .m2 of your local and in case its not there looks into the repositories specified in the pom.xml of project or settings.xml of maven. – Naman Mar 20 '17 at 13:21
  • @nullpointer - Thanks, but this answer applies to Maven in general. The `depenency:copy` tasks manages its own dependencies, and it looks like `LATEST` is supported there: http://stackoverflow.com/a/21314574/1228454 – nwinkler Mar 20 '17 at 13:27
  • Also - the `dependency:copy` goal directly goes to the remote repo if `LATEST` is specified (from what I've seen), it does not seem to check the local repo first. In my case, it always downloaded the one from the remote repo, never used the one from the local repo. Might be caused by the local artifact not having a timestamp. – nwinkler Mar 20 '17 at 13:30
  • What about writing the created version number (of your first build plan) to a file so that the second job can look it up? – J Fabian Meier Mar 20 '17 at 14:34
  • That could work - I'll think about that. It's kind of dirty and will only work when both builds are run on the same agent - but that's also true for the local Maven repo. Maybe the best solution is to upload the snapshot from the first build to the Snapshot repo and pull it from there in the second build. – nwinkler Mar 20 '17 at 14:48
  • 1
    If you want to be sure using the artifacts from the first build step there are two solutions. Using copy workspace by using Jenkins or in pipelines you could use stash/unstash artifacts. – khmarbaise Mar 21 '17 at 07:57

0 Answers0