Thomas Lee
Thomas Lee

Reputation: 1027

Gradle Dependency loading from maven

I am new to gradle.

I have seen some examples about java dependency like the following example but my project will be simply a zip file. I just want to download the zip file.

apply plugin: 'java'
dependencies {
    compile 'commons-lang:commons-lang:2.6'
}

In the above example, it will automatically download the jar file. But it doesn't download my zip file if my maven repositories contains zip that mentioned in the pom.xml about that package.

Questions:

  1. What is the flow when depend on a maven repository? It will first read the pom.xml and then download the zip file?

  2. How to dynamically load the dependency? e.g 'commons-lang:commons-lang:2.6' will have dependency of 'commons-lang:en:1.0" in the pom.xml. How to make it automatically load and loop the dependency list?

Thanks all

I have tried the follwoing script but it gives me error on compile but I have apply the java plugin

My gradle file

apply plugin: 'java'
repositories {
    mavenLocal()
    maven {
        url "http://nexus.com/myrepo/"
    }
}
dependencies {
    compile 'com.a.b:projectA:2.0@zip'
}

I can run without problem that files downloaded are inside .m2

Question about the transitive dependency

I have the pom.xml like this. But it is unable to load the dependency one. It will directly go to the new pom.xml first or download zip directly if i mention sth like this?

   <dependencies> 
         <dependency>
              <groupId>com.a.b.c</groupId>
              <artifactId>base</artifactId>
              <version>1.2</version>
              <type>zip</type>
        </dependency>
   </dependencies>

Upvotes: 3

Views: 10540

Answers (1)

Rene Groeschke
Rene Groeschke

Reputation: 28653

  • When declaring a dependency and a maven repository, this maven repository will be used to resolve the artifact. this means that usually first the metadata is read and then the artifact will be downloaded. If no repository is declared the resolution will fail early.

Having a dependency notation like yours:

dependencies {
    compile 'commons-lang:commons-lang:2.6'
}

gradle resolves the default artifact of that dependency. If you want to resolve additional declared zip files from maven central, you have to use this notation

repositories{
    mavenCentral()
}

dependencies {
    compile 'commons-lang:commons-lang:2.6@zip'
}
  • As a default, the a dependency is transitive. This means, that if e.g 'commons-lang:commons-lang:2.6' has a dependency on 'commons-lang:en:1.0" in its pom.xml the commons-lang library (and again its transitive dependencies if any) is also resolved.

cheers,

René

Upvotes: 6

Related Questions