Interface MavenPublication
- All Superinterfaces:
Named, Publication
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.groupartifactId-project.nameversion-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.
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(org.gradle.api.Action) method
of the POM returned via the getPom() method, or directly by an action (or closure) passed into pom(org.gradle.api.Action).
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
classifier "sources"
}
publishing {
publications {
myPublication(MavenPublication) {
from components.java
artifact sourceJar
pom.withXml {
asNode().appendNode('description', 'A demonstration of Maven POM customization')
}
}
}
}
- Since:
- 1.4
-
Nested Class Summary
Nested classes/interfaces inherited from interface Named
Named.Namer -
Method Summary
Modifier and TypeMethodDescriptionCreates a customMavenArtifactto be included in the publication.artifact(Object source, Action<? super MavenArtifact> config) Creates anMavenArtifactto be included in the publication, which is configured by the associated action.voidfrom(SoftwareComponent component) Provides the software component that should be published.Returns the artifactId for this publication.Returns the complete set of artifacts for this publication.Returns the groupId for this publication.getPom()The POM that will be published.Returns the version for this publication.voidConfigures the POM that will be published.voidsetArtifactId(String artifactId) Sets the artifactId for this publication.voidsetArtifacts(Iterable<?> sources) Clears any previously added artifacts fromgetArtifacts()and creates artifacts from the specified sources.voidsetGroupId(String groupId) Sets the groupId for this publication.voidsetVersion(String version) Sets the version for this publication.
-
Method Details
-
getPom
-
pom
-
from
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.
apply plugin: "java" apply plugin: "maven-publish" publishing { publications { maven(MavenPublication) { from components.java } } }- Parameters:
component- The software component to publish.
-
artifact
Creates a customMavenArtifactto be included in the publication. Theartifactmethod can take a variety of input:- A
PublishArtifactinstance. Extension and classifier values are taken from the wrapped instance. - An
AbstractArchiveTaskinstance. Extension and classifier values are taken from the wrapped instance. - Anything that can be resolved to a
Filevia theProject.file(Object)method. Extension and classifier values are interpolated from the file name. - A
Mapthat 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.
apply plugin: "maven-publish" task sourceJar(type: Jar) { classifier "sources" } 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' } } }- Parameters:
source- The source of the artifact content.
- A
-
artifact
Creates anMavenArtifactto 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 perartifact(Object). The createdMavenArtifactis 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 "sources" } 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" } } } }- Parameters:
source- The source of the artifact.config- An action to configure the values of the constructedMavenArtifact.
-
setArtifacts
Clears any previously added artifacts fromgetArtifacts()and creates artifacts from the specified sources. Each supplied source is interpreted as perartifact(Object). 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 "sources" } publishing { publications { maven(MavenPublication) { from components.java artifacts = ["my-custom-jar.jar", sourceJar] } } }- Parameters:
sources- The set of artifacts for this publication.
-
getArtifacts
MavenArtifactSet getArtifacts()Returns the complete set of artifacts for this publication.- Returns:
- the artifacts.
-
getGroupId
String getGroupId()Returns the groupId for this publication. -
setGroupId
Sets the groupId for this publication. -
getArtifactId
String getArtifactId()Returns the artifactId for this publication. -
setArtifactId
Sets the artifactId for this publication. -
getVersion
String getVersion()Returns the version for this publication. -
setVersion
Sets the version for this publication.
-