我有 2 个子项目的 java 项目。有这样的结构
- project_root
|- client
|- src
|- build.gradle
|- server
|- src
|- build.gradle
|- build.gradle
我所需要的只是在 root build.gradle 中创建“部署”任务并执行以下操作:
- 清理所有子项目
- 清理“目标”文件夹
- 为所有子项目制作罐子
- 将所有子项目中的 jar 复制到根目录中的“目标”文件夹
- 制作目标文件夹的存档。
这就是我尝试这样做的方式
根 build.gradle:
allprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
task cleanTarget(type: Delete) {
delete "target"
delete "target.zip"
}
task cleanAll() {
dependsOn cleanTarget
dependsOn clean
dependsOn subprojects.clean
}
task jarChilds() {
dependsOn subprojects.jar
}
task copyFiles(type: Copy) {
copy {
from("client/build/libs/")
into project.file('target')
}
copy {
from("server/build/libs/")
into project.file('target')
}
}
task zipApp(type: Zip) {
from 'target/'
include '*'
include '*/*'
archiveName 'target.zip'
destinationDir(project.rootDir)
}
task deploy{
dependsOn cleanAll
dependsOn jarChilds
dependsOn copyFiles
dependsOn zipApp
jarChilds.shouldRunAfter cleanAll
copyFiles.shouldRunAfter jarChilds
zipApp.shouldRunAfter copyFiles
}
如果我手动运行它,每个任务都会正常运行,但如果我运行“部署”文件夹“目标”没有创建。这是日志输出:
Executing task 'deploy'...
> Task :clean
> Task :cleanTarget
> Task :client:clean
> Task :server:clean
> Task :cleanAll
> Task :client:compileKotlin
> Task :client:compileJava
> Task :client:processResources
> Task :client:classes
> Task :client:inspectClassesForKotlinIC
> Task :client:jar
Client jar done
> Task :server:compileKotlin
> Task :server:compileJava
> Task :server:processResources
> Task :server:classes
> Task :server:inspectClassesForKotlinIC
> Task :server:jar
Server jar done
> Task :jarChilds
> Task :copyFiles NO-SOURCE
> Task :zipApp NO-SOURCE
> Task :deploy
BUILD SUCCESSFUL in 3s
14 actionable tasks: 14 executed
12:39:15: Task execution finished 'deploy'.
我看到错误“NO-SOURCE”,但文件夹“client/build/libs”和“server/build/libs”存在并包含 jars。我可以肯定,因为手动运行任务“copyFiles”会创建文件夹“target”并复制所有文件。我看到的唯一可能的选项是在“jarChilds”之前运行的“copyFiles”任务。但我不明白为什么。我错过了什么,为什么它不起作用?
P/S/对不起我的英语不好