Downloading all maven dependencies to a directory

2019-01-04 16:14发布

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?

6条回答
走好不送
2楼-- · 2019-01-04 16:47

I found the next command

mvn dependency:copy-dependencies -Dclassifier=sources

here maven.apache.org

查看更多
我命由我不由天
3楼-- · 2019-01-04 17:01

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.

查看更多
疯言疯语
4楼-- · 2019-01-04 17:04

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.

查看更多
Emotional °昔
5楼-- · 2019-01-04 17:06

Create an ivy.xml file to list your project's dependencies:

<ivy-module version="2.0">
    <info organisation="org.demo" module="demo"/>
    <configurations defaultconfmapping="default"/>
    <dependencies>
        <dependency org="commons-lang" name="commons-lang" rev="2.6"/>
        ..
    </dependencies>
</ivy-module>

Downloading these jars and their transitive dependencies can now be done in one of two ways.

Command-line

Ivy can be run as a command line program. The following example will download into a local "lib" directory:

java -jar ivy-2.2.0.jar -ivy ivy.xml -retrieve "lib/[artifact].[ext]"

ANT build

ivy is best used when integrated into your ANT build. The following example target downloads the jars into a local lib directory and generates a HTML report for dependency analysis.

<target name="retrieve" description="Retrieve dependencies locally">
    <ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"/>

    <ivy:report todir="${report.dir}" graph="false"/>
</target>
查看更多
Luminary・发光体
6楼-- · 2019-01-04 17:07

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..

查看更多
smile是对你的礼貌
7楼-- · 2019-01-04 17:08

maven dependency plugin can potentially solve your problem.

If you have a pom created with all your project dependencies specified, all you would need to do is run mvn dependency:copy-dependencies and you will find target/dependencies folder filled with all the dependencies, including transitive.

查看更多
登录 后发表回答