api / org.gradle.api.artifacts.dsl

Package org.gradle.api.artifacts.dsl

Types

ArtifactHandler

interface ArtifactHandler

This class is for defining artifacts to be published and adding them to configurations. Creating publish artifacts does not mean to create an archive. What is created is a domain object which represents a file to be published and information on how it should be published (e.g. the name).

To create an publish artifact and assign it to a configuration you can use the following syntax:

<configurationName> <artifact-notation>, <artifact-notation> ... or <configurationName> <artifact-notation> { ... some code to configure the artifact }

The notation can be one of the following types:

  • PublishArtifact.
  • org.gradle.api.tasks.bundling.AbstractArchiveTask. The information for publishing the artifact is extracted from the archive task (e.g. name, extension, ...). The task will be executed if the artifact is required.
  • A org.gradle.api.provider.Provider of java.io.File, org.gradle.api.file.RegularFile or org.gradle.api.file.Directory. The information for publishing the artifact is extracted from the file or directory name. When the provider represents an output of a particular task, that task will be executed if the artifact is required.
  • java.io.File. The information for publishing the artifact is extracted from the file name.
  • java.util.Map. The map should contain a 'file' key. This is converted to an artifact as described above. You can also specify other properties of the artifact using entries in the map.

In each case, a ConfigurablePublishArtifact instance is created for the artifact, to allow artifact properties to be configured. You can also override the default values for artifact properties by using a closure to configure the properties of the artifact instance

Examples

An example showing how to associate an archive task with a configuration via the artifact handler. This way the archive can be published or referred in other projects via the configuration.

 configurations { //declaring new configuration that will be used to associate with artifacts schema } task schemaJar(type: Jar) { //some imaginary task that creates a jar artifact with some schema } //associating the task that produces the artifact with the configuration artifacts { //configuration name and the task: schema schemaJar } 

ComponentMetadataHandler

interface ComponentMetadataHandler

Allows the build to provide rules that modify the metadata of depended-on software components.

Possible uses of component metadata rules are:

  • Setting the status and status scheme of a component, overriding the value specified in the component descriptor.
  • Declaring whether or not a component is 'changing', thus impacting the cache behaviour of the component.

Example:

 dependencies { components { // Set the status and status scheme for every component belonging to a module in the group "org.foo" all { ComponentMetadataDetails details -> if (details.id.group == "org.foo") { def version = details.id.version // assuming status is last part of version string details.status = version.substring(version.lastIndexOf("-") + 1) details.statusScheme = ["bronze", "silver", "gold", "platinum"] } } // Treat all components in the module "org.foo:bar" as changing withModule("org.foo:bar") { ComponentMetadataDetails details -> details.changing = true } } } 

ComponentModuleMetadataHandler

interface ComponentModuleMetadataHandler

Allows to modify the metadata of depended-on software components.

Example:

 dependencies { modules { //Configuring component module metadata for the entire "google-collections" module, // declaring that legacy library was replaced with "guava". //This way, Gradle's conflict resolution can use this information and use "guava" // in case both libraries appear in the same dependency tree. module("com.google.collections:google-collections") { replacedBy("com.google.guava:guava") } } } 

DependencyConstraintHandler

interface DependencyConstraintHandler

A DependencyConstraintHandler is used to declare dependency constraints.

DependencyHandler

interface DependencyHandler

A DependencyHandler is used to declare dependencies. Dependencies are grouped into configurations (see org.gradle.api.artifacts.Configuration).

To declare a specific dependency for a configuration you can use the following syntax:

 dependencies { configurationName dependencyNotation1, dependencyNotation2, ... } 

Example shows a basic way of declaring dependencies.

 apply plugin: 'java' //so that we can use 'compile', 'testCompile' for dependencies dependencies { //for dependencies found in artifact repositories you can use //the group:name:version notation compile 'commons-lang:commons-lang:2.6' testCompile 'org.mockito:mockito:1.9.0-rc1' //map-style notation: compile group: 'com.google.code.guice', name: 'guice', version: '1.0' //declaring arbitrary files as dependencies compile files('hibernate.jar', 'libs/spring.jar') //putting all jars from 'libs' onto compile classpath compile fileTree('libs') } 
Advanced dependency configuration

To do some advanced configuration on a dependency when it is declared, you can additionally pass a configuration closure:

 dependencies { configurationName(dependencyNotation){ configStatement1 configStatement2 } } 
Examples of advanced dependency declaration including:
 apply plugin: 'java' //so that I can declare 'compile' dependencies dependencies { compile('org.hibernate:hibernate:3.1') { //in case of versions conflict '3.1' version of hibernate wins: force = true //excluding a particular transitive dependency: exclude module: 'cglib' //by artifact name exclude group: 'org.jmock' //by group exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group //disabling all transitive dependencies of this dependency transitive = false } } 
More examples of advanced configuration, useful when dependency module has multiple artifacts:
 apply plugin: 'java' //so that I can declare 'compile' dependencies dependencies { //configuring dependency to specific configuration of the module compile configuration: 'someConf', group: 'org.someOrg', name: 'someModule', version: '1.0' //configuring dependency on 'someLib' module compile(group: 'org.myorg', name: 'someLib', version:'1.0') { //explicitly adding the dependency artifact: artifact { //useful when some artifact properties unconventional name = 'someArtifact' //artifact name different than module name extension = 'someExt' type = 'someType' classifier = 'someClassifier' } } } 
Dependency notations

There are several supported dependency notations. These are described below. For each dependency declared this way, a Dependency object is created. You can use this object to query or further configure the dependency.

You can also always add instances of org.gradle.api.artifacts.Dependency directly:

configurationName <instance> External dependencies

There are two notations supported for declaring a dependency on an external module. One is a string notation formatted this way:

configurationName "group:name:version:classifier@extension"

The other is a map notation:

configurationName group: group, name: name, version: version, classifier: classifier, ext: extension

In both notations, all properties, except name, are optional.

External dependencies are represented by a .

 apply plugin: 'java' //so that we can use 'compile', 'testCompile' for dependencies dependencies { //for dependencies found in artifact repositories you can use //the string notation, e.g. group:name:version compile 'commons-lang:commons-lang:2.6' testCompile 'org.mockito:mockito:1.9.0-rc1' //map notation: compile group: 'com.google.code.guice', name: 'guice', version: '1.0' } 
Project dependencies

To add a project dependency, you use the following notation:

configurationName project(':someProject')

The notation project(':projectA') is similar to the syntax you use when configuring a projectA in a multi-module gradle project.

By default, when you declare dependency to projectA, you actually declare dependency to the 'default' configuration of the projectA. If you need to depend on a specific configuration of projectA, use map notation for projects:

configurationName project(path: ':projectA', configuration: 'someOtherConfiguration')

Project dependencies are represented using a org.gradle.api.artifacts.ProjectDependency.

File dependencies

You can also add a dependency using a org.gradle.api.file.FileCollection:

configurationName files('a file')
 apply plugin: 'java' //so that we can use 'compile', 'testCompile' for dependencies dependencies { //declaring arbitrary files as dependencies compile files('hibernate.jar', 'libs/spring.jar') //putting all jars from 'libs' onto compile classpath compile fileTree('libs') } 

File dependencies are represented using a org.gradle.api.artifacts.SelfResolvingDependency.

Dependencies to other configurations

You can add a dependency using a org.gradle.api.artifacts.Configuration.

When the configuration is from the same project as the target configuration, the target configuration is changed to extend from the provided configuration.

When the configuration is from a different project, a project dependency is added.

Gradle distribution specific dependencies

It is possible to depend on certain Gradle APIs or libraries that Gradle ships with. It is particularly useful for Gradle plugin development. Example:

 //Our Gradle plugin is written in groovy apply plugin: 'groovy' //now we can use the 'compile' configuration for declaring dependencies dependencies { //we will use the Groovy version that ships with Gradle: compile localGroovy() //our plugin requires Gradle API interfaces and classes to compile: compile gradleApi() //we will use the Gradle test-kit to test build logic: testCompile gradleTestKit() } 
Client module dependencies

To add a client module to a configuration you can use the notation:

configurationName module(moduleNotation) { module dependencies } 
The module notation is the same as the dependency notations described above, except that the classifier property is not available. Client modules are represented using a org.gradle.api.artifacts.ClientModule.

DependencyLockingHandler

interface DependencyLockingHandler

A DependencyLockingHandler manages the behaviour and configuration of dependency locking.

RepositoryHandler

interface RepositoryHandler : ArtifactRepositoryContainer

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