【问题标题】:Understanding the Gradle "task" syntax了解 Gradle“任务”语法
【发布时间】:2018-03-05 08:34:06
【问题描述】:

我很难理解 Gradle“任务”语法。

我按照howto定义了build.gradle,用gradle构建了一个Angular4/SpringBoots项目。

build.gradle 包含多个 task 块:

// add our development build NpmTask named buildClientDev
task buildClientDev(type: NpmTask, dependsOn: 'npmInstall') {
    group = 'build'
    description = 'Compile client side folder for development'
    args = ['run', 'buildDev']
}


task buildClient(type: NpmTask, dependsOn: 'npmInstall') {
    group = 'build'
    description = "Compile client side folder for production"
    args = ['run', 'build']
}

// setup watcher on this ng build to link to our overall java development build.
task buildClientWatch(type: NpmTask, dependsOn: 'npmInstall') {
    group = 'application'
    description = "Build and watches the client side assets for rebuilding"
    args = ['run', 'buildWatch']
}

应用程序通过 gradle 启动,通过执行 Gradle 命令./gradlew bootRun

问题:

  • 谁定义了这些任务块的语法?我应用的插件之一?
  • buildDev, build, buildWatch NPM 还是 Gradle 命令?
  • 如果这些是 NPM 命令 - Gradle 的 gradlew bootrun 命令的连接在哪里? Gradle 怎么知道它们应该在gradlew bootrun 之后执行?

完整的 build.gradle:

buildscript {
    ext {
        springBootVersion = '2.0.0.RELEASE'
    }
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "com.moowork.gradle:gradle-node-plugin:1.1.1"
    }
}


apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
// enable building wars: gradlew BootWar
apply plugin: 'war'

// fuilding frontend with npm
apply plugin: "com.moowork.node"

// add our development build NpmTask named buildClientDev
task buildClientDev(type: NpmTask, dependsOn: 'npmInstall') {
    group = 'build'
    description = 'Compile client side folder for development'
    args = ['run', 'buildDev']
}


task buildClient(type: NpmTask, dependsOn: 'npmInstall') {
    group = 'build'
    description = "Compile client side folder for production"
    args = ['run', 'build']
}

// setup watcher on this ng build to link to our overall java development build.
task buildClientWatch(type: NpmTask, dependsOn: 'npmInstall') {
    group = 'application'
    description = "Build and watches the client side assets for rebuilding"
    args = ['run', 'buildWatch']
}

bootRun.dependsOn(buildClientDev)
jar.dependsOn(buildClient)

npm_run_build.inputs.dir new File(projectDir, "frontend")
npm_run_build.outputs.dir new File(projectDir, "build/dist")

group = 'de.webapp.spring'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/snapshot" }
    maven { url "http://repo.spring.io/milestone" }
    maven { url 'https://repo.spring.io/libs-snapshot' }
}


dependencies {
    // makes the web application startable
    compile("org.springframework.boot:spring-boot-starter")
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')

    //data
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-data-rest")

    // RepositoryRestConfigurerAdapter
    compile "org.springframework.data:spring-data-rest-core"
    compile "org.springframework.data:spring-data-rest-webmvc"
    compile "org.springframework:spring-context"

    // enables HAL browser
    compile "org.springframework.data:spring-data-rest-hal-browser"

    // entity requirements
    compile "com.h2database:h2"
    compile "javax.xml.bind:jaxb-api"
}

更新

执行链如下,在做gradlew bootRun

  • 分级。 gradlew bootRun 命令 >
  • Gradle“moowork”插件。 buildClientDev 任务。因为bootRun.dependsOn(buildClientDev)。 >
  • NPM。 “构建开发”任务。因为args = ['run', 'buildDev']>
  • 角 CLI。构建。因为在 package.json 中:buildDev": "ng build"

gradlew bootRun 的有向依赖图如下所示:

gradlew tasktree bootRun

> Task :taskTree

------------------------------------------------------------
Root project
------------------------------------------------------------

:bootRun
+--- :buildClientDev
|    +--- :npmInstall
|    |    \--- :npmSetup
|    |         \--- :nodeSetup
|    \--- :npmSetup
|         \--- :nodeSetup
\--- :classes
     +--- :compileJava
     \--- :processResources

【问题讨论】:

    标签: angular gradle npm


    【解决方案1】:

    谁定义了这些任务块的语法?我应用的插件之一?

    任务块中可用的属性和方法由任务的“类型”定义。在这种情况下,它是来自com.moowork.node 插件的NpmTask

    是 buildDev、build、buildWatch NPM 还是 Gradle 命令?

    它们是 gradle 模型中的任务,类型为 NpmTask(来自 com.moowork.node 插件)

    如果这些是 NPM 命令 - Gradle 的 gradlew bootrun 命令的连接在哪里? Gradle 怎么知道,它们应该在 gradlew bootrun 之后执行?

    同样,bootRun 不是核心 Gradle 任务,它是由 org.springframework.boot 插件添加的。它们在 Gradle 的 DAG 中连接在一起。我可以在您的 build.gradle 中看到两个 dependsOn 声明,它们将它们连接在一起

    • bootRun.dependsOn(buildClientDev)
    • task buildClientDev(type: NpmTask, dependsOn: 'npmInstall')

    如果你想可视化 DAG,我建议你添加 task-tree 插件

    plugins {
        id "com.dorongold.task-tree" version "1.3"
    }
    

    然后你可以运行

    ./gradlew taskTree bootRun
    

    你会得到一个类似于下面的任务树(注意:这个例子是一个完全不同的任务树)

    :build
    +---- :assemble
    |    \--- :jar
    |         \--- :classes
    |              +--- :compileJava
    |              \--- :processResources
    \--- :check
         \--- :test
              +--- :classes
              |    +--- :compileJava
              |    \--- :processResources
              \--- :testClasses
                   +--- :compileTestJava
                   |    \--- :classes
                   |         +--- :compileJava
                   |         \--- :processResources
                   \--- :processTestResources
    

    【讨论】:

    • 谢谢兰斯。现在我理解了链条。插件 treeView 真的是一个不错的提示。 Gradle. "gradlew bootRun" command > Gradle "moowork" plugin. buildClientDev task. Because of bootRun.dependsOn(buildClientDev). > NPM. "buildDev" task. Because of args = ['run', 'buildDev']. > Angular CLI. ng build. Because in package.json: "buildDev": "ng build",
    • 不确定我是否完全理解您的评论,但很高兴听到我的回答有助于您的理解
    • 我在问题中添加了更新,允许结构化布局。
    猜你喜欢
    • 2012-10-01
    • 2018-02-11
    • 2015-02-19
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多