The Ivy Publish Plugin
The Ivy Publish Plugin provides the ability to publish build artifacts in the Apache Ivy format, usually to a repository for consumption by other builds or projects. What is published is one or more artifacts created by the build, and an Ivy module descriptor (normally ivy.xml
) that describes the artifacts and the dependencies of the artifacts, if any.
A published Ivy module can be consumed by Gradle (see Declaring Dependencies) and other tools that understand the Ivy format. You can learn about the fundamentals of publishing in Publishing Overview.
Usage
To use the Ivy Publish Plugin, include the following in your build script:
plugins {
`ivy-publish`
}
plugins {
id 'ivy-publish'
}
The Ivy Publish Plugin uses an extension on the project named publishing
of type PublishingExtension. This extension provides a container of named publications and a container of named repositories. The Ivy Publish Plugin works with IvyPublication publications and IvyArtifactRepository repositories.
Tasks
generateDescriptorFileForPubNamePublication
— GenerateIvyDescriptor-
Creates an Ivy descriptor file for the publication named PubName, populating the known metadata such as project name, project version, and the dependencies. The default location for the descriptor file is build/publications/$pubName/ivy.xml.
publishPubNamePublicationToRepoNameRepository
— PublishToIvyRepository-
Publishes the PubName publication to the repository named RepoName. If you have a repository definition without an explicit name, RepoName will be "Ivy".
publish
-
Depends on: All
publishPubNamePublicationToRepoNameRepository
tasksAn aggregate task that publishes all defined publications to all defined repositories.
Publications
This plugin provides publications of type IvyPublication. To learn how to define and use publications, see the section on basic publishing.
There are four main things you can configure in an Ivy publication:
-
A component — via IvyPublication.from(org.gradle.api.component.SoftwareComponent).
-
Custom artifacts — via the IvyPublication.artifact(java.lang.Object) method. See IvyArtifact for the available configuration options for custom Ivy artifacts.
-
Standard metadata like
module
,organisation
andrevision
. -
Other contents of the module descriptor — via IvyPublication.descriptor(org.gradle.api.Action).
You can see all of these in action in the complete publishing example. The API documentation for IvyPublication
has additional code samples.
Identity values for the published project
The generated Ivy module descriptor file contains an <info>
element that identifies the module. The default identity values are derived from the following:
-
organisation
- Project.getGroup() -
module
- Project.getName() -
revision
- Project.getVersion() -
status
- Project.getStatus() -
branch
- (not set)
Overriding the default identity values is easy: simply specify the organisation
, module
or revision
properties when configuring the IvyPublication. status
and branch
can be set via the descriptor
property — see IvyModuleDescriptorSpec.
The descriptor
property can also be used to add additional custom elements as children of the <info>
element, like so:
publishing {
publications {
create<IvyPublication>("ivy") {
organisation = "org.gradle.sample"
module = "project1-sample"
revision = "1.1"
descriptor.status = "milestone"
descriptor.branch = "testing"
descriptor.extraInfo("http://my.namespace", "myElement", "Some value")
from(components["java"])
}
}
}
publishing {
publications {
ivy(IvyPublication) {
organisation = 'org.gradle.sample'
module = 'project1-sample'
revision = '1.1'
descriptor.status = 'milestone'
descriptor.branch = 'testing'
descriptor.extraInfo 'http://my.namespace', 'myElement', 'Some value'
from components.java
}
}
}
Certain repositories are not able to handle all supported characters. For example, the : character cannot be used as an identifier when publishing to a filesystem-backed repository on Windows.
|
Gradle will handle any valid Unicode character for organisation
, module
and revision
(as well as the artifact’s name
, extension
and classifier
). The only values that are explicitly prohibited are \
, /
and any ISO control character. The supplied values are validated early during publication.
Customizing the generated module descriptor
At times, the module descriptor file generated from the project information will need to be tweaked before publishing. The Ivy Publish Plugin provides a DSL for that purpose. Please see IvyModuleDescriptorSpec in the DSL Reference for the complete documentation of available properties and methods.
The following sample shows how to use the most common aspects of the DSL:
publications {
create<IvyPublication>("ivyCustom") {
descriptor {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
author {
name = "Jane Doe"
url = "http://example.com/users/jane"
}
description {
text = "A concise description of my library"
homepage = "http://www.example.com/library"
}
}
versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}
}
}
publications {
ivyCustom(IvyPublication) {
descriptor {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
author {
name = 'Jane Doe'
url = 'http://example.com/users/jane'
}
description {
text = 'A concise description of my library'
homepage = 'http://www.example.com/library'
}
}
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
In this example we are simply adding a 'description' element to the generated Ivy dependency descriptor, but this hook allows you to modify any aspect of the generated descriptor. For example, you could replace the version range for a dependency with the actual version used to produce the build.
You can also add arbitrary XML to the descriptor file via IvyModuleDescriptorSpec.withXml(org.gradle.api.Action), but you cannot use it to modify any part of the module identifier (organisation, module, revision).
It is possible to modify the descriptor in such a way that it is no longer a valid Ivy module descriptor, so care must be taken when using this feature. |
Customizing dependencies versions
Two strategies are supported for publishing dependencies:
- Declared versions (default)
-
This strategy publishes the versions that are defined by the build script author with the dependency declarations in the
dependencies
block. Any other kind of processing, for example through a rule changing the resolved version, will not be taken into account for the publication. - Resolved versions
-
This strategy publishes the versions that were resolved during the build, possibly by applying resolution rules and automatic conflict resolution. This has the advantage that the published versions correspond to the ones the published artifact was tested against.
Example use cases for resolved versions:
-
A project uses dynamic versions for dependencies but prefers exposing the resolved version for a given release to its consumers.
-
In combination with dependency locking, you want to publish the locked versions.
-
A project leverages the rich versions constraints of Gradle, which have a lossy conversion to Ivy. Instead of relying on the conversion, it publishes the resolved versions.
This is done by using the versionMapping
DSL method which allows to configure the VersionMappingStrategy:
publications {
create<IvyPublication>("ivyCustom") {
versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}
}
}
publications {
ivyCustom(IvyPublication) {
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
In the example above, Gradle will use the versions resolved on the runtimeClasspath
for dependencies declared in api
, which are mapped to the compile
configuration of Ivy.
Gradle will also use the versions resolved on the runtimeClasspath
for dependencies declared in implementation
, which are mapped to the runtime
configuration of Ivy.
fromResolutionResult()
indicates that Gradle should use the default classpath of a variant and runtimeClasspath
is the default classpath of java-runtime
.
Repositories
This plugin provides repositories of type IvyArtifactRepository. To learn how to define and use repositories for publishing, see the section on basic publishing.
Here’s a simple example of defining a publishing repository:
publishing {
repositories {
ivy {
// change to point to your repo, e.g. http://my.org/repo
url = uri(layout.buildDirectory.dir("repo"))
}
}
}
publishing {
repositories {
ivy {
// change to point to your repo, e.g. http://my.org/repo
url = layout.buildDirectory.dir("repo")
}
}
}
The two main things you will want to configure are the repository’s:
-
URL (required)
-
Name (optional)
You can define multiple repositories as long as they have unique names within the build script. You may also declare one (and only one) repository without a name. That repository will take on an implicit name of "Ivy".
You can also configure any authentication details that are required to connect to the repository. See IvyArtifactRepository for more details.
Complete example
The following example demonstrates publishing with a multi-project build. Each project publishes a Java component configured to also build and publish Javadoc and source code artifacts. The descriptor file is customized to include the project description for each project.
rootProject.name = "ivy-publish-java"
include("project1", "project2")
plugins {
`kotlin-dsl`
}
repositories {
gradlePluginPortal()
}
plugins {
id("java-library")
id("ivy-publish")
}
version = "1.0"
group = "org.gradle.sample"
repositories {
mavenCentral()
}
java {
withJavadocJar()
withSourcesJar()
}
publishing {
repositories {
ivy {
// change to point to your repo, e.g. http://my.org/repo
url = uri("${rootProject.buildDir}/repo")
}
}
publications {
create<IvyPublication>("ivy") {
from(components["java"])
descriptor.description {
text = providers.provider({ description })
}
}
}
}
plugins {
id("myproject.publishing-conventions")
}
description = "The first project"
dependencies {
implementation("junit:junit:4.13")
implementation(project(":project2"))
}
plugins {
id("myproject.publishing-conventions")
}
description = "The second project"
dependencies {
implementation("commons-collections:commons-collections:3.2.2")
}
rootProject.name = 'ivy-publish-java'
include 'project1', 'project2'
plugins {
id 'groovy-gradle-plugin'
}
plugins {
id 'java-library'
id 'ivy-publish'
}
version = '1.0'
group = 'org.gradle.sample'
repositories {
mavenCentral()
}
java {
withJavadocJar()
withSourcesJar()
}
publishing {
repositories {
ivy {
// change to point to your repo, e.g. http://my.org/repo
url = "${rootProject.buildDir}/repo"
}
}
publications {
ivy(IvyPublication) {
from components.java
descriptor.description {
text = providers.provider({ description })
}
}
}
}
plugins {
id 'myproject.publishing-conventions'
}
description = 'The first project'
dependencies {
implementation 'junit:junit:4.13'
implementation project(':project2')
}
plugins {
id 'myproject.publishing-conventions'
}
description = 'The second project'
dependencies {
implementation 'commons-collections:commons-collections:3.2.2'
}
The result is that the following artifacts will be published for each project:
-
The Gradle Module Metadata file:
project1-1.0.module
. -
The Ivy module metadata file:
ivy-1.0.xml
. -
The primary JAR artifact for the Java component:
project1-1.0.jar
. -
The Javadoc and sources JAR artifacts of the Java component (because we configured
withJavadocJar()
andwithSourcesJar()
):project1-1.0-javadoc.jar
,project1-1.0-source.jar
.