Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: 'clone' original jar task to create a new task for a jar including dependencies

Tags:

gradle

I would like to create a new task in my project that creates a jar archive with the class files of my project and the class files of the dependencies (also called 'shaded jar' or 'fat jar').

The solution proposed by the Gradle cookbook modifies the standard jar task of the JavaPlugin:

jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

However, I would like to keep the original jar taks as it is and have an additional task for the shaeded jar, i.e. a task that behaves exactly like the jar task, but includes the additional files according to

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

and has another classifier ('shaded').

I tried to take over the configuration of the jar task by copying properties like this:

task shadedJar(type: Jar, dependsOn: configurations.compile) {
    dependencies = tasks.jar.taskDependencies
    source = tasks.jar.source
    manifest = tasks.jar.manifest
    includes = tasks.jar.includes
    classifier = 'shaded'

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

But the resulting tasks does not take over the dependencies of 'jar' and the resulting jar does not include the project's class files. Additionally, this approach seems to cumbersome to be the recommended way of using an existing task as a template for a new one.

What would a recommendable approach to my specific need (the seperate shadedJar task) and for 'cloning' tasks to use them as templates for additional tasks in general?

(I am currently still on Gradle 1.3,but solutions for the current Gradle version are also welcome)

like image 537
neradis Avatar asked May 04 '13 16:05

neradis


People also ask

What is ProcessResources in gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is afterEvaluate in gradle?

> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.


1 Answers

There is no built-in way to clone tasks. However, it's easy to configure the fatJar task to include the same files as the java plugin's jar task:

task fatJar(type: Jar) {
    appendix = "fat"
    from sourceSets.main.output // that's it
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Task autowiring will automatically establish the necessary task dependencies.

If the build script goes on to customize the jar task, you can apply the customizations to both tasks simultaneously:

configure([jar, fatJar]) {
    version = "2.0"
    entryCompression = "STORED"
}

If, unlike in the jar task's case, you are defining the "template" yourself, you can "instantiate" it with a factory method:

def getMeAnotherOne(String name) {
    task(name, type: Jar) {
       version = "2.0"
       entryCompression = "STORED"
    }
}

getMeAnotherOne("jar1")
getMeAnotherOne("jar2")
like image 151
Peter Niederwieser Avatar answered Sep 29 '22 18:09

Peter Niederwieser