DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Download Jar Files and Dependencies


For example we need download jar-file 'http-builder' and all its dependencies to folder "target".

Ivy

First download apache-ivy-x.x.x-bin.tar.gz from Apache Ivy. Extract ivy-x.x.x.jar to ~/warez folder. Then create a ivy definition file and run it:

cat << EOF > ivy.xml
<ivy-module version="2.0">
    <info organisation="org.demo" module="demo"/>
    <configurations defaultconfmapping="default"/>
    <dependencies>
        <!-- replace the following line with the definitions from [MVNRepository](https://mvnrepository.com/) -->
        <dependency org="org.codehaus.groovy.modules.http-builder" name="http-builder" rev="0.7"/>
    </dependencies>
</ivy-module>
EOF
java -jar ~/warez/ivy-2.4.0.jar -ivy ivy.xml -retrieve "target/[artifact].[ext]"

Ref:

http://stackoverflow.com/questions/7908090/downloading-all-maven-dependencies-to-a-directory-not-in-repository

Gradle

Install gradle with sdkman, then run:

cat << EOF > build.gradle
apply plugin: 'java'

task(getjars, type: Copy) {
    into "target"
    from configurations.runtime
}

repositories {
    jcenter()
}

dependencies {
    compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7'
}
EOF
gradle getjars

Note: jcenter() is a superset of mavenCentral(), and faster than it. See Android buildscript repositories: jcenter VS mavencentral for details.

Maven

Download jars of 'mysql' and 'druid', and all their dependencies to subfolder 'jars' (maven will create this folder if not exists):

cat << EOF > pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.znbt</groupId>
  <artifactId>dtester</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.6</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.29</version>
    </dependency>
  </dependencies>
</project>
EOF
mvn dependency:copy-dependencies -DoutputDirectory=jars

That's to say, downloading jars with maven is a 2-step process:

  1. add dependency definitions between <dependencies> tags of template file 'pom.xml':

    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.znbt</groupId>
      <artifactId>dtester</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <dependencies>
    
      </dependencies>
    </project>
    
  2. In the folder of 'pom.xml', run command mvn dependency:copy-dependencies -DoutputDirectory=jars.

Ref: http://stackoverflow.com/questions/7742252/how-to-use-maven-pom-to-download-jar-files-only-to-a-specific-directory



Published

Feb 27, 2017

Last Updated

Jan 2, 2019

Category

Tech

Tags

  • dependency 3
  • ivy 19
  • jar 2

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor