Repositories

Dependencies are usually stored in some kind of repository. A repository has a layout that defines a pattern for the path of a versioned library module. Gradle knows, for example, the layout of a Maven repository. Ivy repositories can have customized layouts, and with Gradle, we can configure a customized layout. The repository can be accessible via the file system, HTTP, SSH, or other protocols.

We can declare several repository types in the Gradle build file. Gradle provides some preconfigured repositories, but it is also very easy to use a custom Maven or Ivy repository. We can also declare a simple file system repository to be used for resolving and finding dependencies. The following table shows the preconfigured and custom repositories we can use:

Repository type

Description

Maven repository

Maven layout repository on a remote computer or file system.

Maven central repository

Preconfigured Maven layout repository to search for dependencies in the Maven central repository.

Maven local repository

Preconfigured Maven repository that finds dependencies in the local Maven repository.

Ivy repository

Ivy repository that can be located on a local or remote computer.

Flat directory repository

Simple repository on the local file system of the computer or a network share.

We define a repository with the repositories() method. This method accepts a closure that is used to configure an org.gradle.api.artifacts.dsl.RepositoryHandler object.

Adding Maven repositories

A lot of Java projects use Maven as a build tool and for Maven's dependency management features. A Maven repository stores libraries with version information and metadata described in a descriptor XML file. The layout of a Maven repository is fixed and follows the pattern someroot/[organization]/[module]/[revision]/[module]-[revision].[ext]. The organization section is split into subfolders based on the dots used in the organization name. For example, if the organization name is org.gradle, an org folder with the subfolder gradle needs to be in the Maven repository. A JAR library with the organization name org.gradle, module name gradle-api, and revision 1.0 is resolved via the path someroot/org/gradle/gradle-api/1.0/gradle-api-1.0.jar.

The Maven central repository is located at http://repo1.maven.org/maven2 and contains a lot of libraries. Many open source projects deploy their artifacts to Maven's central repository. We can use the mavenCentral() method in the configuration closure for the repositories() method. The following example is a build file where we have defined the Maven central repository:

repositories {
    mavenCentral()
}

If we have used Maven before on our computer, there is a good chance we have a local Maven repository. Maven will use a hidden folder in our home directory to store downloaded dependency libraries. We can add this local Maven repository, with the method mavenLocal(), to the list of repositories. We can add the Maven local repository to our build file, as follows:

repositories {
    mavenLocal()
    mavenCentral()
}

Both the central and local Maven repositories are preconfigured Maven repositories. We can also add a custom repository that follows the Maven layout. For example, our company can have a Maven repository available via the intranet. We define the URL of the Maven repository with the maven() or mavenRepo() method.

The example build file uses both methods to add two new Maven repositories available through our intranet:

repositories {
    maven {
        // Name is optional. If not set url property is used
        name = 'Main Maven repository'
        url = 'http://intranet/repo'
    }

    mavenRepo(name: 'Snapshot repository', url: 'http://intranet/snapshots')
}

Both methods configure a repository via a combination of a closure and method arguments. Sometimes we must access a Maven repository that stores the metadata in descriptor XML files, but the actual JAR files are in a different location. To support this scenario, we must set the property artifactUrls and assign the addresses of the servers that store the JAR files:

repositories {
    maven {
        url: 'http://intranet/mvn'
        artifactUrls 'http://intranet/jars'
        artifactUrls 'http://intranet/snapshot-jars'
    }
}

To access a Maven repository with basic authentication, we can set the credentials when we define the repository:

repositories {
    maven(name: 'Secured repository') {
        credentials {
            username = 'username'
            password = 'password'
        }
        url = 'http://intranet/repo'
    }
}

It is not a good idea to store the username and password as plain text in the build file; this is because anyone can read our password, if stored in plain text. It is better if we define the properties in a file gradle.properties, in the Gradle user home directory, apply the correct security constraints on the property file, and use those properties in our build file:

repositories {
    maven(name: 'Secured repository') {
        credentials {
            // Define properties usernameSecuredRepo
            // and passwordSecuredRepo in
            // $USER_HOME/.gradle/gradle.properties
            username = usernameSecuredRepo
            password = passwordSecuredRepo
        }
        url = 'http://intranet/repo'
    }
}

Adding Ivy repositories

An Ivy repository has a customizable layout; this means that there is no single predefined layout as with a Maven repository. The default layout for an Ivy repository has the pattern someroot/[organization]/[module]/[revision]/[type]s/[artifact].[ext]. The name of the organization is not split into subfolders, as with the Maven layout. So, our module gradle with the organization name org.gradle and artifact gradle-api with revision 1.0 is resolved via the path someroot/org.gradle/gradle/1.0/jars/gradle-api.jar.

We use the same resources() method to configure an Ivy repository. We use the method ivy() to configure the settings for an Ivy repository. We define the URL of the repository and optionally, a name:

repositories {
    ivy(url: 'http://intranet/ivy-repo', name: 'Our repository')

    ivy {
        url = 'http://intranet/ivy-snapshots'
    }
}

If our Ivy repository has a Maven layout, we can set the layout property to maven. We can use the same property to define a custom layout for a repository. We define the patterns that are used to resolve the descriptor XML files and the actual library files.

The following table shows the different layout names we can use and the default patterns for the preconfigured layouts:

Layout name

Pattern Ivy descriptors

Pattern artifacts

gradle

someroot/[organization]/[module]/[revision]/ivy-[revision].xml

someroot/[organization]/[module]/[revision]/[artifact]-[revision](-[classifier])(.[ext])

maven

someroot/[organization]/[module]/[revision]/ivy-[revision].xml

someroot/[organization]/[module]/[revision]/[artifact]-[revision](-[classifier])(.[ext])

pattern

Custom

Custom

The example build file uses the preconfigured layout names gradle and maven and also a custom pattern:

repositories {
    ivy {
        url = 'http://intranet/ivy-snapshots'
        layout = 'maven'
    }

    ivy {
        url = 'http://intranet/repository'
        layout = 'gradle'
    }

    ivy {
        url = 'http://intranet/custom'
        layout('pattern') {
            // Pattern to resolve Ivy descriptor files.
            ivy '[module]/[revision]/ivy.xml'

            // Pattern to resolve files.
            artifact '[module]/[revision]/[artifact](.[ext])'
        }
    }
}

Instead of using the layout() method to define a custom pattern, we can use the methods ivyPattern() and artifactPattern() to define the patterns for the Ivy repository:

repositories {
    ivy {
        url = 'http://intranet/custom'
        ivyPatterns '[module]/[revision]/ivy.xml'
        artifactPatterns '[module]/[revision]/[artifact](.[ext])'
    }
}

To access an Ivy repository that is secured with basic authentication, we must pass our credentials. Just like with the secured Maven repository, it is best to store the username and password as properties in the file $USER_HOME/.gradle/gradle.properties:

repositories {
    ivy {
        credentials {
            username = usernameFromGradleProperties
            password = passwordFromGradleProperties
        }
        url = 'http://intranet/custom'
        ivyPatterns '[module]/[revision]/ivy.xml'
        artifactPatterns '[module]/[revision]/[artifact](.[ext])'
        artifactPatterns '[module]/[revision]/[artifact](.[ext])'
    }
}

Adding a local directory repository

To use a simple repository on the local file system or a network share mapped as local storage, we must use the flatDir() method. The flatDir() methods accepts arguments or a closure to configure the correct directory. We can assign a single directory or multiple directories.

Gradle will resolve files in the configured directory using the first match it finds with the following patterns:

  • [artifact]-[version].[ext]
  • [artifact]-[version]-[classifier].[ext]
  • [artifact].[ext]
  • [artifact]-[classifier].[ext]

The following example build file defines a flat directory repository:

repositories {
    flatDir(dir: '../lib', name: 'libs directory')

    flatDir {
        dirs '../project-files', '/volumes/shared-libs'
        name = 'All dependency directories'
   }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.221