第 5 部分:编写构建脚本
通过开发构建脚本来了解创作 Gradle 的基础知识。
第 1 步.Project
对象
构建脚本调用 Gradle API 来配置构建。
在配置阶段,Gradle 在根目录和子项目目录中查找构建脚本。
build.gradle(.kts)
当找到构建脚本 时,Gradle 会配置一个Project对象。
您可以直接在脚本中使用项目接口上的任何方法和属性。
例如:
defaultTasks("some-task") // Delegates to Project.defaultTasks()
reportsDir = file("reports") // Delegates to Project.file() and the Java Plugin
defaultTasks 'some-task' // Delegates to Project.defaultTasks()
reportsDir = file('reports') // Delegates to Project.file() and the Java Plugin
步骤 2. 构建脚本
让我们分解一下插件的构建脚本:
plugins { (1)
`java-gradle-plugin` (2)
id("org.jetbrains.kotlin.jvm") version "1.9.0" (3)
}
repositories { (4)
mavenCentral() (5)
}
dependencies { (6)
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") (7)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
gradlePlugin { (8)
val greeting by plugins.creating { (9)
id = "license.greeting"
implementationClass = "license.LicensePlugin"
}
}
// Additional lines //
1 | Use the plugins{} block from KotlinSettingsScript in the Kotlin DSL |
2 | Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins |
3 | Apply the Kotlin JVM plugin to add support for Kotlin |
4 | Use Project.repositories() to configure the repositories for this project |
5 | Use Maven Central for resolving dependencies |
6 | Use Project.dependencies() to configure the dependencies for this project |
7 | Use the Kotlin JUnit 5 integration |
8 | Use the gradlePlugin{} block from GradlePluginDevelopmentExtension in the Kotlin DSL |
9 | Define the plugin id and implementationClass |
plugins { (1)
id 'java-gradle-plugin' (2)
id 'groovy' (3)
}
repositories { (4)
mavenCentral() (5)
}
dependencies { (6)
testImplementation libs.spock.core
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
gradlePlugin { (7)
plugins {
greeting {
id = 'license.greeting' (8)
implementationClass = 'license.LicensePlugin'
}
}
}
// Additional lines //
1 | Use the plugins{} block from the PluginDependenciesSpec API in the Groovy DSL |
2 | Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins |
3 | Apply the Groovy plugin to add support for Groovy |
4 | Use Project.repositories() to configure the repositories for this project |
5 | Use Maven Central for resolving dependencies |
6 | Use Project.dependencies() to configure the dependencies for this project |
7 | Use the gradlePlugin{} block from the PluginAware API in the Groovy DSL |
8 | Define the plugin id and implementationClass |
插件可以增强您的构建能力,如下所示:
plugins {
id("java") // core plugin, no version required
id("org.some.plugin") version "2.8" // community plugin, version required
}
plugins {
id 'java' // core plugin, no version required
id 'org.some.plugin' version '2.8' // community plugin, version required
}
存储库部分让 Gradle 知道从哪里提取依赖项:
repositories {
mavenCentral() // get dependencies from the Maven central repository
}
repositories {
mavenCentral() // get dependencies from the Maven central repository
}
依赖项是构建应用程序或库的要求:
dependencies {
// group: 'org.apache.commons', name: 'commons-lang3', version: '3.13.0'
implementation("org.apache.commons:commons-lang3:3.13.0")
}
dependencies {
// group: 'org.apache.commons', name: 'commons-lang3', version: '3.13.0'
implementation 'org.apache.commons:commons-lang3:3.13.0'
}
在此示例中,implementation()
意味着commons-lang3
必须将库添加到 Java 类路径中。
为 Gradle 项目声明的每个依赖项都必须适用于某个范围。也就是说,依赖关系要么在编译时、运行时或两者都需要。这称为配置,implementation
当仅在运行时类路径中需要依赖项时使用该配置。
配置块(不要与上面的依赖配置混淆)通常用于配置应用的插件:
gradlePlugin { // Define a custom plugin
val greeting by plugins.creating { // Define `greeting` plugin using the `plugins.creating` method
id = "license.greeting" // Create plugin with the specified ID
implementationClass = "license.LicensePlugin" // and specified implementation class
}
}
gradlePlugin { // Define a custom plugin
plugins {
greeting { // Define a plugin named greeting
id = 'license.greeting' // using the id
implementationClass = 'license.LicensePlugin' // and implementationClass
}
}
}
应用时java-gradle-plugin
,用户必须使用配置块配置他们正在开发的插件gradlePlugin{}
。
任务是构建期间执行的工作单元。它们可以通过插件或内联定义:
val functionalTest by tasks.registering(Test::class) {
testClassesDirs = functionalTestSourceSet.output.classesDirs
classpath = functionalTestSourceSet.runtimeClasspath
useJUnitPlatform()
}
tasks.named<Test>("test") {
// Use JUnit Jupiter for unit tests.
useJUnitPlatform()
}
tasks.register('functionalTest', Test) {
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
useJUnitPlatform()
}
tasks.named('test') {
// Use JUnit Jupiter for unit tests.
useJUnitPlatform()
}
在 Gradle init 生成的示例中,我们定义了两个任务:
-
functionalTest
:该任务是使用 注册的tasks.register()
。它配置功能测试的测试任务。 -
test
:此任务是使用tasks.named()
现有test
任务配置的。它还将任务配置为使用 JUnit Jupiter 进行单元测试。
步骤 3. 更新构建脚本
在以下部分中,我们将更新LicensePlugin
一个自动为源代码文件生成许可证标头的插件。让我们首先使用新插件的正确名称更新构建脚本license
:
gradlePlugin {
val license by plugins.creating { // Update name to license
id = "com.tutorial.license" // Update id to com.gradle.license
implementationClass = "license.LicensePlugin"
}
}
gradlePlugin {
// Define the plugin
plugins {
license { // Update name to license
id = 'com.tutorial.license' // Update id to com.gradle.license
implementationClass = 'license.LicensePlugin'
}
}
}
步骤 3. 应用插件
让我们将我们的license
插件应用到app
子项目中:
plugins {
application
id("com.tutorial.license") // Apply the license plugin
}
plugins {
id 'application'
id('com.tutorial.license') // Apply the license plugin
}
步骤 4. 查看插件任务
Build init 在生成 Gradle 插件项目时会创建一个“hello world”插件。里面LicensePlugin
只是一个向控制台打印问候语的任务,任务名称是greeting
:
class LicensePlugin: Plugin<Project> {
override fun apply(project: Project) { // Apply plugin
project.tasks.register("greeting") { task -> // Register a task
task.doLast {
println("Hello from plugin 'com.tutorial.greeting'") // Hello world printout
}
}
}
}
class LicensePlugin implements Plugin<Project> {
void apply(Project project) {
// Register a task
project.tasks.register("greeting") {
doLast {
println("Hello from plugin 'com.tutorial.greeting'")
}
}
}
}
正如我们所看到的,该license
插件在应用时会greeting
通过简单的打印语句公开一个任务。
步骤 5.查看插件任务
当license
插件应用于项目时app
,greeting
任务变得可用:
要查看根目录中的任务,请运行:
$ ./gradlew tasks --all
------------------------------------------------------------
Tasks runnable from root project 'authoring-tutorial'
------------------------------------------------------------
...
Other tasks
-----------
app:greeting
app:task1
app:task2
lib:task3
最后,greeting
使用./gradlew greeting
或运行任务:
$ ./gradlew :app:greeting
> Task :app:greeting
Hello from plugin 'com.tutorial.greeting'
下一步: 写作任务>>