MavenPublication - Gradle DSL Version 9.4.1 (original) (raw)

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) {

}

} }

The default Maven POM identifying attributes are mapped as follows:

For certain common use cases, it's often sufficient to specify the component to publish, and nothing more (MavenPublication.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 MavenPublication.artifact(java.lang.Object) and [MavenPublication.artifact(java.lang.Object, org.gradle.api.Action)](../dsl/org.gradle.api.publish.maven.MavenPublication.html#org.gradle.api.publish.maven.MavenPublication:artifact%28java.lang.Object, org.gradle.api.Action%29) methods. You can also completely replace the set of published artifacts using MavenPublication.setArtifacts(java.lang.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 MavenPublication.getPom()method or directly by an action (or closure) passed into MavenPublication.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.

plugins { id 'java' id 'maven-publish' }

task sourceJar(type: Jar) { from sourceSets.main.allJava archiveClassifier = "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/" } } } } }

Properties

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

Methods

Method Description
artifact(source) Creates a custom MavenArtifact to be included in the publication. The artifact method can take a variety of input:
[artifact](org.gradle.api.publish.maven.MavenPublication.html#org.gradle.api.publish.maven.MavenPublication:artifact%28java.lang.Object, org.gradle.api.Action%29)(source, config) 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(java.lang.Object). 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) Provides the software component that should be published.
pom(configure) 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) Clears any previously added artifacts from MavenPublication.getArtifacts() and creates artifacts from the specified sources. Each supplied source is interpreted as per MavenPublication.artifact(java.lang.Object). For example, to exclude the dependencies declared by a component and instead use a custom set of artifacts:
suppressAllPomMetadataWarnings() Silences all the compatibility warnings for the Maven publication. Warnings are emitted when Gradle features are used that cannot be mapped completely to Maven POM.
suppressPomMetadataWarningsFor(variantName) Silences the compatibility warnings for the Maven publication for the specified variant. Warnings are emitted when Gradle features are used that cannot be mapped completely to Maven POM.
versionMapping(configureAction) Configures the version mapping strategy. For example, to use resolved versions for runtime dependencies:

Script blocks

No script blocks

Property details

The artifactId for this publication.

The complete set of artifacts for this publication.

The groupId for this publication.

The POM that will be published.

The version for this publication.

Method details

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

The following example demonstrates the addition of various custom artifacts.

plugins { id 'maven-publish' }

task sourceJar(type: Jar) { archiveClassifier = "sources" }

publishing { publications { maven(MavenPublication) { artifact sourceJar artifact 'my-file-name.jar' artifact source: sourceJar, classifier: 'src', extension: 'zip' } } }

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(java.lang.Object). 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.

plugins { id 'maven-publish' }

task sourceJar(type: Jar) { archiveClassifier = "sources" }

publishing { publications { maven(MavenPublication) { artifact(sourceJar) {

    classifier = "src"
    extension = "zip"
  }
  artifact("my-docs-file.htm") {
    classifier = "documentation"
    extension = "html"
  }
}

} }

Provides the software component that should be published.

Currently 3 types of component are supported: 'components.java' (added by the JavaPlugin), 'components.web' (added by the WarPlugin) and `components.javaPlatform` (added by the JavaPlatformPlugin). 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.

plugins { id 'java' id 'maven-publish' }

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

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.

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

plugins { id 'java' id 'maven-publish' }

task sourceJar(type: Jar) { archiveClassifier = "sources" }

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

void suppressAllPomMetadataWarnings()

Silences all the compatibility warnings for the Maven publication. Warnings are emitted when Gradle features are used that cannot be mapped completely to Maven POM.

void suppressPomMetadataWarningsFor(String variantName)

Silences the compatibility warnings for the Maven publication for the specified variant. Warnings are emitted when Gradle features are used that cannot be mapped completely to Maven POM.

Configures the version mapping strategy. For example, to use resolved versions for runtime dependencies:

plugins { id 'java' id 'maven-publish' }

publishing { publications { maven(MavenPublication) { from components.java versionMapping { usage('java-runtime'){ fromResolutionResult() } } } } }