为什么 gradle 任务没有 运行 正确的顺序
Why gradle tasks doesn't run in proper order
我有 java 个项目和 2 个子项目。像这样的结构
- project_root
|- client
|- src
|- build.gradle
|- server
|- src
|- build.gradle
|- build.gradle
我只需要在根目录 buiild.gradle 中创建 'deploy' 任务并执行下一步操作:
- 清理所有子项目
- 清理 'target' 文件夹
- 为所有子项目制作 jar
- 将所有子项目的 jar 复制到根目录中的文件夹 'target'
- 制作目标文件夹的存档。
我就是这样做的
root 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
}
如果我手动 运行 每个任务 运行 都正确,但是如果我 运行 'deploy' 文件夹 'target' 没有创建。这是日志输出:
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' 存在并包含 jar。我可以肯定,因为手动 运行ning 任务 'copyFiles' 创建文件夹 'target' 并复制所有文件。
我看到的唯一可能的选项是 'copyFiles' task 运行ning before 'jarChilds'。但我不明白为什么。我错过了什么,为什么它不起作用?
P/S/ 抱歉我的英语不好
您的任务copyFiles
使用方法copy
of the Project
instance instead of configuring the actual task. This is the reason why the task stays unconfigured and, once it is executed, reports that there are no files to process (NO-SOURCE
). To configure your task, use methods of the task type Copy
:
task copyFiles(type: Copy) {
from("client/build/libs/")
from("server/build/libs/")
into("target")
}
附加提示:您可能根本不需要您的任务 copyFiles
。相反,您可以只使用任务 zipApp
来收集文件:
task zipApp(type: Zip) {
from subprojects.jar
archiveName 'target.zip'
destinationDir(project.rootDir)
}
通过将 subprojects
的 jar
任务放入 from
方法中, Gradle 甚至会自动添加任务依赖项,因此您无需使用 dependsOn
.
注册它们
查看以下简短而完整的 build.gradle
文件:
subprojects { // using subprojects here, because your root project has no sources
apply plugin: 'java'
repositories {
mavenCentral()
}
}
task clean(type: Delete) { // without the 'java' plugin, there is no automatic 'clean' task
dependsOn subprojects.clean
delete 'target.zip'
}
task zipApp(type: Zip) {
mustRunAfter clean
from subprojects.jar
archiveName 'target.zip'
destinationDir(project.rootDir)
}
task deploy {
dependsOn clean
dependsOn zipApp
}
我有 java 个项目和 2 个子项目。像这样的结构
- project_root
|- client
|- src
|- build.gradle
|- server
|- src
|- build.gradle
|- build.gradle
我只需要在根目录 buiild.gradle 中创建 'deploy' 任务并执行下一步操作:
- 清理所有子项目
- 清理 'target' 文件夹
- 为所有子项目制作 jar
- 将所有子项目的 jar 复制到根目录中的文件夹 'target'
- 制作目标文件夹的存档。
我就是这样做的
root 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
}
如果我手动 运行 每个任务 运行 都正确,但是如果我 运行 'deploy' 文件夹 'target' 没有创建。这是日志输出:
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' 存在并包含 jar。我可以肯定,因为手动 运行ning 任务 'copyFiles' 创建文件夹 'target' 并复制所有文件。 我看到的唯一可能的选项是 'copyFiles' task 运行ning before 'jarChilds'。但我不明白为什么。我错过了什么,为什么它不起作用?
P/S/ 抱歉我的英语不好
您的任务copyFiles
使用方法copy
of the Project
instance instead of configuring the actual task. This is the reason why the task stays unconfigured and, once it is executed, reports that there are no files to process (NO-SOURCE
). To configure your task, use methods of the task type Copy
:
task copyFiles(type: Copy) {
from("client/build/libs/")
from("server/build/libs/")
into("target")
}
附加提示:您可能根本不需要您的任务 copyFiles
。相反,您可以只使用任务 zipApp
来收集文件:
task zipApp(type: Zip) {
from subprojects.jar
archiveName 'target.zip'
destinationDir(project.rootDir)
}
通过将 subprojects
的 jar
任务放入 from
方法中, Gradle 甚至会自动添加任务依赖项,因此您无需使用 dependsOn
.
查看以下简短而完整的 build.gradle
文件:
subprojects { // using subprojects here, because your root project has no sources
apply plugin: 'java'
repositories {
mavenCentral()
}
}
task clean(type: Delete) { // without the 'java' plugin, there is no automatic 'clean' task
dependsOn subprojects.clean
delete 'target.zip'
}
task zipApp(type: Zip) {
mustRunAfter clean
from subprojects.jar
archiveName 'target.zip'
destinationDir(project.rootDir)
}
task deploy {
dependsOn clean
dependsOn zipApp
}