Intermediate Shared Layers
When you support three or more Minecraft versions (for example 26.1, 26.2, and 26.3), breaking changes rarely split cleanly into "all versions" vs. "one version":
- API A existed in
26.1and26.2, then changed in26.3. - API B was introduced in
26.2and stayed the same in26.3.
Without intermediate layers, you would have to copy-paste the 26.1/26.2 code into both src/26.1/common and src/26.2/common, and copy-paste the 26.2/26.3 code into both src/26.2/common and src/26.3/common.
Inspired by Kotlin Multiplatform's intermediate source sets (like nonWebMain or appleMain), Graft lets you declare arbitrary shared(...) layers and compose them with dependsOn(...).
Declaring and attaching shared(...) layers
Here is the real-world setup from PlayerAnimationLibrary, which supports Minecraft 26.1, 26.2, and 26.3 across Fabric and NeoForge:
graft {
modId = "player_animation_library"
common {
mixins.from(file("src/main/resources/player_animation_library.mixins.json"))
}
fabric {}
neoforge {}
// Shared by 26.1 and 26.2 (before 26.3 removed PoseStack#mulPose(Quaternionfc)
// and changed ResourceManager#listResources to ResourceManager.Selector)
def pre263 = shared("pre263") {
mixins.from(file("src/pre263/resources/player_animation_library-pre26_3.mixins.json"))
}
// Shared by 26.2 and 26.3 (after 26.2 changed AvatarRenderState/Model APIs)
def mc262Plus = shared("mc262Plus") {
mixins.from(file("src/mc262Plus/resources/player_animation_library-26_2_plus.mixins.json"))
}
version("26.3") {
dependsOn(mc262Plus)
common { mixins.from(file("src/26.3/common/resources/player_animation_library-26_3.mixins.json")) }
fabric { loaderVersion("0.19.5"); fabricApi("0.146.1") }
neoforge { loaderVersion("26.3.0.1-alpha") }
}
version("26.2") {
dependsOn(pre263, mc262Plus)
common { mixins.from(file("src/26.2/common/resources/player_animation_library-26_2.mixins.json")) }
fabric { loaderVersion("0.19.5"); fabricApi("0.146.0") }
neoforge { loaderVersion("26.2.0.2-beta") }
}
version("26.1") {
dependsOn(pre263)
common { mixins.from(file("src/26.1/common/resources/player_animation_library-26_1.mixins.json")) }
fabric { loaderVersion("0.19.5"); fabricApi("0.145.4") }
neoforge { loaderVersion("26.1.2.15") }
}
}
How the DAG and API stubs resolve
For the configuration above, each layer gets its own exact Cloche API stub intersection:
| Layer | Directory | Inherited by | API stub intersection |
|---|---|---|---|
common | src/main | 26.1, 26.2, 26.3 (Fabric + NeoForge) | APIs common to all 6 targets |
pre263 | src/pre263 | 26.1, 26.2 (Fabric + NeoForge) | APIs common to 26.1 & 26.2 |
mc262Plus | src/mc262Plus | 26.2, 26.3 (Fabric + NeoForge) | APIs common to 26.2 & 26.3 |
26.2:common | src/26.2/common | 26.2:fabric, 26.2:neoforge | APIs common to 26.2 Fabric & NeoForge |
Everything propagates automatically:
- Classes and resources from
pre263are compiled once and packaged into26.1and26.2JARs — never duplicated into26.3. - Mixins declared in
pre263are automatically included in${graft_fabric_mixins}and${graft_neoforge_mixins}for26.1and26.2only. - Dependencies (
dependencies { ... }) and Jar-in-Jar includes (includes { ... }) declared on ashared(...)layer are automatically propagated to every inheriting version and leaf target.
Chaining shared(...) layers and custom directories
shared(...) layers can depend on other shared(...) layers — Graft resolves the dependency graph transitively:
val pre263 = shared("pre263")
val legacy = shared("legacy", "src/compat/legacy") {
dependsOn(pre263)
}
version("26.1") {
dependsOn(legacy) // transitively inherits both `legacy` and `pre263`
// ...
}
You can also customize a layer's source folder inside the block with dir("src/custom/path") (which maps <path>/java and <path>/resources).
Keeping shared code clean with abstract compat bases
A very effective pattern with shared(...) layers is putting an abstract base class in the shared layer and tiny concrete subclasses in each version:
src/main:interface PalVersionCompat(SPI)src/pre263:abstract class AbstractPalPre263Compat implements PalVersionCompat(implementsrotateDegreesandlistResourcesonce for both26.1and26.2)src/26.1/common:final class PalCompat261 extends AbstractPalPre263Compat(only the26.1-specific bits)src/26.2/common:final class PalCompat262 extends AbstractPalPre263Compat(only the26.2-specific bits)src/26.3/common:final class PalCompat263 implements PalVersionCompat(the26.3-specific bits)
Zero duplicated logic, full compile-time type safety against each version's exact Minecraft classes, and tiny universal JAR overhead.