【问题标题】:GRADLE tasks run configuration [duplicate]GRADLE任务运行配置[重复]
【发布时间】:2020-10-30 06:06:38
【问题描述】:

我有一个项目,它由两部分组成:Spring Boot 和 React。 在我的 Spring Boot build.gradle 配置中,我指定了应该执行哪些操作才能构建和运行应用程序。

如下所示:

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
  id "com.github.node-gradle.node" version "2.2.4"
}

group = 'com.vtti'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
  implementation 'org.apache.poi:poi:3.10-FINAL'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
        compile 'org.apache.poi:poi:3.10-FINAL'
        compile 'org.apache.poi:poi-ooxml:3.10-FINAL'
        compile 'com.sendgrid:sendgrid-java:4.1.2'
        compile 'org.json:json:20190722'
        compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.11.0'
}

test {
    useJUnitPlatform()
}

node {
  // Version of node to use.
  version = '10.16.3'
  // Version of Yarn to use.
  yarnVersion = '1.21.1'

  // Base URL for fetching node distributions (change if you have a mirror).
  distBaseUrl = 'https://nodejs.org/dist'

  // If true, it will download node using above parameters.
  // If false, it will try to use globally installed node.
  download = true

  // Set the work directory for unpacking node
  workDir = file("${project.buildDir}/nodejs")
  // Set the work directory for YARN
  yarnWorkDir = file("${project.buildDir}/yarn")
  // Set the work directory where node_modules should be located
  nodeModulesDir = file("${project.projectDir}")
}


task appYarnInstall(type: YarnTask) {
  description = "Install all dependencies from package.json"
  workingDir = file("${project.projectDir}/src/main/client")
  args = ["install"]
}

task appYarnBuild(type: YarnTask) {
  description = "Build production version of the React client"
  workingDir = file("${project.projectDir}/src/main/client")
  args = ["run", "build"]
}

task copyClient(type: Copy) {
  from 'src/main/client/build'
  // into 'build/resources/main/static/.'
  into 'src/main/resources/static/.'
}

appYarnBuild.dependsOn appYarnInstall
copyClient.dependsOn appYarnBuild
compileJava.dependsOn copyClient

当我运行 gradlew build 一切正常时,Gradle 执行 yarn installyarn build 等。

但是,当我运行 gradlew bootRun 并只想编译和运行一个项目时,它会再次执行所有 yarn 操作并构建一个新的前端,这会导致多个可见的“更改文件” git 并且应该再次提交。

是否可以指定何时运行任务并仅在 gradlew build 而不是 gradlew bootRun 上运行它们?

【问题讨论】:

    标签: java reactjs spring gradle build.gradle


    【解决方案1】:

    您可以使用 -x 命令行选项并提供要排除的任务的名称来排除任务的执行。

    例如 gradle bootRun -x appYarnInstall

    【讨论】:

      【解决方案2】:

      以前有人问过这个问题,但在您的具体情况下,这可能是您正在寻找的:

      gradle.taskGraph.whenReady { graph ->
        if (graph.hasTask(bootRun)) {
          tasks.withType(YarnTask){
            enabled = false
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        这样的问题经常出现,而答案几乎总是针对症状而不是实际原因。大多数解决方案将包含以下 Gradle 功能之一:

        • 命令行选项-x或其程序等效项gradle.startParameter.excludedTaskNames
        • 直接访问gradle.taskGraph
        • 任务的onlyIf 子句

        在大多数情况下,这些解决方案实际上是肮脏的 hack,以后可能适得其反。

        在很多情况下,实际问题是任务配置和连接方式错误。 Gradle 世界中的一个常见误解是认为只有少数任务可以从命令行调用。基于这个假设,整个构建脚本都是围绕这些任务设计的(通常只有 Java 插件中的任务build)。在这些情况下,整个构建脚本由dependsOn 语句组成,当调用gradle build 时,这些语句以正确的顺序执行任务。您关注命令(而不是任务)gradlew buildgradlew bootRun 的问题显示了同样的问题:

        是否可以指定何时应运行任务并仅在 gradlew build 而不是 gradlew bootRun 上运行它们?

        一个更好的问题是:

        任务appYarnInstallappYarnBuild是否可以只在需要运行的时候才运行?

        正如您在问题中解释的那样,实际问题是由任务再次运行这一事实引起的。那么,也许我们应该弄清楚它们什么时候需要运行?如果我对您的项目结构的理解正确,则需要在两种情况下运行:

        • 如果前端根本不存在(例如在新结账时)
        • 如果某些前端源文件发生了变化

        现在您可以在构建脚本中实现此逻辑,但 Gradle 通过其 incremental build support 开箱即用地提供了此逻辑。您只需要定义创建文件的任务的输入和输出,Gradle 就会确定任务何时需要运行。

        由于我不完全了解您的哪些任务处理了哪些文件(而且我不熟悉 Gradle Node 插件),因此我很难为您提供一个完整工作的构建脚本,但让我给您一些提示:

        • 不要将 Gradle 处理的源目录与 Node 或 Yarn 等外部系统处理的源目录混用。 workingDir hack 变得很有必要,因为这些工具需要另一个存储库布局。在您当前的设置中,任务结果最终位于源目录 (src/main/client/build) 中。

        • 不要使用Copy 类型的任务。相反,定义任务appYarnBuild 的输出并将此任务用作processResources 的附加输入。这也消除了您对compileJava 的任务依赖。

        • 始终将任务结果存储在 buildDir 中,而不是 src 的子文件夹中。这样您就可以始终使用gradle clean build 创建一个干净的构建。如果任务在src 中创建文件,它们不会在clean 期间被删除,并且可能会在以后的构建中导致问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多