Creating a task in a standalone project

To make a task reusable for other projects, we must have a way to distribute the task. Also, other projects that want to use the task must be able to find our task. We will see how we can publish our task in a repository and how other projects can use the task in their projects.

We have seen how we can place the task implementation from the build file into the buildSrc directory. The buildSrc directory is similar to a normal Gradle build project, so it is easy to create a standalone project for our task. We only have to copy the contents of the buildSrc directory to our newly created project directory.

Let's create a new project directory and copy the contents of the buildSrc directory. We must edit the build.gradle file of our standalone project. Gradle implicitly added the Groovy plugin and dependencies on the Gradle API and Groovy for us when the build.gradle file is in the buildSrc directory. Now we have a standalone project, and we must add those dependencies ourselves.

The following build.gradle file has all the definitions necessary to build and deploy our artifact to a local distribution directory. We could also define a corporate intranet repository so that other projects can re-use our InfoTask in their projects.

apply plugin: 'groovy'
apply plugin: 'maven'

version = '1.0'
group = 'sample.infotask'
archivesBaseName = 'infotask'

repositories.mavenCentral()

dependencies {
    compile gradleApi()
    groovy localGroovy()
    testCompile 'junit:junit:4.10'
}

uploadArchives {
    repositories.mavenDeployer {
        repository(url: 'file:../lib')
    }
}

When we invoke the uploadArchives task to publish our packaged InfoTask in the ../lib directory, we see the following output:

$ gradle uploadArchives
...
:uploadArchives
Uploading: sample/infotask/infotask/1.0/infotask-1.0.jar to repository remote at file:../lib
Transferring 5K from remote
Uploaded 5K

BUILD SUCCESSFUL

Total time: 3.431 secs

We have published our task, and other projects can use it in their builds. Remember that anything in the buildSrc directory of a project is added automatically to the classpath of the build. But if we have a published artifact with the task, this will not happen automatically. We must configure our build and add the artifact as a dependency of the build script.

We use the buildscript{} script block in our build to configure the classpath of our Gradle project. To include our published InfoTask in a new project, we must add the artifact as a classpath configuration dependency for our build.

We create a new directory and add the following build.gradle file to the directory:

buildscript {
    repositories {
        maven {
            url 'file:../lib'
        }
    }
    dependencies {
        classpath group: 'sample.infotask', name: 'infotask', version: '1.0'
    }
}

task info(type: sample.InfoTask)

defaultTasks 'info'

Next, we can run the build and see in the output that the InfoTask is executed:

$ gradle
:info
Current Gradle version: 1.1

BUILD SUCCESSFUL

Total time: 3.452 secs


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

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