了解Gradle“任务”语法

问题描述 投票:1回答:1

我正在努力理解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。 “buildDev”任务。因为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个回答
3
投票

谁定义了这些任务块的语法?我申请的其中一个插件?

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

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

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

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

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

  • 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
© www.soinside.com 2019 - 2024. All rights reserved.