api / org.gradle.api.publish / Publication

Publication

interface Publication : Named

A publication is a description of a consumable representation of one or more artifacts, and possibly associated metadata.

Since
1.3

Inherited Functions

getName

abstract fun getName(): String

The object's name.

Must be constant for the life of the object.

Inheritors

IvyPublication

interface IvyPublication : Publication

A IvyPublication is the representation/configuration of how Gradle should publish something in Ivy format, to an Ivy repository. You directly add a named Ivy Publication the project's publishing.publications container by providing IvyPublication as the type.

 publishing { publications { myPublicationName(IvyPublication) { // Configure the publication here } } } 

The Ivy module identifying attributes of the publication are mapped as follows:

  • module - project.name
  • organisation - project.group
  • revision - project.version
  • status - project.status

For certain common use cases, it's often sufficient to specify the component to publish, using (#from(org.gradle.api.component.SoftwareComponent). The published component is used to determine which artifacts to publish, and which configurations and dependencies should be listed in the generated ivy descriptor file.

You can add configurations to the generated ivy descriptor file, by supplying a Closure to the #configurations(org.gradle.api.Action) method.

To add additional artifacts to the set published, use the #artifact(Object) and #artifact(Object, org.gradle.api.Action) methods. You can also completely replace the set of published artifacts using #setArtifacts(Iterable). Together, these methods give you full control over the artifacts to be published.

In addition, IvyModuleDescriptorSpec provides configuration methods to customize licenses, authors, and the description to be published in the Ivy module descriptor.

For any other tweaks to the publication, it is possible to modify the generated Ivy descriptor file prior to publication. This is done using the IvyModuleDescriptorSpec#withXml(org.gradle.api.Action) method, normally via a Closure passed to the #descriptor(org.gradle.api.Action) method.

Example of publishing a java component with an added source jar and custom module description
 apply plugin: "java" apply plugin: "ivy-publish" task sourceJar(type: Jar) { from sourceSets.main.allJava } publishing { publications { myPublication(IvyPublication) { from components.java artifact(sourceJar) { type "source" extension "src.jar" conf "runtime" } descriptor { license { name = "Custom License" } author { name = "Custom Name" } description { text = "Custom Description" } } } } } 

MavenPublication

interface MavenPublication : Publication

A MavenPublication is the representation/configuration of how Gradle should publish something in Maven format. You directly add a named Maven Publication the project's publishing.publications container by providing MavenPublication as the type.

 publishing { publications { myPublicationName(MavenPublication) { // Configure the publication here } } } 
The default Maven POM identifying attributes are mapped as follows:
  • groupId - project.group
  • artifactId - project.name
  • version - project.version

For certain common use cases, it's often sufficient to specify the component to publish, and nothing more (#from(org.gradle.api.component.SoftwareComponent). The published component is used to determine which artifacts to publish, and which dependencies should be listed in the generated POM file.

To add additional artifacts to the set published, use the #artifact(Object) and #artifact(Object, org.gradle.api.Action) methods. You can also completely replace the set of published artifacts using #setArtifacts(Iterable). Together, these methods give you full control over what artifacts will be published.

To customize the metadata published in the generated POM, set properties, e.g. MavenPom#getDescription(), on the POM returned via the #getPom() method or directly by an action (or closure) passed into #pom(org.gradle.api.Action). As a last resort, it is possible to modify the generated POM using the MavenPom#withXml(org.gradle.api.Action) method.

Example of publishing a Java module with a source artifact and a customized POM
 apply plugin: "java" apply plugin: "maven-publish" task sourceJar(type: Jar) { from sourceSets.main.allJava classifier "sources" } publishing { publications { myPublication(MavenPublication) { from components.java artifact sourceJar pom { name = "Demo" description = "A demonstration of Maven POM customization" url = "http://www.example.com/project" licenses { license { name = "The Apache License, Version 2.0" url = "http://www.apache.org/licenses/LICENSE-2.0.txt" } } developers { developer { id = "johnd" name = "John Doe" email = "john.doe@example.com" } } scm { connection = "scm:svn:http://subversion.example.com/svn/project/trunk/" developerConnection = "scm:svn:https://subversion.example.com/svn/project/trunk/" url = "http://subversion.example.com/svn/project/trunk/" } } } } }