example · gradle · daukle/gradle

gradle-included-build

A Gradle plugin built from source in this repository and applied to the project, through Gradle's composite builds, with no build.gradle, no settings.gradle and no wrapper anywhere in the project. build-logic is an included build that contributes the project plugin example.hello, and the task that plugin registers runs as a daukle task.

$ daukle gradle:hello
provisioning gradle 9.6.0
hello from build-logic, applied to "example-gradle-included-build"

That line is printed by a class compiled from build-logic/src/main/java in the same Gradle run, so it is evidence that the build was included, built, and its plugin applied by id. The quotes are Gson's: the plugin formats the name with new Gson().toJson(...), so the line is also evidence that the included build compiled and ran against a dependency daukle/maven resolved into that build's own block.

This example needs daukle/gradle 1.9.0 and daukle/maven 1.2.0.

What to look at

[toolchains.gradle.builds."build-logic"] describes the included build, and the key is its directory, with / between components, exactly as a module name is. Its sources default to build-logic/src/main/java. Declaring gradlePlugins makes it a PLUGIN build: daukle writes pluginManagement { includeBuild('included/build-logic') } into the generated settings.gradle and a gradlePlugin {} block into the included build's own generated file. Nothing is written into build-logic/ itself.

The plugins entry has no version, and that is how it says the plugin comes from an included build rather than from a pinned jar. It must name an id some build declares in gradlePlugins. It renders as id 'example.hello' inside the project's plugins {} block, not as apply plugin:, because Gradle finds a plugin from an included build only through plugins {}.

The included build has dependencies of its own, resolved by daukle/maven. Its [[toolchains.maven.resolve]] entry names into = ["builds", "build-logic", "classpath"], the path to the build's own classpath, so Gson lands on build-logic and on nothing else. The pins are in daukle/maven/classpath.lua, written by daukle maven:resolve --resolve and included from daukle.lua, which is why the example carries both files.

tasks = ["hello"] surfaces the task the plugin registers as daukle gradle:hello, the same way a task from a third-party plugin is surfaced.

What this example cannot show

A library build. A build with no gradlePlugins is included at the top level instead, and a project or a module depends on it with requiresBuilds = ["<name>"]. The plugin's own cases run one; this example keeps to the plugin half, which is the one a build-logic directory is for.

Every file in it

build-logic/src/main/java/com/example/logic/HelloPlugin.java

package com.example.logic;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public final class HelloPlugin implements Plugin<Project> {
  @Override
  public void apply(Project project) {
    project.getTasks().register("hello", task -> task.doLast(ignored ->
        System.out.println("hello from build-logic, applied to "
            + new com.google.gson.Gson().toJson(project.getName()))));
  }
}

daukle.toml

schema = 1
project = "daukle/example-gradle-included-build"
version = "1.0.0"

[modules]

[resolvers.github]
url = "https://raw.githubusercontent.com/daukle/daukle/1f515141e06d16eace718a45944a39933611fa39/plugins/github-releases.lua"
sha256 = "3789865c266eb53fafcf7300d77a24461170c615195d92d0ddf88078c556e23e"

[plugins]
gradle = { resolver = "github", coordinate = "daukle/gradle@^1.9.0" }
maven  = { resolver = "github", coordinate = "daukle/maven@^1.2.0" }

[toolchains.gradle]
version = "9.6.0"
plugins = [{ id = "example.hello" }]
tasks = ["hello"]

[toolchains.gradle.builds."build-logic"]
gradlePlugins = [{ id = "example.hello", implementationClass = "com.example.logic.HelloPlugin" }]

[toolchains.maven]
for = "gradle"

[[toolchains.maven.resolve]]
coordinates = ["com.google.code.gson:gson:2.10.1"]
into = ["builds", "build-logic", "classpath"]

daukle/maven/classpath.lua

-- Generated by maven:resolve. Do not edit: daukle.toml is yours, this is not.
-- Every url and sha256 here was fetched and hashed, because Maven Central
-- publishes no .sha256 for these artifacts.
--
-- Include it from daukle.lua:
--   daukle.include("daukle/maven/classpath.lua")

do
  local toolchain = daukle.config.toolchains and daukle.config.toolchains.gradle
  if toolchain == nil then
    error('daukle.config.toolchains.gradle does not exist: maven resolved for "'
          .. 'gradle" but the manifest declares no such toolchain', 0)
  end

  do
    local owner = toolchain
    owner["builds"] = owner["builds"] or {}
    owner = owner["builds"]
    owner["build-logic"] = owner["build-logic"] or {}
    owner = owner["build-logic"]
    local pins = owner["classpath"] or {}

    pins[#pins + 1] = {
      url = "https://repo1.maven.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar",
      sha256 = "4241c14a7727c34feea6507ec801318a3d4a90f070e4525681079fb94ee4c593",
      as = "gson 2.10.1",
    }

    owner["classpath"] = pins
  end
end

Not shown here: needs-tools.