A simple command line to download a remote maven2 artifact to the local repository? - maven-2

I have a library that I distribute using maven 2. The typical user of this library doesn't use maven to build their applications, but is likely somewhat familiar with maven and probably has it installed.
I'd like to document a "simple" one line command they can use to download my library's artifacts to their local ~/.m2/repository without requiring that they set up a pom.xml to do it.
I thought there was a way to do this, but I can't seem to find it after looking through the install:install-file and dependency plugin documentation. I tried things like:
mvn install:install-file -DrepositoryId=java.net -Durl=http://download.java.net/maven/2/ -Dfile=robo-guice-0.4-20091121.174618-1.jar -DpomFile=robo-guice-0.4-20091121.174618-1.pom -DgroupId=robo-guice -DartifactId=robo-guice -Dversion=0.4-SNAPSHOT -Dpackaging=jar
but I think I'm barking up the wrong tree since it appears that the install plugin is used to copy locally built files into the local repository, rather than download remote artifacts into the local repository.
This is the artifact I'd like to install: http://download.java.net/maven/2/robo-guice/robo-guice/0.4-SNAPSHOT/
Is this possible using maven?

Since version 2.1 of the Maven Dependency Plugin, there is a dependency:get goal for this purpose. To make sure you are using the right version of the plugin, you'll need to use the "fully qualified name":
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get \
-DrepoUrl=http://download.java.net/maven/2/ \
-Dartifact=robo-guice:robo-guice:0.4-SNAPSHOT

Give them a trivial pom with these jars listed as dependencies and instructions to run:
mvn dependency:go-offline
This will pull the dependencies to the local repo.
A more direct solution is dependency:get, but it's a lot of arguments to type:
mvn dependency:get -DrepoUrl=something -Dartifact=group:artifact:version

As of version 2.4 of the Maven Dependency Plugin, you can also define a target destination for the artifact by using the -Ddest flag. It should point to a filename (not a directory) for the destination artifact. See the parameter page for additional parameters that can be used
mvn org.apache.maven.plugins:maven-dependency-plugin:2.4:get \
-DremoteRepositories=http://download.java.net/maven/2 \
-Dartifact=robo-guice:robo-guice:0.4-SNAPSHOT \
-Ddest=c:\temp\robo-guice.jar

Related

maven repository and dependencies download

I've used standard maven command mvn clean install and according to logs (in the same console window) dependencies were loaded and artifacts were installed to my dedicated directory for maven repository P:\.m2\repository. Maven build process succeeded.
I'm using maven 2.2.1
Here is the problem. Repository directory is empty. What I'm missing?
Thanks.
Some reasons:
Someone deleted the folder (or it's content)
Maven installed the files in a different place
I suggest to run mvn again with the option -X. Running mvn clean -X should be enough to see the paths which it uses to locate dependencies (install will download many more files but we need to see only one).
Note: P:\ sounds like a network drive. Even if this is your home folder (i.e. not shared with other people), this isn't a very good idea since it will cause a lot of network traffic and make your builds slow and brittle (in case of network problems).
Did you change the repository location in your m2_install_dir/conf/settings.xml?
If not, try to locate your files in something like:
C:\Documents & Settings\your_username.m2
or C:\Users\your_username.m2
I think you are using something like gitbash and its stripping off the backslashes in your repo value. run maven install with -X. check if you have a directory called P:\.m2repository

How can I create local Maven repository for my own libraries?

I need to have my own libraries in Maven repository, and I only need these my own libraries (about 2-7 libs). Is it possible to copy these jars to some local folder and then use that as repository in Maven?
Assuming these libraries only need to be available for your local build, you can just install them in to your repo from the cmd line:
mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> -Dversion=<snapshot> -Dfile=<path_to_your_jar_file> -Dpackaging=jar -DgeneratePom=true
You can use any artefact/group/version you likr - but these then need to be used in your pom when listing the dependency

Forcing Maven2 to download source jars to local repository

I have my company dependencies (including non-FOSS third party stuff) in a Nexus repository, including source jars (downloaded with mvn dependency:resolve), where available.
I would like to have the source jars (and javadoc jars) download to my local repository (~/.m2/repository) so that I can go "offline".
I tried various combinations of mvn dependency:resolve dependency:go-offline -Dclassifier=sources, but it does not seem to copy them to my local repository.
Thanks.
Did you try:
mvn dependency:sources

It's possible to put binary files on nexus repository?

At my work all development uses Java technology, and we use Nexus to manage our Maven repositories. But for a new project, the build requires dll and exe artifacts. Is it possible to put those windows binary files into a Nexus repository? Is there some plugin to make this simpler? Is what I'm trying to do crazy?
I use Nexus to store all the binary dependencies that I download from the internet.
You can upload the files using the Nexus GUI or use the Maven command line as follows:
mvn deploy:deploy-file \
-Durl=$REPO_URL \
-DrepositoryId=$REPO_ID \
-DgroupId=org.apache.maven \
-DartifactId=maven \
-Dversion=2.2.1 \
-Dpackaging=zip \
-Dfile=maven.zip
This will generate the POM for your zip package automatically.
To retrieve dependencies, you can just navigate to the Nexus URL, or use a generic dependency manager tool like ivy:
java -jar ivy.jar -dependency org.apache.maven maven 2.2.1 -retrieve [artifact].[ext]

Maven install-file won't generate pom.xml

I've installed some third party jars to my repository using the following command:
mvn install:install-file -Dfile=/home/anotherCoder/Downloads/nifty-1.0.jar -DgroupId=nifty-gui -DartifactId=nifty-gui -Dversion=1.0 -Dpackaging=jar
However, once I do mvn compile, maven complains that there is no pom file in the repository and attempts to download it, but can't cause it is not published at any remote repository.
Here is the exact message from maven:
Downloading: http://repo1.maven.org/maven2/nifty-gui/nifty-gui/1.0/nifty-gui-1.0.pom
[INFO] Unable to find resource 'nifty-gui:nifty-gui:pom:1.0' in repository central (http://repo1.maven.org/maven2)
So how do I get maven to generate a pom file for that jar and put it in my local repository?
You tell it to! :-)
mvn install:install-file
-Dfile=/home/anotherCoder/Downloads/nifty-1.0.jar
-DgroupId=nifty-gui
-DartifactId=nifty-gui
-Dversion=1.0
-Dpackaging=jar
-DgeneratePom=true
(Command placed on multiple lines so you can easily see the last parameter.)
Nice, huh? In the future you can go to a plug-in's documentation, view its goals, and you can see all the parameters it accepts. For example, the install-file goal.
Edit:
Regarding the question of the default behavior of the generatePom flag, the documentation indicates it defaults to true, and the code appears to support that. However, using Maven 2.0.9 with the maven-install-plugin version 2.2 (both versions are slightly out of date), it does not generate a POM. So, perhaps incrementing the version(s) will allow the default to work.
> touch DeleteMe.jar
> mvn install:install-file -DgroupId=Delete -DartifactId=Me -Dversion=0.0.0 -Dpackaging=jar -Dfile=DeleteMe.jar
...
[INFO] BUILD SUCCESSFUL
...
> ls ~/.m2/repository/Delete/Me/0.0.0/
Me-0.0.0.jar
(No generated POM.)
The install:install-file goal has an optional parameter generatePom (since version 2.1) that allows to:
Generate a minimal POM for the
artifact if none is supplied via the
parameter pomFile.
Defaults to
true if there is no existing POM in
the local repository yet.
This parameter defaults to true since version 2.3 (and false in 2.1, 2.2). So if you're using a version of the install plugin prior to 2.3, you'll have to pass the parameter in the command.
Just in case, the syntax to explicitly use the version 2.3 of the install plugin would be:
mvn org.apache.maven.plugins:maven-install-plugin:2.3:install-file \
-Dfile=/home/anotherCoder/Downloads/nifty-1.0.jar -DgroupId=nifty-gui \
-DartifactId=nifty-gui -Dversion=1.0 -Dpackaging=jar
I had the same issue I think as you, I had a shell script using the install:install-file goal like this:
mvn -o install:install-file -e
-DgroupId=org.jfree.jcommon
-DartifactId=jcommon
-Dversion=1.0.15
-Dpackaging=jar
-Dfile=jcommon-1.0.15.jar
Couple of things to note:
artifactId cannot contain '.' ... not sure why but the install would fail if this contained '.'
Running the above command only generated a pom with maven 3.x. By adding the following arguments, I was able to get the jar's to be copied:
-DgeneratePom=true -DupdateReleaseInfo=true
Well and in case your third party library really is "nifty gui" all you need to do is to add the nifty maven repository to your pom.xml:
<repositories>
<repository>
<id>nifty-maven-repo.sourceforge.net</id>
<url>http://nifty-gui.sourceforge.net/nifty-maven-repo</url>
</repository>
</repositories>
and your maven project will automatically download nifty :D
PS: I know that this was not your actual question but it might help with nifty integration :)