Gradle Maven Publish Plugin: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 25: Line 25:


==Publications==
==Publications==
{{External|https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html}}


A Maven Publication is the representation of how Gradle publishes an artifact in Maven format. The Maven Publish plugin publications are defined by the [https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html MavenPublication] type. They are added to the <tt>publishing.publications</tt> as follows:
A Maven Publication is the representation of how Gradle publishes an artifact in Maven format. The Maven Publish plugin publications are defined by the [https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html MavenPublication] type. They are added to the <tt>publishing.publications</tt> as follows:

Revision as of 22:49, 15 May 2018

External

Internal

Overview

"maven-publish" is an newer alternative to publishing with Maven Plugin, using "publish" tasks instead of Maven Plugin "Upload" tasks. The plugin generates Maven metadata and publishes the generated metadata and the associated artifacts to Maven repositories. The concepts behind publishing with "maven-publish", and the plugin mechanics, are described in the Concepts and Plugin Mechanics below. A step by step procedure is available under the Procedure section.

The plugin is activated with:

apply plugin: 'maven-publish'

Concepts and Plugin Mechanics

Mechanics

The plugin creates an extension of type PublishingExtension named "publishing". The extension provides a container of named publications and a container of named repositories. The plugin does not have effect if no Maven publication is added to the publications container. If publications and repositories are present, the plugin processes those configurations and based on their content, creates POM generation tasks and publishing tasks that can be used to carry out the publishing action.

Publications

https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html

A Maven Publication is the representation of how Gradle publishes an artifact in Maven format. The Maven Publish plugin publications are defined by the MavenPublication type. They are added to the publishing.publications as follows:

publishing {
  publications {
    <publication-name>(MavenPublication) {
        from <component>
        artifact <task>
        artifact(source, configuration)
        pom(configuration)
    }
  }
}

The above syntax shows that an artifact can be specified by indicating the software component that should be published, or by defining the artifact in-line. If the artifact (or artifacts) are specified in-line, their details can be configured. The generated POM can be configured in-line as well.

Each publication must have an unique name within the project. All the artifacts associated with a publication share the groupID/artifactID/version project coordinates. A publication has a POM and at least one artifact, but it can have several artifacts (or example the class JAR and the source JAR).

Software Component Publication

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

Custom Artifact Publication

Custom artifacts can be included. They can be raw files or archives (JAR, ZIP). For each custom artifact it is possible to specify extension and classifier values to use for publications. Only one of the published artifact can have an empty classifier, all other must have unique classifier/extension combinations.

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

publishing {
  publications {
      multiart(MavenPublication) {
         ...
         artifact srcJar {
             classifier "sources"
         }
      }
}

Repositories

The Maven Publish plugin publications are defined by the MavenArtifactRepository type.

Relationship to pre-declared repositories, how can we use it?

POM Generation Tasks

Publishing Tasks

The plugin creates publishing tasks, based on declared publications and repositories, for the combination of each publication and repository, and makes them available for use. All publishing tasks are triggered by the overarching "publish" tasks, which publishes all publications produced by the project.

Procedure

Activate the plugin:

apply plugin: 'maven-publish'

Declare the publications.

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

Declared the target repositories. The example below assumes we want to publish locally into the local Maven repository, and also into a remote Maven repository:

repositories {
    maven {
        url "${url}"
        authentication {
            basic(BasicAuthentication)
        }
    }
}

Publish:

gradle publish

Examples

https://github.com/NovaOrdis/playground/tree/master/gradle/java-publishing