为包含依赖关系的jar创建新任务

为包含依赖关系的jar创建新任务

本文介绍了Gradle:“克隆”原始jar任务,为包含依赖关系的jar创建新任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想在我的项目中创建一个新任务,创建一个jar文件,其中包含我的项目的类文件和依赖关系的类文件(也称为着色jar或fat jar)。



Gradle Cookbook提出的解决方案修改了JavaPlugin的标准jar任务:

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

然而,我想保留原始的jar会保持原样,并且对于有问题的jar有一个额外的任务,即一个任务的行为与jar任务完全相同,但包括根据

 来自{configurations.compile.collect {it.isDirectory()?它:zipTree(it)}} 

并有另一个分类器('shaded')。



我尝试通过复制像这样的属性来接管jar任务的配置:

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

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

但是结果任务并没有接管'jar'和生成的jar的依赖关系不包含项目的类文件。此外,这种方法似乎很麻烦,作为使用现有任务作为新模板的推荐方式。



根据我的具体需求推荐的方法是什么单独的阴影Jar任务)和'克隆'任务来将它们用作一般额外任务的模板?

(我目前仍在Gradle 1.3上,但解决方案为当前的Gradle版本也是受欢迎的)

解决方案

克隆任务没有内置的方法。但是,将 fatJar 任务配置为包含与 java 插件的相同的文件很容易。 jar 任务:




$ b

  task fatJar(类型:Jar){
appendix =fat
from sourceSets.main.output //这就是
from {configurations.compile.collect {it.isDirectory()?它:zipTree(it)}}
}

任务自动连线会自动建立必要的任务依赖关系。



如果构建脚本继续自定义 jar 任务,可以同时将定制应用于两个任务:
$ b

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

如果与 jar 任务不同,你自己定义了template,你可以用工厂方法实例化它:

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

getMeAnotherOne(jar1)
getMeAnotherOne(jar2)


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)

解决方案

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")

这篇关于Gradle:“克隆”原始jar任务,为包含依赖关系的jar创建新任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:03