【发布时间】: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, buildWatchNPM 还是 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
【问题讨论】: