Chapter 9. Artifact Basics

This chapter is currently under construction.

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

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

9.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 9.1. Definition of a configuration

build.gradle

configurations {
    compile
}

To access a configuration:

Example 9.2. Accessing a configuration

build.gradle

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

To configure a configuration:

Example 9.3. Configuration of a configuration

build.gradle

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

9.2. Repositories

Artifacts are stored in repositories.

To use maven central repository:

Example 9.4. Usage of Maven central repository

build.gradle

repositories {
    mavenCentral()
}

To use a local directory:

Example 9.5. Usage of a local directory

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 9.6. Accessing a repository

build.gradle

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

To configure a repository:

Example 9.7. Configuration of a repository

build.gradle

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

9.3. External dependencies

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

Example 9.8. Definition of an external dependency

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 9.9. Usage of external dependency of a configuration

build.gradle

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

Output of gradle -q listJars

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

9.4. Artifact publishing

TBD

9.5. API

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