chubbsondubs
chubbsondubs

Reputation: 38852

Downloading all maven dependencies to a directory NOT in repository?

I started to convert my project to maven because I needed to use a library that was distributed in binary form over maven only, but after banging my head against the wall on it for far too long I've decided to stop hurting myself and just use Ant. I'd like to just have maven download the jar and all of its transitive dependencies into a directory of my choosing so I can just check them into my SCM as I normally enjoy and be a blissful developer once again.
Any ideas how to do that easily?

Upvotes: 156

Views: 225821

Answers (7)

Simone
Simone

Reputation: 1

Based on Patrik Polak's answer, based on user, based on Raghuram here's the same script for windows users.

CD /D %~dp0

IF "%1" EQU "" goto:EOF
IF "%2" EQU "" goto:EOF
IF "%3" EQU "" goto:EOF

SET groupId=%1
SET artifactId=%2
SET version=%3

echo ^<project^>                                      > pom.xml
echo   ^<modelVersion^>4.0.0^</modelVersion^>         >> pom.xml
echo   ^<groupId^>com.temp.temp^</groupId^>           >> pom.xml
echo   ^<artifactId^>temp^</artifactId^>              >> pom.xml
echo   ^<packaging^>jar^</packaging^>                 >> pom.xml
echo   ^<version^>0.0.0^</version^>                   >> pom.xml
echo   ^<dependencies^>                               >> pom.xml
echo     ^<dependency^>                               >> pom.xml
echo       ^<groupId^>%groupId%^</groupId^>           >> pom.xml
echo       ^<artifactId^>%artifactId%^</artifactId^>  >> pom.xml
echo       ^<version^>%version%^</version^>           >> pom.xml
echo     ^</dependency^>                              >> pom.xml
echo   ^</dependencies^>                              >> pom.xml
echo   ^<build^>                                      >> pom.xml
echo     ^<plugins^>                                  >> pom.xml
echo     ^</plugins^>                                 >> pom.xml
echo   ^</build^>                                     >> pom.xml
echo ^</project^>                                     >> pom.xml


mkdir .\%groupId%-%artifactId%-%version%-jars

call  mvn dependency:copy-dependencies -DoutputDirectory=.\%groupId%-%artifactId%-%version%-jars

del pom.xml

pause

Upvotes: 0

Patrik Polak
Patrik Polak

Reputation: 51

My simple script based on user based on Raghuram.

getmvndep.sh

#!/bin/bash
groupId=$1
artifactId=$2
version=$3
echo "<project>"                                                      > pom.xml
echo "  <modelVersion>4.0.0</modelVersion>"                          >> pom.xml
echo "  <groupId>com.temp.temp</groupId>"                            >> pom.xml
echo "  <artifactId>temp</artifactId>"                               >> pom.xml
echo "  <packaging>jar</packaging>"                                  >> pom.xml
echo "  <version>0.0.0</version>"                                    >> pom.xml
echo "  <dependencies>"                                              >> pom.xml
echo "    <dependency>"                                              >> pom.xml
echo  "     <groupId>${groupId}</groupId>"                           >> pom.xml
echo  "     <artifactId>${artifactId}</artifactId>"                  >> pom.xml
echo  "     <version>${version}</version>"                           >> pom.xml
echo "    </dependency>"                                             >> pom.xml
echo "  </dependencies>"                                             >> pom.xml
echo "  <build>"                                                     >> pom.xml
echo "    <plugins>"                                                 >> pom.xml
echo "    </plugins>"                                                >> pom.xml
echo "  </build>"                                                    >> pom.xml
echo "</project>"                                                    >> pom.xml
mvn dependency:copy-dependencies -DoutputDirectory="./libs/${version}"
rm pom.xml

Upvotes: 5

Raghuram
Raghuram

Reputation: 52665

The maven dependency plugin can potentially solve your problem.

If you have a pom with all your project dependencies specified, all you would need to do is run

mvn dependency:copy-dependencies

and you will find the target/dependencies folder filled with all the dependencies, including transitive.

Adding Gustavo's answer from below: To download the dependency sources, you can use

mvn dependency:copy-dependencies -Dclassifier=sources

(via Apache Maven Dependency Plugin doc).

Upvotes: 308

Baked Inhalf
Baked Inhalf

Reputation: 3735

I finally figured out a how to use Maven. From within Eclipse, create a new Maven project.

Download Maven, extract the archive, add the /bin folder to path.

Validate install from command-line by running mvn -v (will print version and java install path)

Change to the project root folder (where pom.xml is located) and run:

mvn dependency:copy-dependencies

All jar-files are downloaded to /target/dependency.

To set another output directory:

mvn dependency:copy-dependencies -DoutputDirectory="c:\temp"

Now it's possible to re-use this Maven-project for all dependency downloads by altering the pom.xml

Add jars to java project by build path -> configure build path -> libraries -> add JARs..

Upvotes: 17

MagGGG
MagGGG

Reputation: 20976

Please check if you have some config files in ${MAVEN_HOME}/conf directory like settings.xml. Those files overrides settings from .m2 folder and because of that, repository folder from .m2 might not be visible or discarded.

Upvotes: 3

Gustavo
Gustavo

Reputation: 795

I found the next command

mvn dependency:copy-dependencies -Dclassifier=sources

here maven.apache.org

Upvotes: 7

Evandro Coan
Evandro Coan

Reputation: 9475

Based on @Raghuram answer, I find a tutorial on Copying project dependencies, Just:

  1. Open your project pom.xml file and find this:

    <project>
      [...]
      <build>
        <plugins>
          ...
        </plugins>
      </build>
      [...]
    </project>
    
  2. Than replace the <plugins> ... </plugins> with:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    
  3. And call maven within the command line mvn dependency:copy-dependencies

After it finishes, it will create the folder target/dependency within all the jar's dependencies on the current directory where the pom.xml lives.

Upvotes: 9

Related Questions