Skip to content

Instantly share code, notes, and snippets.

@jakobkmar
Last active November 18, 2023 22:45
Show Gist options
  • Save jakobkmar/3c7e68ff57957d647a37ed568e5068c7 to your computer and use it in GitHub Desktop.
Save jakobkmar/3c7e68ff57957d647a37ed568e5068c7 to your computer and use it in GitHub Desktop.
Make fabric-loom's `include` transitive

fabric-loom transitive include

First of all, this should only be used in very special cases. Including dependencies in your mod transitively means that you always have to check whether there are any unwanted dependencies down the line, and exclude these.

Note: if you use languages other than Java, e.g. Kotlin, the likelyhood that you need transitive includes increases

Create the custom configuration

First, create a custom configuration. This is also where you specifiy the excludes.

val transitiveInclude: Configuration by configurations.creating {
    exclude(group = "com.mojang")
    exclude(group = "org.jetbrains.kotlin")
    exclude(group = "org.jetbrains.kotlinx")
}

The excludes are important, since transitive includes can add a lot of unwanted dependencies which are already on the classpath.

Add dependencies to the custom configuration

Now you can use the new configuration inside the dependencies block.

dependencies {
    transitiveInclude(implementation("org.litote.kmongo:kmongo-coroutine-serialization:4.6.0")!!)
}

Apply the custom configuration to include

The important part comes next, in order to resolve all dependencies transitively and add them to the non-transitive include configuration of fabric-loom, do this at the end of the dependencies block:

dependencies {
    // all your declared dependencies

    transitiveInclude.resolvedConfiguration.resolvedArtifacts.forEach {
        include(it.moduleVersion.id.toString())
    }
}

Done

And you are done. You can now build the project and check the META-INF/jars directory inside your jars. Again, make sure that you excluded all unwanted dependencies.

@Phelms215
Copy link

Could you please provide more context to your first step? where exactly are you creating this configuration?

@jakobkmar
Copy link
Author

@Phelms215 in the top level of the build.gradle.kts file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment