Repositories

satya - 3/18/2019, 10:32:25 AM

RepositoryHandler is documented here, which is represented by "repositories" script

RepositoryHandler is documented here, which is represented by "repositories" script

satya - 3/18/2019, 10:32:48 AM

Basic definition

A RepositoryHandler manages a set of repositories, allowing repositories to be defined and queried.

satya - 3/18/2019, 10:35:06 AM

Available repositories


flatdir //a set of local directories
google() //google maven repo
gradlePluginPortal() //gradles repos
ivy
Jcenter
maven
mavenCentral
mavenLocal

satya - 3/18/2019, 10:38:08 AM

Examples


repositories {
    mavenCentral artifactUrls: 
             ["http://www.mycompany.com/artifacts1", 
               "http://www.mycompany.com/artifacts2"]

    mavenCentral name: "nonDefaultName", 
        artifactUrls: ["http://www.mycompany.com/artifacts1"]
}

repositories {
    mavenCentral()
}

repositories {
  jcenter {
    artifactUrls = 
       ["http://www.mycompany.com/artifacts1",
        "http://www.mycompany.com/artifacts2"]
  }
  jcenter {
    name = "nonDefaultName"
    artifactUrls = ["http://www.mycompany.com/artifacts1"]
  }
}

repositories {
    flatDir name: 'libs', dirs: "$projectDir/libs"
    flatDir dirs: ["$projectDir/libs1", "$projectDir/libs2"]
}

satya - 3/18/2019, 10:39:41 AM

JCenter and Maven uses the following object to configure themselves

JCenter and Maven uses the following object to configure themselves

satya - 3/18/2019, 11:09:25 AM

Consider this


repositories {
  //This creates a repo pointing to the maven central
  mavenCentral()

  //This creates another maven repo
  //Sets its URL
  maven {
     url "some-url"
     //The following simply overwrites the one before
     //So one is enough
     setUrl("some-other-url");
  }

  //This creates a third maven repo
  //if you want at a different url
  maven {
     url "some-new-url"
  }

}//end of repos

satya - 3/18/2019, 11:09:54 AM

So each entry under repositories creates a new repository

So each entry under repositories creates a new repository

satya - 3/18/2019, 11:10:18 AM

More than one repository of a particular type is ok

More than one repository of a particular type is ok

satya - 3/18/2019, 11:15:33 AM

How do you know what methods are available to configure a given repo like Maven?

Go to the repos doc page above at Gradle and see what Java object is represented by that particular repo. Java API will tell you how to configure that particular repo.

satya - 3/18/2019, 11:16:30 AM

For example here is the MavenRepo java object: MavenArtifactRepository

For example here is the MavenRepo java object: MavenArtifactRepository

satya - 3/18/2019, 11:18:48 AM

Here is an example to exercise this


buildscript {

       ext {
              var1 = 'value1'
       }

       repositories {
        maven {
            println "\nOutput from first maven"
            println "***********************"
            println it
            println delegate
            println owner

            println this
            //The following is wrong, the last will take affect
              url ?url1'
              setUrl('url2')
        }

        //you can do this instead
        maven {
            println "\nOutput from second maven"
            println "***********************"

              println it
              println delegate
              println owner
              println this
              url = "blah again" // :)

        }
       }
}

 

task Target {
       doLast {
              println "done"
              println it
              println delegate
       }
}

satya - 3/18/2019, 11:19:56 AM

This will show that the two maven repos are different

> Configure project :

Output from first maven

***********************

org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_De

corated@c4664c

org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_De

corated@c4664c

DynamicObject for org.gradle.api.internal.artifacts.repositories.DefaultMavenArt

ifactRepository_Decorated@c4664c

root project 'Sample 3, Test Property Scope'

Output from second maven

***********************

org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_De

corated@12a713e

org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_De

corated@12a713e

DynamicObject for org.gradle.api.internal.artifacts.repositories.DefaultMavenArt

ifactRepository_Decorated@12a713e

root project 'Sample 3, Test Property Scope'

> Task :Target

done

task ':Target'

task ':Target'

satya - 3/18/2019, 11:25:00 AM

So, reading the repositories gradle syntax


//A method call on Project
//Delegate is the RepositoryHandler
//(You know that by looking at Project Signature)

repositories {

   //A method call on RepositoryHandler
   //Creates a Repo represented by
   //MavenArtifactRepository
   Jcenter()

   Jcenter {
     //MavenArtifactRepository
     //is avaiale as the delegate
     //of this closure
     //So all methods are fair game
     //on that delegated object
   }
}

satya - 3/18/2019, 11:26:58 AM

One thing to notice is that ....

the method setUrl(url) is not a plural. It only allows 1 URL per repo. This is perhaps to allow for multiple credentials and other assets of that repo.