Gradle: The clone task to create a new task for the jar, including dependencies

I would like to create a new task in my project that creates a jar archive with class files of my project and dependency class files (also called "shaded bank" or "bold bank").

The solution proposed by Gradle's cookie book modifies the standard JavaPlugin jar task:

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 hatched jar, that is, a task that behaves exactly the same as the jar task, but includes additional files in accordance with

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

and has a different classifier ("shaded").

I tried to take on the configuration of the jar task by copying the following properties:

 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 do not affect the jar dependencies, and as a result, jar does not include project class files. In addition, this approach seems cumbersome to be the recommended way to use an existing task as a template for a new one.

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

(Currently I'm still on Gradle 1.3, but solutions for the current version of Gradle are also welcome)

+7
source share
1 answer

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

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

Task autoload will automatically install the necessary task dependencies.

If the build script continues to configure the jar task, you can apply the settings for both tasks at the same time:

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

If, unlike the case of the jar task, you define the "template" yourself, you can "create an instance" using the factory method:

 def getMeAnotherOne(String name) { task(name, type: Jar) { version = "2.0" entryCompression = "STORED" } } getMeAnotherOne("jar1") getMeAnotherOne("jar2") 
+8
source

All Articles