如何从gradle中其他任务创建的文件制作一个jar文件?

How to make a jar file from the files created by other tasks in gradle?

在gradle中,我需要通过taskB 将执行taskA 后创建的文件制作一个jar。 TaskA 没有明确的输出目录。代码如下所示:

task taskA << {
    //do sth during execution
}

task taskB(type:Jar) {
    //how should I fill the "from" method to achieve my goal
    from('a folder')
    archiveName = 'A.jar'
    destinationDir = file('destination')
}

期待您的回答,谢谢!

2015-8-2新增(更新)如下:

实在抱歉之前的描述不准确

答案显示到现在我已经想到了。但是他们达不到我的目的。

准确地说,“taskA”是android项目中一个模块的内置任务,即“assembleDebug”任务。这个模块依赖于同一个项目中的一些其他模块。因此,当通过执行“assembleDebug”/“assembleRelease”编译此模块时,其他模块也将被编译并通过使彼此的模块编译链接。class 文件到此模块文件夹中的 jar。

而且我需要将所有这些模块的编译.class文件打包到一个jar文件,而不是打包jar 文件到 jar 文件,在“taskB”中。

如您所见,不仅有一个源文件夹,而且这些文件夹也不是任务的输出。所以不知道这种使用“from task's output”形式的方法能不能满足我的要求

是否有有效的解决方案?

如果 taskA 有 'Copy' 类型,那么你的工作应该很简单:

task taskA(type: Copy) {
  // do sth
}

task taskB(type: Jar) {
  from taskA
}

这里举例说明如何将依赖项类和当前项目的类合并到一个JAR中:

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile project(':depA')
}

task unzipAllClasses(type: Copy) {
    inputs.files(jar)
    inputs.files(configurations.compile)

    from configurations.compile.collect { zipTree(it) }, jar.outputs.files.collect { zipTree(it) }
    into new File(buildDir, "combined-classes")
}

task combinedJar(type: Jar) {
    from unzipAllClasses
    archiveName 'combined-with-deps.jar'
}

assemble.dependsOn combinedJar

您仍然必须确保最终出现在合并的 JAR 文件中的清单包含您想要的内容,并且您必须以某种方式发布它而不在项目的类路径中引入重复项 类,具体取决于在这个项目上。