Chapter 5. Artifact Basics

This chapter is currently under construction.

For all the details of artifact handling see Chapter 22, Artifact Management.

This chapter introduces some of the basics of artifact handling in Gradle.

5.1. Artifact Configurations

Artifacts are grouped into configurations. A configuration is simply a set of files with a name. You can use them to declare the external dependencies your project has, or to declare the artifacts which your project publishes.

To define a configuration:

Example 5.1.  build.gradle

configurations {
    compile
}

To access a configuration:

Example 5.2.  build.gradle

println configurations.compile.name
println configurations['compile'].name

To configure a configuration:

Example 5.3.  build.gradle

configurations {
    compile {
        description = 'compile classpath'
        transitive = true
    }
    runtime {
        extendsFrom compile
    }
}
configurations.compile {
    description = 'compile classpath'
}

5.2. Repositories

Artifacts are stored in repositories.

To use maven central repository:

Example 5.4.  build.gradle

repositories {
    mavenCentral()
}

To use a local directory:

Example 5.5.  build.gradle

repositories {
    flatDir name: 'localRepository', dirs: 'lib'
}

You can also use any Ivy resolver. You can have multiple repositories.

To access a repository:

Example 5.6.  build.gradle

println repositories.localRepository.name
    println repositories['localRepository'].name

To configure a repository:

Example 5.7.  build.gradle

repositories {
    localRepository {
        addArtifactPattern(file('lib').absolutePath + '/[name]/[revision]/[name]-[revision].[ext]')
    }
}
repositories.localRepository {
    addArtifactPattern(file('lib').absolutePath + '/[name]/[revision]/[name]-[revision].[ext]')
}

5.3. External Dependencies

To define an external dependency, you add a dependency to a configuration:

Example 5.8.  build.gradle

configurations {
    compile
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
}

group and version are optional

TBD - configuring an external dependency

To use the external dependencies of a configuration:

Example 5.9.  build.gradle

task listJars << {
    configurations.compile.each { File file -> println file.name }
}

Example 5.10. Output of gradle -q listJars

> gradle -q listJars
commons-collections-3.2.jar

5.4. Artifact Publishing

TBD

5.5. API

Configurations are contained in a ConfigurationContainer . Each configuration implements the Configuration .