Creating artifacts

We saw how to define artifacts, but we also need to create artifacts in our build files. We can either use an archive task to create the artifact or a file can be an artifact. Most of the time, when we use Gradle in a Java project, we build an archive with compiled classes and resources. Actually, the Java plugin adds a jar task to our project that will just do that. The JAR file created is then added to the archives configuration.

In the next example build file, we will use the Java plugin and simply rely on the default artifact configuration and tasks. The following code shows this:

apply plugin: 'java'

// Define project properties.
group = 'com.mrhaki.sample'
version = '2.1'
archivesBaseName = 'sample'

// Extra task to check the artifacts.
task artifactsInfo << {
  configurations
    .findByName('archives')
    .allArtifacts
    .each { artifact ->
      println artifact.file.name
    }
}

We can now run the buildArchives task and check the artifacts with the artifactsInfo task from the command line:

$ gradle buildArchives artifactsInfo
:compileJava
:processResources
:classes
:jar
:buildArchives
:artifactsInfo
sample-2.1.jar

BUILD SUCCESSFUL

Total time: 7.643 secs
$

In this case, we have a single artifact; however, in the same project we can have more than one artifact when we use Gradle. For example, we might want to have our source packaged into a JAR file and our generated documentation as well. Both JAR files should be part of the archives configuration so that, when we execute the buildArchives task, all the tasks necessary to create these JAR files are executed.

We extend our previous example build file, add the code to create two extra JAR files, and add them to the archives artifact configuration. The following code shows this:

apply plugin: 'java'

// Define project properties.
group = 'com.mrhaki.sample'
version = '2.1'
archivesBaseName = 'sample'

// Create a JAR file with the
// Java source files.
task sourcesJar(type: Jar) {
  classifier = 'sources'

  from sourceSets.main.allJava
}

// Create a JAR file with the output
// of the javadoc task.
task javadocJar(type: Jar) {
  classifier = 'javadoc'

  from javadoc
}

artifacts {
  // Add the new archive tasks
  // to the artifacts configuration.
  archives sourcesJar, javadocJar
}

// Extra task to check the artifacts.
task artifactsInfo << {
  configurations
    .findByName('archives')
    .allArtifacts
    .each { artifact ->
      println artifact.file.name
    }
}

We will now execute the buildArchives and artifactsInfo tasks. We see in the output that our two new tasks, sourcesJar and javadocJar, are executed. And the generated artifact files are sample-2.1.jar, sample-2.1-sources.jar, and sample-2.1-javadoc.jar. The following command shows this:

$ gradle buildArchives artifactsInfo
:compileJava
:processResources
:classes
:jar
:javadoc
:javadocJar
:sourcesJar
:buildArchives
:artifactsInfo
sample-2.1.jar
sample-2.1-sources.jar
sample-2.1-javadoc.jar

BUILD SUCCESSFUL

Total time: 2.945 secs
$

In the preceding example, we have a Java project and, from the same source set, we want to create two different archive files. The source set contains a few API classes and implementation classes. We want to have a JAR file with the API classes and a JAR file, along with the implementation classes. The following code shows this:

apply plugin: 'java'

// Define project properties.
group = 'com.mrhaki.sample'
version = '2.1'
archivesBaseName = 'sample'

// We create a new source set
// api, which contains the
// Java sources. This means
// Gradle will search for the
// directory src/api/java.
sourceSets {
  api
}

task apiJar(type: Jar) {
  appendix = 'api'

  // We use the output of the
  // compilation of the api
  // source set, to be the
  // contents of this JAR file.
  from sourceSets.api.output
}

artifacts {
  // Assign apiJar archive task to the
  // archives configuration.
  archives apiJar
}

// Extra task to check the artifacts.
task artifactsInfo << {
  configurations
    .findByName('archives')
    .allArtifacts
    .each { artifact ->
      println artifact.file.name
    }
}

We will now run the buildArchives task and see that all the tasks necessary to create the JAR file with the classes from the api source set are executed:

$ gradle buildArchives artifactsInfo
:compileApiJava
:processApiResources
:apiClasses
:apiJar
:compileJava
:processResources
:classes
:jar
:buildArchives
:artifactsInfo
sample-2.1.jar
sample-api-2.1.jar

BUILD SUCCESSFUL

Total time: 2.
095 secs
$
..................Content has been hidden....................

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