0

我有 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/对不起我的英语不好

4

1 回答 1

3

您的任务copyFiles使用实例的方法copyProject而不是配置实际任务。这就是任务保持未配置的原因,一旦执行,就会报告没有要处理的文件 ( NO-SOURCE)。要配置您的任务,请使用任务类型的方法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)
}

通过jar将. subprojects_ from_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
}
于 2020-06-24T13:39:23.730 回答