7
votes

I don't understand why this is so difficult:

In a script, I need to copy an artifact from nexus to a certain directory. Using the Nexus REST API I would have to specify the repository which I don't want to have to know about. So I tried getting the artifact with maven-dependency-plugin's get goal instead, which works well. (In that case I get it from a group on our nexus which includes both, releases and snapshots.)

However, I now have the artifact in my local repo and the same plugin's "copy" goal does not seem to be able to get that artifact out of there. Is it really necessary to descend into the .m2 folder and grab that jar with the unix cp command? Anybody ever copied artifacts from their local repos to other dirs before?

Alternatively, if someone can tell me how to get an artifact via the Nexus API without specifying the repo, that would work, too.

4
Which maven plugin did you try using? I would expect the dependency plugin to work for this: maven.apache.org/plugins/maven-dependency-plugin When you say "I now have the artifact in my local repo", do you mean, in that case, you ONLY have the artifact in your local repo-- that is, on the same machine?Keith
I used org.apache.maven.plugins:maven-dependency-plugin:2.8:get to get the artifact from nexus to my local repo in ~/.m2 - so now it is on my local machine (and on our local nexus, too).Cpt. Senkfuss
In the second step, I try to use org.apache.maven.plugins:maven-dependency-plugin:2.8:copy to get that artifact from ~/.m2/** to some other dir on my machine but the copy goal goes straight to central and doesn't look at my local repo at all (it doesn't look at our local nexus because that's not configured in my settings.xml, which might be weird in its own right, but we configure that in our company parent pom; it the first step, I can supply the local nexus' url, that's why that works)Cpt. Senkfuss

4 Answers

4
votes

Seems like the problem was the _maven.repositories file in conjunction with the particular maven setup at my company.

We don't put the info about the local repo in our settings.xml. It's all in the parent pom that all our projects use. But if you want to do some pure mvn-CLI magic you don't have the parent-pom so you have to provide the URL to the local repo yourself. This is possible with the dependecy:get goal, which is why I was able to download my artifact from our Nexus into my local repo.

When using copy, however, you can't specify a URL. But why would I want to? I just downloaded that artifact into my local repo, right?

That's where the _maven.repositories file comes into play. Even with the -o switch, maven3 consults that file, which specifies the original repo that the artifact came from. (thanks to the guys in this thread for posting their findings!). If it can't reach the repo, it will claim that your file isn't there. (Btw., this is not helpful imho. It should say something about the original repo not being reachable and that the file therefore won't be copied.)

This was the reason why copy didn't work for me.

Simply renaming that file does the trick.

I will have to investigate a cleaner solution to this, though.

To make things even more complicated, I couldn't use dependency:copy or dependency:copy-dependecies. For some reasons they require a pom, which I don't have in my usecase. What does work is org.apache.maven.plugins:maven-dependency-plugin:2.8:copy which I believe is supposed to be the same thing, but that's another story.

Thanks for your answers!

3
votes

Just tried this, and it worked for me:

mvn dependency:copy-dependencies -DincludeArtifactIds=jcharts -DincludeGroupIds=jcharts -DoutputDirectory=/tmp/

This copied the artifact jcharts:jcharts to /tmp/ It was in my local (and remote) repo when this was executed.

1
votes

If you are using Sonatype Nexus, you should consider disabling "Central" as outlined in their book.

Otherwise, per @Keith, dependency:copy-dependency will do what you are trying to do. dependency:get specially downloads from remote repositories, as stated in the documentation

You can also force Maven to not download from non-local repositories l by running it in offline mode: -o

Edit

You can also use Maven Wagon Plugin to copy the file from your local repository to an arbitrary directory. This shouldn't require a pom (but you may need to provide the full URL to the jar).

0
votes

I needed to be able to specify the group ID, artifact ID and version (and packaging), this worked for me:

mvn dependency:copy \
  -Dartifact=org.openmrs.web:openmrs-webapp:2.2.0-SNAPSHOT:war \
  -DoutputDirectory=/tmp