了解Gradle“任务”语法

时间:2018-03-05 08:34:06

标签: angular gradle npm

我正在努力理解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命令./gradlew bootRun

,通过gradle启动应用程序

问题:

  • 谁定义了这些任务块的语法?我申请的其中一个插件?
  • 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']>
  • Angular 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

1 个答案:

答案 0 :(得分: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