Using archive task artifacts

In the following example build file, we define a new publication with the name publishJar, and we define the output of the jar archive task as an artifact:

apply plugin: 'maven-publish'
apply plugin: 'java'

// Configuration block for publishing
// artifacts from the project.
publishing {

  // Define publications with what
  // needs to be published.
  publications {

    // Name of this publication
    // is publishJar.
    publishJar(MavenPublication) {

      // Use output of jar task
      // as the artifact for
      // the publication.
      artifact jar

      // Alternatively we can use
      // a Map notation:
      // artifact source: jar
    }

  }
}

Next, we will run the tasks task and, in the output, we will be able to see newly generated tasks for publishing this publication:

$ gradle tasks
...
Publishing tasks
----------------
generatePomFileForPublishJarPublication - Generates the Maven POM file for publication 'publishJar'.
publish - Publishes all publications produced by this project.
publishPublishJarPublicationToMavenLocal - Publishes Maven publication 'publishJar' to the local Maven repository.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
...
BUILD SUCCESSFUL

Total time: 4.215 secs

Notice the two extra tasks, generatePomFileForPublishJarPublication and publishPublishJarPublicationToMavenLocal. The name of the publication, publishJar, is used for the two tasks. Gradle uses the generatePomFileFor<publicationName>Publication pattern for a task to generate a POM for a publication. The task pattern to publish a publication to the local Maven repository is publish<publicationName>PublicationToMavenLocal. Later in this chapter, we will see how we can add other repositories. We cannot yet invoke the tasks because we also need to set the group and version project properties, but we will cover this in the section about generating a POM file. We can now focus on defining the artifacts for a publication in this section.

We are not restricted to one artifact for a publication; we can add more by invoking the artifact method multiple times. Or, we can use the artifacts property to assign multiple artifacts. It is important that each artifact should have unique classifier and extension property values for a single publication. Gradle will check this before we can invoke any tasks, so we immediately get an error message when the artifacts don't have a unique combination of classifier and extensions property values.

In the following example build file, we add two extra artifacts to our publication with the artifact method:

apply plugin: 'maven-publish'
apply plugin: 'java'

task sourcesJar(type: Jar) {
  from sourceSets.main.allJava
  classifier = 'sources'
}

task javadocJar(type: Jar) {
  from javadoc
}

publishing {

  publications {

    publishJar(MavenPublication) {

      artifact jar

      artifact sourcesJar

      artifact javadocJar {
        // Each artifact must have
        // a unique classifier.
        // We can set the classifier
        // via the task as in sourcesJar
        // or here in the artifact configuration.
        classifier = 'javadoc'
      }

      // Or with a Map notation we
      // can write:
      // artifact source: javadocJar, classifier: 'javadoc'

    }

  }
}

Instead of using the artifact method, we can also use the artifacts property and assign multiple artifacts. Each of the artifacts we assign must have a unique combination of classifier and extension property values. In the next example build file, we will use the same artifacts as in the previous example but, this time, we will assign them to the artifacts property:

apply plugin: 'maven-publish'
apply plugin: 'java'

task sourcesJar(type: Jar) {
  from sourceSets.main.allJava
  classifier = 'sources'
}

task javadocJar(type: Jar) {
  from javadoc
  classifier = 'javadoc'
}

publishing {

  publications {

    publishJar(MavenPublication) {

      // Use artifacts property to
      // define the artifacts.
      // The classifier for each of
      // these artifacts must be
      // unique.
      artifacts = [
        jar,
        sourcesJar,
        javaDocJar]

    }

  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.163.197