Article updated on

How to use maven just to copy dependencies

Maven is one of the best java applications used to build and deploy Java Projects but sometimes we may just need to use it to manage or download dependencies which is probably its best feature. In this example I'm using Linux Ubuntu to download several jars to a folder.

1 - First make sure you have installed maven.

From the console type: mvn -version

2 - Create a pom.xml file in the parent folder for the folder you wish to download all the libraries

Use this pom.xml as template.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>TestMaven</groupId>
    <artifactId>TestMaven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <org.springframework.version>3.2.0.RELEASE</org.springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <!-- hardcode values, or use properties, depending on what you want
                                        to do -->
                                    <groupId>TestMaven</groupId>
                                    <artifactId>TestMaven</artifactId>
                                    <version>0.0.1-SNAPSHOT</version>
                                    <type>[ packaging ]</type>
                                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                            <!-- other configurations here -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

* In this example I'm downloading some core Spring libraries.

3 - Execute the command to download the libraries.

Create a folder in which you want to download all the libraries specified in the pom.xml in this example I created a folder called lib.

Execute the following command in the console.

mvn dependency:copy-dependencies -DoutputDirectory=yourfoldername

4 - Verify that the libraries are there.

5 - Configure your proxy (If needed)

Go to your maven installation folder  /conf/settings.xml (create it if it doesn't exist) and use the following as template.

<settings>
  <proxies>
   <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host>PUT-YOUR-PROXY-NAME</host>
      <port>PUT-YOUR-PORT-NUMBER</port>
      <username>proxyuser</username>
      <password>somepassword</password>
      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

 


Notes

  • I am using the maven-dependency-plugin
  • You can integrate this example with Eclipse, I does not produce any syntax erroros nevertheless, it is not possible to run it with the eclipse maven plug-in so far.