In the previous section, you learned that the Java plugin adds an archives
configuration that is used to group artifacts from the project. Just as we created configurations for dependencies in our project, we can also create our own configurations for its artifacts. To assign an archive or file to this configuration, we must use the artifacts
configuration block in our build script. Inside the configuration closure, we use the name of the configuration followed by the artifact. We can also further customize the artifact definition inside the artifacts
block.
We can define artifacts with the following three types:
In the next example build file, we will use an archive task to define the artifacts for the project. It is important to remember to apply the Gradle base plugin to the project because the base plugin adds the task rules for build<ConfigurationName>
and upload<ConfigurationName>
. The following code shows this:
// The base plugin adds the // build<ConfigurationName> and // upload<ConfigurationName> tasks // to our project. apply plugin: 'base' // Add archive task that will // create a ZIP file with some // contents we want to be published. task manual(type: Zip) { baseName = 'manual' from 'src/manual' } // Create a new artifacts configuration // with the name manualDistribution. configurations { manualDistribution } // Use the manual archive task // to define the artifact for the // manualDistribution configuration. // Syntax: // configurationName archiveTask artifacts { manualDistribution manual }
When we use an archive task to define the artifact for a configuration, Gradle also adds a task dependency for the artifact. This means that, if we invoke the buildManualDistribution
task, Gradle also invokes the manual
task that generates the archive for the artifact configuration. We see this when we execute the task from the command line. The following command shows this:
$ gradle buildManualDistribution :manual :buildManualDistribution BUILD SUCCESSFUL Total time: 1.368 s ecs
Besides archive tasks, we can use a file as an artifact. Gradle will use the properties of the file to define the artifact name, type, and extension. In the following example build file, we use a file as an artifact:
apply plugin: 'base' configurations { readmeDistribution } artifacts { // Use a file as artifact. // Name and extension are extracted // from the actual file. readmeDistribution file('src/files/README.txt') }
We can add an extra configuration closure when we use the file artifact notation. In the closure, we can set the name, type, extension, and classifier attributes. The following code shows this:
apply plugin: 'base' configurations { readmeDistribution } artifacts { // Define file artifact, but we also // customize the file artifact // name, extension and classifier. readmeDistribution file('src/files/README.txt'), { name 'PLEASE_READ_THIS' extension '' classifier 'docs' } }
One interesting method we can use in the file artifact configuration closure is the builtBy
method. This method accepts one or more task names that are responsible for building the artifact. If we use this method, Gradle can determine the tasks that need to be executed when we run the build<ConfigurationName>
or upload<ConfigurationName>
task.
We will use the builtBy
method in the next example build file:
apply plugin: 'base' configurations { readmeDistribution } // New task that copies // a file to the build directory. task docFiles(type:Copy) { from 'src/files' into "${buildDir}/docs" include 'README.txt' } artifacts { // Define file artifact. readmeDistribution(file("${buildDir}/docs/README.txt")) { // Define which task is responsible // for creating the file, so a // task dependency is added for // the buildReadmeDistribution and // uploadReadmeDistribution tasks. builtBy docFiles } }
To ensure that the docFiles
task is added as a task dependency, we run the buildReadmeDistribution
from the command line. The following command shows this:
$ gradle buildReadmeDistribution :docFiles :buildReadmeDistribution BUILD SUCCESSFUL Total time: 0.864 secs
Finally, we can use a map notation when we define a file artifact. We use the file
attribute to define the file. We can also use the name
, extension
, type
, classifier
, and builtBy
keys for the definition. In the following example build file, we use the map notation:
apply plugin: 'base' configurations { readmeDistribution } task docFiles(type:Copy) { from 'src/files' into "${buildDir}/docs" include 'README.txt' } artifacts { // Define file artifact. readmeDistribution( file: "${buildDir}/docs/README.txt", name: 'DO_READ', extension: 'me', type: 'text', classifier: 'docs' builtBy: docFiles ) }
18.223.172.132