# MavenPublication

API Documentation: | `MavenPublication`

Note: This class is incubating and may change in a future version of Gradle.

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 (`MavenPublication.from()`. 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 `MavenPublication.artifact()` and `MavenPublication.artifact()` methods. You can also completely replace the set of published artifacts using `MavenPublication.setArtifacts()`. Together, these methods give you full control over what artifacts will be published.

For any other tweaks to the publication, it is possible to modify the generated POM prior to publication. This is done using the `MavenPom.withXml()` method of the POM returned via the `MavenPublication.getPom()` method, or directly by an action (or closure) passed into `MavenPublication.pom()`.

### Example of publishing a java module with a source artifact and custom POM description

    apply plugin: "java"
    apply plugin: "maven-publish"

    task sourceJar(type: Jar) {
      from sourceSets.main.allJava
    }

    publishing {
      publications {
        myPublication(MavenPublication) {
          from components.java
          artifact sourceJar {
            classifier "source"
          }
          pom.withXml {
            asNode().appendNode('description', 'A demonstration of Maven POM customization')
          }
        }
      }
    }

### Properties

Property | Description
`artifactId` | Incubating The artifactId for this publication.
`artifacts` | Incubating The complete set of artifacts for this publication.
`groupId` | Incubating The groupId for this publication.
`pom` | Incubating The POM that will be published.
`version` | Incubating The version for this publication.

### Methods

Method | Description
`artifact(source)` | Incubating Creates a custom `MavenArtifact` to be included in the publication. The `artifact` method can take a variety of input:
`artifact(source, config)` | Incubating Creates an `MavenArtifact` to be included in the publication, which is configured by the associated action. The first parameter is used to create a custom artifact and add it to the publication, as per `MavenPublication.artifact()`. The created `MavenArtifact` is then configured using the supplied action, which can override the extension or classifier of the artifact. This method also accepts the configure action as a closure argument, by type coercion.
`from(component)` | Incubating Provides the software component that should be published.
`pom(configure)` | Incubating Configures the POM that will be published. The supplied action will be executed against the `MavenPublication.getPom()` result. This method also accepts a closure argument, by type coercion.
`setArtifacts(sources)` | Incubating Clears any previously added artifacts from `MavenPublication.getArtifacts()` and creates artifacts from the specified sources. Each supplied source is interpreted as per `MavenPublication.artifact()`. For example, to exclude the dependencies declared by a component and instead use a custom set of artifacts:

### Script blocks

No script blocks

### Property details

#### `String` `artifactId`

Note: This property is incubating and may change in a future version of Gradle.

The artifactId for this publication.

#### `MavenArtifactSet` `artifacts`

Note: This property is incubating and may change in a future version of Gradle.

The complete set of artifacts for this publication.

#### `String` `groupId`

Note: This property is incubating and may change in a future version of Gradle.

The groupId for this publication.

#### `MavenPom` `pom` (read-only)

Note: This property is incubating and may change in a future version of Gradle.

The POM that will be published.

#### `String` `version`

Note: This property is incubating and may change in a future version of Gradle.

The version for this publication.

### Method details

#### `MavenArtifact` `artifact`(`Object` source)

Note: This method is incubating and may change in a future version of Gradle.

Creates a custom `MavenArtifact` to be included in the publication. The `artifact` method can take a variety of input:

- A `PublishArtifact` instance. Extension and classifier values are taken from the wrapped instance.
- An `AbstractArchiveTask` instance. Extension and classifier values are taken from the wrapped instance.
- Anything that can be resolved to a `File` via the `Project.file()` method. Extension and classifier values are interpolated from the file name.
- A `Map` that contains a 'source' entry that can be resolved as any of the other input types, including file. This map can contain a 'classifier' and an 'extension' entry to further configure the constructed artifact.

The following example demonstrates the addition of various custom artifacts.

    apply plugin: "maven-publish"

    task sourceJar(type: Jar) {
      classifier "source"
    }

    publishing {
      publications {
        maven(MavenPublication) {
          artifact sourceJar // Publish the output of the sourceJar task
          artifact 'my-file-name.jar' // Publish a file created outside of the build
          artifact source: sourceJar, classifier: 'src', extension: 'zip'
        }
      }
    }

#### `MavenArtifact` `artifact`(`Object` source, ``Action`<? super `MavenArtifact`>` config)

Note: This method is incubating and may change in a future version of Gradle.

Creates an `MavenArtifact` to be included in the publication, which is configured by the associated action. The first parameter is used to create a custom artifact and add it to the publication, as per `MavenPublication.artifact()`. The created `MavenArtifact` is then configured using the supplied action, which can override the extension or classifier of the artifact. This method also accepts the configure action as a closure argument, by type coercion.

    apply plugin: "maven-publish"

    task sourceJar(type: Jar) {
      classifier "source"
    }

    publishing {
      publications {
        maven(MavenPublication) {
          artifact sourceJar {
            // These values will be used instead of the values from the task. The task values will not be updated.
            classifier "src"
            extension "zip"
          }
          artifact("my-docs-file.htm") {
            classifier "documentation"
            extension "html"
          }
        }
      }
    }

#### `void` `from`(`SoftwareComponent` component)

Note: This method is incubating and may change in a future version of Gradle.

Provides the software component that should be published.

- Any artifacts declared by the component will be included in the publication.
- The dependencies declared by the component will be included in the published meta-data.

Currently 2 types of component are supported: 'components.java' (added by the JavaPlugin) and 'components.web' (added by the WarPlugin). For any individual MavenPublication, only a single component can be provided in this way. The following example demonstrates how to publish the 'java' component to a Maven repository.

    apply plugin: "java"
    apply plugin: "maven-publish"

    publishing {
      publications {
        maven(MavenPublication) {
          from components.java
        }
      }
    }

#### `void` `pom`(``Action`<? super `MavenPom`>` configure)

Note: This method is incubating and may change in a future version of Gradle.

Configures the POM that will be published. The supplied action will be executed against the `MavenPublication.getPom()` result. This method also accepts a closure argument, by type coercion.

#### `void` `setArtifacts`(``Iterable`<?>` sources)

Note: This method is incubating and may change in a future version of Gradle.

Clears any previously added artifacts from `MavenPublication.getArtifacts()` and creates artifacts from the specified sources. Each supplied source is interpreted as per `MavenPublication.artifact()`. For example, to exclude the dependencies declared by a component and instead use a custom set of artifacts:

    apply plugin: "java"
    apply plugin: "maven-publish"

    task sourceJar(type: Jar) {
      classifier "source"
    }

    publishing {
      publications {
        maven(MavenPublication) {
          from components.java
          artifacts = ["my-custom-jar.jar", sourceJar]
        }
      }
    }