# Part 5: Writing a Build Script

version 9.6.0-20260602042426+0000

Learn the basics of authoring Gradle by developing the Build Script.

In this section, you will:

- Understand the Project object

- Update the Build script

- Update the Plugin

- Apply the Plugin

- Run a task from the Plugin

## Step 0. Before you Begin

1. You initialized your Java app in part 1.

2. You understand the Gradle Build Lifecycle from part 2.

3. You added a subproject and a separate Build in part3.

4. You viewed a Settings file in part 4.

## Step 1. The `Project` object

Build scripts invoke Gradle APIs to configure the build.

During the configuration phase, Gradle finds the build script(s) in the root and subproject directories.

When a build script, `build.gradle(.kts)`, is found, Gradle configures a Project object.

The purpose of the Project object is to create a collection of Task objects, apply plugins, and retrieve dependencies.

You can use any of the methods and properties on the Project interface directly in your script.

For example:

    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

## Step 2. The Build script

Let’s break down the build script for the plugin:

build.gradle.kts

    plugins {                                                             (1)
        `java-gradle-plugin`                                              (2)
        id("org.jetbrains.kotlin.jvm") version "2.2.0-RC2"                  (3)
    }

    repositories {                                                        (4)
        mavenCentral()                                                    (5)
    }

    dependencies {                                                        (6)
        testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")     (7)
        testRuntimeOnly("org.junit.platform:junit-platform-launcher")
    }

    gradlePlugin {                                                        (8)
        plugins.create("greeting") {                                      (9)
            id = "license.greeting"
            implementationClass = "license.LicensePlugin"
        }
    }

    // Additional lines //

build.gradle

    plugins {                                                           (1)
        id 'java-gradle-plugin'                                         (2)
        id 'groovy'                                                     (3)
    }

    repositories {                                                      (4)
        mavenCentral()                                                  (5)
    }

    dependencies {                                                      (6)
        testImplementation libs.spock.core                              (7)
        testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
    }

    gradlePlugin {                                                      (8)
        plugins {
            greeting {
                id = 'license.greeting'                                 (9)
                implementationClass = 'license.LicensePlugin'
            }
        }
    }

    // Additional lines //

1 | Apply plugins using the `plugins{}` block
2 | Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins
3 | Apply a language plugin (Kotlin JVM or 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 | Add test dependencies
8 | Use the `gradlePlugin{}` block to configure plugin development
9 | 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:

build.gradle.kts

    gradlePlugin {  // Define a custom plugin
        plugins.create("greeting") {  // Define `greeting` plugin using the `plugins.create` method
            id = "license.greeting" // Create plugin with the specified ID
            implementationClass = "license.LicensePlugin"   // and specified implementation class
        }
    }

build.gradle

    gradlePlugin {  // Define a custom plugin
        plugins.create('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:

build.gradle.kts

    tasks.register<Test>("functionalTest") {
        testClassesDirs = functionalTestSourceSet.output.classesDirs
        classpath = functionalTestSourceSet.runtimeClasspath
        useJUnitPlatform()
    }

    tasks.named<Test>("test") {
        // Use JUnit Jupiter for unit tests.
        useJUnitPlatform()
    }

build.gradle

    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:

1. `functionalTest`: This task is registered using `tasks.register()`. It configures the test task for functional tests.

2. `test`: This task is configured using `tasks.named()` for the existing `test` task. It also configures the task to use JUnit Jupiter for unit tests.

## Step 3. Update the Build scripts

Over the following sections, we will update `LicensePlugin` to a plugin that automatically generates license headers for source code files. Let’s first update the build script with the proper name for our new `license` plugin:

build.gradle.kts

    gradlePlugin {
        plugins.create("license") {         // Update name to license
            id = "com.tutorial.license"     // Update id to com.gradle.license
            implementationClass = "license.LicensePlugin"
        }
    }

build.gradle

    gradlePlugin {
        plugins.create('license') {         // Update name to license
            id = 'com.tutorial.license'     // Update id to com.gradle.license
            implementationClass = 'license.LicensePlugin'
        }
    }

## Step 4. Apply the Plugin

Let’s apply our `license` plugin to the `app` subproject:

app/build.gradle.kts

    plugins {
        application
        id("com.tutorial.license")  // Apply the license plugin
    }

app/build.gradle

    plugins {
        id 'application'
        id('com.tutorial.license')  // Apply the license plugin
    }

## Step 5. View Plugin Task

Build init creates a "hello world" plugin when generating a Gradle plugin project. Inside `LicensePlugin` is simply a task that prints a greeting to the console, the task name is `greeting`:

build.gradle.kts

    class LicensePlugin: Plugin<Project> {
        override fun apply(project: Project) {                          // Apply plugin
            project.tasks.register("greeting") {                        // Register a task
                doLast {
                    println("Hello from plugin 'com.tutorial.greeting'")  // Hello world printout
                }
            }
        }
    }

build.gradle

    class LicensePlugin implements Plugin<Project> {
        void apply(Project project) {
            // Register a task
            project.tasks.register("greeting") {
                doLast {
                    println("Hello from plugin 'com.tutorial.greeting'")
                }
            }
        }
    }

As we can see, the `license` plugin, when applied, exposes a `greeting` task with a simple print statement.

## Step 6. View Plugin Tasks

When the `license` plugin is applied to the `app` project, the `greeting` task becomes available:

To view the task in the root directory, run:

    $ ./gradlew tasks --all

    ------------------------------------------------------------
    Tasks runnable from root project 'authoring-tutorial'
    ------------------------------------------------------------
    ...
    Other tasks
    -----------
    app:greeting
    app:task1
    app:task2
    lib:task3

Finally, run the `greeting` task using `./gradlew greeting` or:

    $ ./gradlew :app:greeting

    > Task :app:greeting
    Hello from plugin 'com.tutorial.greeting'

Next Step: Writing Tasks >>