Part 5: Writing a Build Script (original) (raw)
Let’s break down the build script for the plugin:
gradle/license-plugin/plugin/build.gradle.kts
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 |
gradle/license-plugin/plugin/build.gradle
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, which enhance your build capabilities, are included like this:
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
}The repositories section lets Gradle know where to pull dependencies from:
repositories {
mavenCentral() // get dependencies from the Maven central repository
}repositories {
mavenCentral() // get dependencies from the Maven central repository
}Dependencies are requirements for building your application or library:
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'
}In this example, implementation() means that the commons-lang3 library must be added to the Java classpath.
Every dependency declared for a Gradle project must apply to a scope. That is, the dependency is either needed at compile time, runtime, or both. This is called a configuration and the implementation configuration is used when the dependency is only needed in the runtime classpath.
Configuration blocks (not to be confused with dependency configurations above) are typically used to configure an applied plugin:
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
}
}
}When the java-gradle-plugin is applied, users must configure the plugin they are developing using the gradlePlugin{} configuration block.
Tasks are units of work executed during your build. They can be defined by plugins or inline:
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()
}In the example generated by Gradle init, we define two tasks:
functionalTest: This task is registered usingtasks.register(). It configures the test task for functional tests.test: This task is configured usingtasks.named()for the existingtesttask. It also configures the task to use JUnit Jupiter for unit tests.