开发者社区> 问答> 正文

Gradle:一次运行2个Jar任务,仅制作一个jar文件

我在Gradle中创建了2个Jar任务。一种普通班:

tasks.register('compile-common', JavaCompile) {
    description = 'compile common classes'
    source = sourceSets.main.scala.srcDirs.collect { it.toPath().resolve('my/artifact/common').toFile() }
    classpath = sourceSets.main.compileClasspath
    destinationDir = project.buildDir.toPath().resolve('classes.common').toFile()
}

tasks.register('common-jar', Jar) {
    description = 'build common jar'
    manifest {
        attributes 'Build': project.version
    }
    archivesBaseName = project.name + '-common'
    from project.buildDir.toPath().resolve('classes.common').toFile()
    dependsOn 'compile-common'
}

对于具有自定义名称的完整项目:

tasks.register('project-jar', Jar) {
    description = 'build project jar'
    manifest {
        attributes 'Build': project.version
    }
    archivesBaseName = project.name
    from sourceSets.main.output
    dependsOn 'compileScala'
}

之后,我在bash中运行build:

$ rm -rf build/libs; ./gradlew project-jar common-jar; ls -l build/libs
-rw-r--r--  1 a.iatsuk  staff  265619 Dec 24 14:52 project-common-0.3.61.jar

$ rm -rf build/libs; ./gradlew common-jar project-jar ; ls -l build/libs
-rw-r--r--@ 1 a.iatsuk  staff  11482514 Dec 24 14:53 project-0.3.61.jar

$ rm -rf build/libs; ./gradlew common-jar; ./gradlew project-jar; ls -l build/libs
-rw-r--r-- 1 a.iatsuk  staff  11482514 Dec 24 14:54 project-0.3.61.jar
-rw-r--r-- 1 a.iatsuk  staff  265619   Dec 24 14:54 project-common-0.3.61.jar

为什么同时调用这两个任务时我不能得到2个罐子?我尝试编写释放任务,但无论如何它不起作用:

tasks.register('build-release') {
    dependsOn 'common-jar'
    dependsOn 'project-jar'
}

展开
收起
几许相思几点泪 2019-12-24 21:12:28 882 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
NO MORE "SBT ASSEMBLY":RETHINK 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载