Skip to content

Commit

Permalink
Package services, lang and mod into a single jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Oct 10, 2023
1 parent fc751cc commit 894ba0f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 161 deletions.
125 changes: 81 additions & 44 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import net.fabricmc.loom.api.LoomGradleExtensionAPI
import net.fabricmc.loom.LoomGradleExtension
import net.fabricmc.loom.task.RemapJarTask
Expand Down Expand Up @@ -41,17 +42,21 @@ repositories {
}

// SpongeForge libraries
val forgeServiceLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeServiceLibraries")
val forgeGameLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeGameLibraries")
val forgeGameOnlyLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeGameOnlyLibraries")
val serviceLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeServiceLibraries")
val gameLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeGameLibraries")

val gameManagedLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeGameManagedLibraries")

val serviceShadedLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeServiceShadedLibraries")
val gameShadedLibrariesConfig: NamedDomainObjectProvider<Configuration> = configurations.register("spongeGameShadedLibraries")

configurations.named("forgeRuntimeLibrary") {
extendsFrom(forgeServiceLibrariesConfig.get())
extendsFrom(serviceLibrariesConfig.get())
}

// ModLauncher layers
val serviceLayerConfig: NamedDomainObjectProvider<Configuration> = configurations.register("serviceLayer") {
extendsFrom(forgeServiceLibrariesConfig.get())
extendsFrom(serviceLibrariesConfig.get())
extendsFrom(configurations.getByName("forgeDependencies"))
}
val langLayerConfig: NamedDomainObjectProvider<Configuration> = configurations.register("langLayer") {
Expand All @@ -60,7 +65,7 @@ val langLayerConfig: NamedDomainObjectProvider<Configuration> = configurations.r
val gameLayerConfig: NamedDomainObjectProvider<Configuration> = configurations.register("gameLayer") {
extendsFrom(serviceLayerConfig.get())
extendsFrom(langLayerConfig.get())
extendsFrom(forgeGameLibrariesConfig.get())
extendsFrom(gameLibrariesConfig.get())

afterEvaluate {
extendsFrom(configurations.getByName("minecraftNamed"))
Expand Down Expand Up @@ -186,7 +191,8 @@ extensions.configure(LoomGradleExtensionAPI::class) {
sourceSet(forgeAccessors)
sourceSet(forgeLaunch)

configuration(forgeGameOnlyLibrariesConfig.get())
configuration(gameManagedLibrariesConfig.get())
configuration(gameShadedLibrariesConfig.get())
}

create("sponge") {
Expand Down Expand Up @@ -224,10 +230,7 @@ dependencies {

forgeMixins.implementationConfigurationName(project(commonProject.path))

val serviceLibraries = forgeServiceLibrariesConfig.name
val gameLibraries = forgeGameLibrariesConfig.name
val gameOnlyLibraries = forgeGameOnlyLibrariesConfig.name

val serviceLibraries = serviceLibrariesConfig.name
serviceLibraries("org.spongepowered:plugin-spi:$apiPluginSpiVersion")
serviceLibraries(project(transformersProject.path))
serviceLibraries(platform("org.spongepowered:configurate-bom:$apiConfigurateVersion"))
Expand All @@ -242,19 +245,23 @@ dependencies {
exclude(group = "org.spongepowered", module = "configurate-core")
exclude(group = "org.checkerframework", module = "checker-qual")
}
serviceLibraries("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion")

val gameLibraries = gameLibrariesConfig.name
gameLibraries("org.spongepowered:spongeapi:$apiVersion")
gameLibraries("javax.inject:javax.inject:1")
gameLibraries("com.zaxxer:HikariCP:2.7.8")
gameLibraries(platform("net.kyori:adventure-bom:$apiAdventureVersion"))
gameLibraries("net.kyori:adventure-serializer-configurate4")

// due to dependency substitution we must add SpongeAPI manually
gameOnlyLibraries("org.spongepowered:spongeapi:$apiVersion") { isTransitive = false }
val serviceShadedLibraries = serviceShadedLibrariesConfig.name
serviceShadedLibraries(project(transformersProject.path)) { isTransitive = false }

val gameShadedLibraries = gameShadedLibrariesConfig.name
gameShadedLibraries("org.spongepowered:spongeapi:$apiVersion") { isTransitive = false }

afterEvaluate {
spongeImpl.copyModulesExcludingProvided(forgeGameLibrariesConfig.get(), serviceLayerConfig.get(), forgeGameOnlyLibrariesConfig.get())
spongeImpl.copyModulesExcludingProvided(serviceLibrariesConfig.get(), configurations.getByName("forgeDependencies"), serviceShadedLibrariesConfig.get())
spongeImpl.copyModulesExcludingProvided(gameLibrariesConfig.get(), serviceLayerConfig.get(), gameManagedLibrariesConfig.get())
}

testplugins?.also {
Expand Down Expand Up @@ -308,17 +315,6 @@ tasks {
manifest.from(forgeManifest)
from(forgeMixins.output)
}

val forgeAppLaunchServicesJar by registering(Jar::class) {
archiveClassifier.set("applaunch-services")
manifest.from(forgeManifest)

from(commonProject.sourceSets.named("applaunch").map { it.output })
from(forgeAppLaunch.output)

duplicatesStrategy = DuplicatesStrategy.WARN
}

val forgeLangJar by registering(Jar::class) {
archiveClassifier.set("lang")
manifest {
Expand All @@ -328,9 +324,19 @@ tasks {
from(forgeLang.output)
}

val forgeServicesDevJar by registering(Jar::class) {
archiveClassifier.set("services-dev")
manifest.from(forgeManifest)

from(commonProject.sourceSets.named("applaunch").map { it.output })
from(forgeAppLaunch.output)

duplicatesStrategy = DuplicatesStrategy.WARN
}

afterEvaluate {
withType(net.fabricmc.loom.task.AbstractRunTask::class) {
classpath += files(mods, forgeAppLaunchServicesJar, forgeLangJar, forgeAppLaunch.runtimeClasspath)
classpath += files(mods, forgeServicesDevJar, forgeLangJar)

argumentProviders += CommandLineArgumentProvider {
mixinConfigs.asSequence()
Expand All @@ -349,56 +355,87 @@ tasks {

val emitDependencies by registering(org.spongepowered.gradle.impl.OutputDependenciesToJson::class) {
group = "sponge"
this.dependencies("main", forgeGameOnlyLibrariesConfig)
this.dependencies("main", gameManagedLibrariesConfig)
this.excludedDependencies(gameShadedLibrariesConfig)

outputFile.set(installerResources.map { it.file("org/spongepowered/forge/applaunch/loading/moddiscovery/libraries.json") })
}
named(forgeAppLaunch.processResourcesTaskName).configure {
dependsOn(emitDependencies)
}

shadowJar {
val forgeServicesShadowJar by register("servicesShadowJar", ShadowJar::class) {
group = "shadow"
archiveClassifier.set("services")

mergeServiceFiles()
configurations = listOf(serviceShadedLibrariesConfig.get())
exclude("META-INF/INDEX.LIST", "META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA", "module-info.class")

manifest {
attributes("Multi-Release" to true)
from(forgeManifest)
}

from(commonProject.sourceSets.named("applaunch").map { it.output })
from(forgeAppLaunch.output)

// Make sure to relocate access widener so that we don't conflict with other coremods
relocate("net.fabricmc.accesswidener", "org.spongepowered.forge.libs.accesswidener")
}

shadowJar {
group = "shadow"
archiveClassifier.set("mod-dev")

configurations = listOf()
mergeServiceFiles()
configurations = listOf(gameShadedLibrariesConfig.get())

archiveClassifier.set("universal-dev")
manifest {
attributes(mapOf(
attributes(
"Superclass-Transformer" to "common.superclasschange,forge.superclasschange",
"Multi-Release" to true,
"MixinConfigs" to mixinConfigs.joinToString(",")
))
)
from(forgeManifest)
}

from(commonProject.sourceSets.main.map { it.output })
from(commonProject.sourceSets.named("mixins").map {it.output })
from(commonProject.sourceSets.named("accessors").map {it.output })
from(commonProject.sourceSets.named("launch").map {it.output })
from(transformersProject.sourceSets.named("main").map { it.output })

from(forgeLaunch.output)
from(forgeAccessors.output)
from(forgeMixins.output)

// TODO jarjar service and lang

// Make sure to relocate access widener so that we don't conflict with other
// coremods also using access widener
relocate("net.fabricmc.accesswidener", "org.spongepowered.forge.libs.accesswidener")
}

val remapShadowJar = register("remapShadowJar", RemapJarTask::class) {
group = "loom"
archiveClassifier.set("mod")

inputFile.set(shadowJar.flatMap { it.archiveFile })
archiveClassifier.set("universal")
dependsOn(shadowJar)
atAccessWideners.add("common.accesswidener")
}

val universalJar = register("universalJar", Jar::class) {
group = "build"
archiveClassifier.set("universal")

manifest.from(forgeManifest)

from(forgeServicesShadowJar.archiveFile.map { zipTree(it) })

into("jars") {
from(remapShadowJar)
rename("spongeforge-(.*)-mod.jar", "spongeforge-mod.jar")

from(forgeLangJar)
rename("spongeforge-(.*)-lang.jar", "spongeforge-lang.jar")
}
}

assemble {
dependsOn(remapShadowJar)
dependsOn(universalJar)
}

templateResources {
Expand Down

This file was deleted.

Loading

2 comments on commit 894ba0f

@Zidane
Copy link
Member

@Zidane Zidane commented on 894ba0f Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to re-do (perhaps not the best word but) the overall gradle config for the Sponge implementation to do jar in jar? Almost all deps (if not all) come from common and the accesswidener relocate would be in common's services jar. Its a small example but is what immediately came to my mind after reading this config.

This would also let us take a step back and look at our source sets. AppLaunch and Launch in common/vanilla are...weird and I wonder if we've over-engineered this somewhat. It may be I'm overthinking but I always want to slim down and make sure we're doing only what we need to and nothing more.
 
Thoughts?

@Yeregorix
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The accesswidener relocate is temporary. We are now using access transformers (converted from access wideners by arch loom) in SF production so we will no longer need to shade the accesswidener lib at all in SF jar. Only SV will.
Overall there are tons of cleanup I would like to do in SC and SV but for now I'm focusing on having a runnable SF jar.
The next step (in a separate PR) will be to update SC, SV and modlauncher-transformers to match SF's ModLauncher version. I think this will be the appropriate time to refactor the remaining Gradle stuff because I don't know yet what SV will look like when ModLauncher will be updated.

Please sign in to comment.