Using archive task artifacts

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

apply plugin: 'ivy-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(IvyPublication) {

      // 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 run the tasks task, and in the output, we see new generated tasks to publish this publication:

$ gradle tasks
...
Publishing tasks
----------------
generateDescriptorFileForPublishJarPublication - Generates the Ivy Module Descriptor XML file for publication 'publishJar'.
publish - Publishes all publications produced by this project.
...
BUILD SUCCESSFUL

Total time: 4.215 secs

Notice the extra task, generateDescriptorFileForPublishJarPublication. The name of the publishJar publication is used for this task. Gradle uses the following pattern for a task to generate an Ivy descriptor XML file for a generateDescriptorFileFor<publicationName>Publication publication. We cannot yet invoke the task because we also need to set the group and version project properties, but we will see this in the section about generating an Ivy descriptor file. We will 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. We can even use the artifacts property to assign multiple artifacts. It is important that each artifact has 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: 'ivy-publish'
apply plugin: 'java'

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

task javadocJar(type: Jar) {
  from javadoc
}

publishing {

  publications {

    publishJar(IvyPublication) {

      artifact jar

      // Add output of sourcesJar task
      // as an artifacts. In the task
      // the classifier is already
      // set to sources.
      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: 'ivy-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(IvyPublication) {

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

    }

  }
}

Using file artifacts

Instead of an archive task, we can also use a file as an artifact. Gradle tries to extract the extension and classifier properties from the filename. We can also configure these properties ourselves when we add the file as a publication artifact.

In the following example build file, we use the src/files/README and src/files/COPYRIGHT files as a publication artifact:

apply plugin: 'ivy-publish'

publishing {
  publications {
    documentation(IvyPublication) {

      // Use file name as a publication artifact.
      artifact 'src/files/README'

      artifact('src/files/COPYRIGHT') {
        // Each file artifact must have a
        // unique classifier and extension.
        classifier = 'metaInformation'
      }

      // Alternative syntax is with
      // the Map notation:
      // artifact source: 'src/files/README'
      // artifact source: 'src/files/COPYRIGHT',
      //          extension: 'metaInformation'

    }
  }
}

Using software components

Besides the artifact method and the artifacts property, we can also use the from method inside a publications configuration block. We specify a Gradle SoftwareComponent object as an argument to the from method. The java plugin adds a SoftwareComponent object with the name java, and it includes the jar artifact and all runtime dependencies. The war plugin adds the war artifact as a SoftwareComponent object.

In the next example build file, we will apply the war plugin to our project. The war plugin extends the java plugin, so we will also implicitly apply the java plugin to our project. We will also define two publications, with each using the SoftwareComponent object from both plugins:

apply plugin: 'ivy-publish'
apply plugin: 'war'


publishing {

  publications {

    // First publication with
    // the name javaJar, contains
    // the artifact created by the
    // jar task.
    javaJar(IvyPublication) {
      from components.java
    }

    // Second publication with
    // the name webWar, contains
    // the artifact created by
    // the war task.
    webWar(IvyPublication) {
      from components.web
    }

  }

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

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