Tutorial :Is it possible to explicity tell maven to download and install an artifact to the local repository?



Question:

Yes I read Utility for downloading artifacts from maven repo without mvn/poms and other related questions, but I don't want to install the file manually. Actually I want to have something like a wget for maven, which fetches an artifact (with dependencies) and puts it somewhere or installs it in the local repository. Is the maybe a plugin available which does this?


Solution:1

There is the dependency:get goal, as described at the dependency plugin manual.

This featured was requested at this jira ticket.

But I tried it with Maven 2.1.0 and it didn't work.


Solution:2

I'm not sure if there is a Maven plugin to handle this, but it is fairly simple to use the Maven Ant tasks for this purpose. You don't need to have Maven or a POM file, just Ant and this build file:

<?xml version="1.0" encoding="UTF-8"?>  <project name="mvn-get" default="get" basedir=".">      <property name="maven.ant.tasks.jar" value="${ant.home}/lib/maven-ant-tasks-2.011.jar" />      <property name="maven.ant.tasks.bootstrap.location" value="http://apache.inetbridge.net/maven/binaries/maven-ant-tasks-2.0.11.jar" />      <available property="maven.ant.tasks.jar.exists" file="${maven.ant.tasks.jar}" />        <!-- This will download the "latest version" of the maven-ant-tasks if needed -->      <target name="bootstrap_maven" unless="maven.ant.tasks.jar.exists">          <get src="${maven.ant.tasks.bootstrap.location}" dest="${maven.ant.tasks.jar}" />      </target>        <!-- This will initialize all the maven ant tasks and download the requested artifact and its dependencies -->      <target name="get" depends="bootstrap_maven" xmlns:artifact="urn:maven-artifact-ant">          <path id="maven.ant.tasks.classpath" path="${maven.ant.tasks.jar}" />          <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant" classpathref="maven.ant.tasks.classpath" />          <condition property="maven.repo.local" value="${maven.repo.local}" else="${user.home}/.m2/repository">              <isset property="maven.repo.local" />          </condition>          <echo>maven.repo.local=${maven.repo.local}</echo>          <artifact:localRepository id="local.repository" path="${maven.repo.local}" />          <artifact:dependencies pathId="build.classpath" sourcesFilesetId="sources.id">              <dependency groupId="${mvn.get.groupId}" artifactId="${mvn.get.artifactId}" version="${mvn.get.version}"/>              <localRepository refid="local.repository" />          </artifact:dependencies>      </target>    </project>  

Using a command line like the following will do what you want:

ant -Dmvn.get.groupId=commons-httpclient -Dmvn.get.artifactId=commons-httpclient -Dmvn.get.version=3.1 -Dmaven.repo.local=.  

The inspiration for this came from this excellent blog post.


Solution:3

You mean a way without adding a dependency to the pom.xml?

Because by adding a dependency to your pom.xml the maven dependency management automatically retrieves the artifact from the repositories you specified and installs it to the local repository.

<dependencies>    <dependency>      <groupId>foo</groupId>      <artifactId>foo</artifactId>      <version>1.0.1</version>    </dependency>  </dependencies>  


Solution:4

I would investigate using the maven dependency plugin in combination with the maven install plugin.


Solution:5

I use the following shell script to install a Commons Logging replacement, but in this case I already have the jars in hand:

  #! /bin/sh    mvn install:install-file \      -DgroupId=commons-logging \      -DartifactId=commons-logging \      -Dversion=99.0-does-not-exist \      -Dpackaging=jar \      -DcreateChecksum=true \      -DpomFile=commons-logging-99.0-does-not-exist.pom \      -Dfile=commons-logging-99.0-does-not-exist.jar    mvn install:install-file \      -DgroupId=commons-logging \      -DartifactId=commons-logging-api \      -Dversion=99.0-does-not-exist \      -Dpackaging=jar \      -DcreateChecksum=true \      -DpomFile=commons-logging-api-99.0-does-not-exist.pom \      -Dfile=commons-logging-api-99.0-does-not-exist.jar  

When you say "local repository" do you mean your own, as an individual, i.e., the one in ~/.m2 on unix, or do you mean your company repository? It's a different question. For the former, I'm with jitter; just add the entry to your pom. For the latter, check the repository server's docs for how to get a copy of central.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »