Skip to content

Commit d49f3c2

Browse files
committed
jar shadowing
1 parent e73ca67 commit d49f3c2

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

build.gradle

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Please check https://github.com/GregTechCEu/Buildscripts/blob/master/build.gradle for updates.
66
*/
77

8+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
9+
import com.gtnewhorizons.retrofuturagradle.mcp.ReobfuscatedJar
810
import com.modrinth.minotaur.dependencies.ModDependency
911
import com.modrinth.minotaur.dependencies.VersionDependency
1012
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
@@ -25,6 +27,7 @@ plugins {
2527
id 'com.modrinth.minotaur' version '2.7.+' apply false
2628
id 'com.diffplug.spotless' version '6.13.0' apply false
2729
id 'com.palantir.git-version' version '3.0.0' apply false
30+
id 'com.github.johnrengelman.shadow' version '8.1.1' apply false
2831
}
2932

3033
if (verifySettingsGradle()) {
@@ -61,6 +64,9 @@ propertyDefaultIfUnset("includeWellKnownRepositories", true)
6164
propertyDefaultIfUnset("includeCommonDevEnvMods", true)
6265
propertyDefaultIfUnset("noPublishedSources", false)
6366
propertyDefaultIfUnset("forceEnableMixins", false)
67+
propertyDefaultIfUnset("usesShadowedDependencies", false)
68+
propertyDefaultIfUnset("minimizeShadowedDependencies", true)
69+
propertyDefaultIfUnset("relocateShadowedDependencies", true)
6470
propertyDefaultIfUnset("modrinthProjectId", "")
6571
propertyDefaultIfUnset("modrinthRelations", "")
6672
propertyDefaultIfUnset("curseForgeProjectId", "")
@@ -189,6 +195,11 @@ if (project.file('.git/HEAD').isFile() || project.file('.git').isFile()) {
189195
apply plugin: 'com.palantir.git-version'
190196
}
191197

198+
// Shadowing
199+
if (usesShadowedDependencies.toBoolean()) {
200+
apply plugin: 'com.github.johnrengelman.shadow'
201+
}
202+
192203

193204
// Configure Java
194205

@@ -356,6 +367,13 @@ repositories {
356367
configurations {
357368
embed
358369
implementation.extendsFrom(embed)
370+
371+
if (usesShadowedDependencies.toBoolean()) {
372+
for (config in [compileClasspath, runtimeClasspath, testCompileClasspath, testRuntimeClasspath]) {
373+
config.extendsFrom(shadowImplementation)
374+
config.extendsFrom(shadowCompile)
375+
}
376+
}
359377
}
360378

361379
dependencies {
@@ -534,21 +552,7 @@ if (usesMixins.toBoolean()) {
534552

535553
jar {
536554
manifest {
537-
def attribute_map = [:]
538-
if (coreModClass) {
539-
attribute_map['FMLCorePlugin'] = "${modGroup}.${coreModClass}"
540-
}
541-
if (!containsMixinsAndOrCoreModOnly.toBoolean() && (usesMixins.toBoolean() || coreModClass)) {
542-
attribute_map['FMLCorePluginContainsFMLMod'] = true
543-
}
544-
if (accessTransformersFile) {
545-
attribute_map['FMLAT'] = accessTransformersFile.toString()
546-
}
547-
548-
if (usesMixins.toBoolean()) {
549-
attribute_map['ForceLoadAsMod'] = !containsMixinsAndOrCoreModOnly.toBoolean()
550-
}
551-
attributes(attribute_map)
555+
attributes(getManifestAttributes())
552556
}
553557

554558
// Add all embedded dependencies into the jar
@@ -571,6 +575,66 @@ tasks.register('apiJar', Jar) {
571575
}
572576
}
573577

578+
// Configure shadow jar task
579+
if (usesShadowedDependencies.toBoolean()) {
580+
tasks.named('shadowJar', ShadowJar).configure {
581+
manifest {
582+
attributes(getManifestAttributes())
583+
}
584+
// Only shadow classes that are actually used, if enabled
585+
if (minimizeShadowedDependencies.toBoolean()) {
586+
minimize()
587+
}
588+
configurations = [
589+
project.configurations.shadowImplementation,
590+
project.configurations.shadowCompile
591+
]
592+
archiveClassifier.set('dev')
593+
if (relocateShadowedDependencies.toBoolean()) {
594+
relocationPrefix = modGroup + '.shadow'
595+
enableRelocation = true
596+
}
597+
}
598+
configurations.runtimeElements.outgoing.artifacts.clear()
599+
configurations.apiElements.outgoing.artifacts.clear()
600+
configurations.runtimeElements.outgoing.artifact(tasks.named('shadowJar', ShadowJar))
601+
configurations.apiElements.outgoing.artifact(tasks.named('shadowJar', ShadowJar))
602+
tasks.named('jar', Jar) {
603+
enabled = false
604+
finalizedBy(tasks.shadowJar)
605+
}
606+
tasks.named('reobfJar', ReobfuscatedJar) {
607+
inputJar.set(tasks.named('shadowJar', ShadowJar).flatMap({it.archiveFile}))
608+
}
609+
AdhocComponentWithVariants javaComponent = (AdhocComponentWithVariants) project.components.findByName('java')
610+
javaComponent.withVariantsFromConfiguration(configurations.shadowRuntimeElements) {
611+
skip()
612+
}
613+
for (runTask in ['runClient', 'runServer']) {
614+
tasks.named(runTask).configure {
615+
dependsOn('shadowJar')
616+
}
617+
}
618+
}
619+
620+
def getManifestAttributes() {
621+
def attributes = [:]
622+
if (coreModClass) {
623+
attributes['FMLCorePlugin'] = "${modGroup}.${coreModClass}"
624+
}
625+
if (!containsMixinsAndOrCoreModOnly.toBoolean() && (usesMixins.toBoolean() || coreModClass)) {
626+
attributes['FMLCorePluginContainsFMLMod'] = true
627+
}
628+
if (accessTransformersFile) {
629+
attributes['FMLAT'] = accessTransformersFile.toString()
630+
}
631+
632+
if (usesMixins.toBoolean()) {
633+
attributes['ForceLoadAsMod'] = !containsMixinsAndOrCoreModOnly.toBoolean()
634+
}
635+
return attributes
636+
}
637+
574638

575639
// IDE Configuration
576640

@@ -705,7 +769,7 @@ if (modrinthApiKey.isPresent() || deploymentDebug.toBoolean()) {
705769
gameVersions = [minecraftVersion]
706770
loaders = ["forge"]
707771
debugMode = deploymentDebug.toBoolean()
708-
uploadFile = file("build/libs/${archivesBaseName}-${version}.jar")
772+
uploadFile = reobfJar
709773
additionalFiles = getSecondaryArtifacts()
710774
}
711775
if (modrinthRelations.size() != 0) {
@@ -906,7 +970,7 @@ def propertyDefaultIfUnsetWithEnvVar(String propertyName, defaultValue, String e
906970
}
907971

908972
def getSecondaryArtifacts() {
909-
def secondaryArtifacts = [jar]
973+
def secondaryArtifacts = [usesShadowedDependencies.toBoolean() ? tasks.shadowJar : tasks.jar]
910974
if (!noPublishedSources.toBoolean()) secondaryArtifacts += [sourcesJar]
911975
if (apiPackage) secondaryArtifacts += [apiJar]
912976
return secondaryArtifacts

gradle.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ includeWellKnownRepositories = true
6666
includeCommonDevEnvMods = true
6767

6868

69+
# If enabled, you may use 'shadowCompile' for dependencies. They will be integrated in your jar. It is your
70+
# responsibility check the licence and request permission for distribution, if required.
71+
usesShadowedDependencies = false
72+
# If disabled, won't remove unused classes from shaded dependencies. Some libraries use reflection to access
73+
# their own classes, making the minimization unreliable.
74+
minimizeShadowedDependencies = true
75+
# If disabled, won't rename the shadowed classes.
76+
relocateShadowedDependencies = true
77+
78+
6979
# Publishing to modrinth requires you to set the MODRINTH_API_KEY environment variable to your current modrinth API token.
7080

7181
# The project's ID on Modrinth. Can be either the slug or the ID.

0 commit comments

Comments
 (0)