example · gradle · daukle/gradle

gradle-nested-modules

Four modules at three depths, compiled and run through a provisioned Gradle with no build.gradle, no settings.gradle and no wrapper anywhere in the project. It is the shape of a large real build: a flat core, two modules both called core under different parents, and a module three directories deep that depends across all of them.

$ daukle gradle:run
provisioning gradle 9.6.0
tools+core+interop

That line is assembled from three modules at three depths, so it is evidence that every requires became a real project dependency and that every module's sources were found.

This example needs daukle/gradle 1.7.0 and daukle 0.7.0.

What to look at

A module name is its directory, with / between components. [modules."tools/maven/plugin"] is the module whose sources live in tools/maven/plugin/src/main/java, and daukle writes Gradle's include ':tools:maven:plugin' for it. TOML needs the key quoted. A name written with Gradle's : is refused, and the message says which / form to write instead.

tools and tools/maven are not modules here, and that is fine. Gradle makes each one an implicit parent project. daukle writes each a build file that holds only its header, so nothing a previous build left in that directory is ever read.

Three modules produce a jar called core.jar: core, interop/core and tools/core. Gradle names a jar after the last component, the three land at three different paths, and all three sit on the runtime classpath of gradle:run without colliding.

What this example cannot show

includeBuild, a composite build. It is a different Gradle mechanism, declared under [toolchains.gradle.builds] rather than [modules], and gradle-included-build shows it.

A project directory that differs from the module name. Gradle lets settings.gradle move a project anywhere; here the name IS the directory, which is what keeps one spelling for every requires.

Every file in it

core/src/main/java/com/example/core/Core.java

package com.example.core;

public final class Core {
  public static String name() {
    return "core";
  }
}

daukle.toml

schema = 1
project = "daukle/example-gradle-nested-modules"
version = "1.0.0"

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

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

[modules.core]

[modules."interop/core"]

[modules."tools/core"]
requires = ["core"]

[modules."tools/maven/plugin"]
requires = ["tools/core", "interop/core"]

[toolchains.gradle]
version = "9.6.0"

[toolchains.gradle.modules."tools/maven/plugin"]
main = "com.example.plugin.App"

interop/core/src/main/java/com/example/interop/Interop.java

package com.example.interop;

public final class Interop {
  public static String name() {
    return "interop";
  }
}

tools/core/src/main/java/com/example/tools/Tools.java

package com.example.tools;

public final class Tools {
  public static String name() {
    return "tools+" + com.example.core.Core.name();
  }
}

tools/maven/plugin/src/main/java/com/example/plugin/App.java

package com.example.plugin;

public final class App {
  public static void main(String[] args) {
    System.out.println(com.example.tools.Tools.name() + "+" + com.example.interop.Interop.name());
  }
}

Not shown here: needs-tools.