1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-04 15:43:00 +00:00

Share some dependency exclusion code

- Disable Gradle module metadata for all Minecraft projects
 - Run dependency exclusion code for all projects

We need the former for MDG on 1.21, so might as well do some other
cleanup while we're here.
This commit is contained in:
Jonathan Coates
2025-01-13 00:00:10 +00:00
parent 9a914e75c4
commit ef0af67e96
8 changed files with 35 additions and 76 deletions

View File

@@ -12,6 +12,7 @@ publishing {
register<MavenPublication>("maven") { register<MavenPublication>("maven") {
artifactId = base.archivesName.get() artifactId = base.archivesName.get()
from(components["java"]) from(components["java"])
suppressAllPomMetadataWarnings()
pom { pom {
name = "CC: Tweaked" name = "CC: Tweaked"

View File

@@ -67,7 +67,7 @@ abstract class CCTweakedExtension(
/** /**
* Dependencies excluded from published artifacts. * Dependencies excluded from published artifacts.
*/ */
private val excludedDeps: ListProperty<Dependency> = project.objects.listProperty(Dependency::class.java) internal val excludedDeps: ListProperty<Dependency> = project.objects.listProperty(Dependency::class.java)
/** All source sets referenced by this project. */ /** All source sets referenced by this project. */
val sourceSets = sourceDirectories.map { x -> x.map { it.sourceSet } } val sourceSets = sourceDirectories.map { x -> x.map { it.sourceSet } }
@@ -249,13 +249,6 @@ abstract class CCTweakedExtension(
excludedDeps.add(dep) excludedDeps.add(dep)
} }
/**
* Configure a [MavenDependencySpec].
*/
fun configureExcludes(spec: MavenDependencySpec) {
for (dep in excludedDeps.get()) spec.exclude(dep)
}
private fun <T> gitProvider(default: T, command: List<String>, process: (String) -> T): Provider<T> { private fun <T> gitProvider(default: T, command: List<String>, process: (String) -> T): Provider<T> {
val baseResult = project.providers.exec { val baseResult = project.providers.exec {
commandLine = listOf("git", "-C", project.rootDir.absolutePath) + command commandLine = listOf("git", "-C", project.rootDir.absolutePath) + command

View File

@@ -8,6 +8,9 @@ import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.plugins.PublishingPlugin
import org.gradle.jvm.toolchain.JavaLanguageVersion import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.plugins.ide.idea.model.IdeaModel import org.gradle.plugins.ide.idea.model.IdeaModel
import org.jetbrains.gradle.ext.IdeaExtPlugin import org.jetbrains.gradle.ext.IdeaExtPlugin
@@ -26,6 +29,13 @@ class CCTweakedPlugin : Plugin<Project> {
cct.sourceDirectories.add(SourceSetReference.internal(sourceSets.getByName("main"))) cct.sourceDirectories.add(SourceSetReference.internal(sourceSets.getByName("main")))
} }
project.plugins.withType(PublishingPlugin::class.java) {
val publishing = project.extensions.getByType(PublishingExtension::class.java)
publishing.publications.withType(MavenPublication::class.java) {
excludeMavenDependencies(project, this, cct.excludedDeps)
}
}
project.plugins.withType(IdeaExtPlugin::class.java) { extendIdea(project) } project.plugins.withType(IdeaExtPlugin::class.java) { extendIdea(project) }
} }

View File

@@ -4,70 +4,43 @@
package cc.tweaked.gradle package cc.tweaked.gradle
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.plugins.BasePluginExtension import org.gradle.api.plugins.BasePluginExtension
import org.gradle.api.provider.Provider
import org.gradle.api.publish.maven.MavenPublication import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.specs.Spec
/** /**
* A dependency in a POM file. * A dependency in a POM file.
*/ */
data class MavenDependency(val groupId: String?, val artifactId: String?, val version: String?, val scope: String?) private data class MavenDependency(val groupId: String?, val artifactId: String?) {
constructor(project: Project, dep: Dependency) : this(
/** dep.group,
* A spec specifying which dependencies to include/exclude. when (dep) {
*/ is ProjectDependency -> project.project(dep.path).extensions.getByType(BasePluginExtension::class.java).archivesName.get()
class MavenDependencySpec { else -> dep.name
private val excludeSpecs = mutableListOf<Spec<MavenDependency>>() },
)
fun exclude(spec: Spec<MavenDependency>) {
excludeSpecs.add(spec)
}
fun exclude(dep: Dependency) {
exclude {
// We have to cheat a little for project dependencies, as the project name doesn't match the artifact group.
val name = when (dep) {
is ProjectDependency -> dep.dependencyProject.extensions.getByType(BasePluginExtension::class.java).archivesName.get()
else -> dep.name
}
(dep.group.isNullOrEmpty() || dep.group == it.groupId) &&
(name.isNullOrEmpty() || name == it.artifactId) &&
(dep.version.isNullOrEmpty() || dep.version == it.version)
}
}
fun exclude(dep: MinimalExternalModuleDependency) {
exclude {
dep.module.group == it.groupId && dep.module.name == it.artifactId
}
}
fun isIncluded(dep: MavenDependency) = !excludeSpecs.any { it.isSatisfiedBy(dep) }
} }
/** /**
* Configure dependencies present in this publication's POM file. * Remove dependencies in a POM file based on a list of dependencies
* *
* While this approach is very ugly, it's the easiest way to handle it! * While this approach is very ugly, it's the easiest way to handle it!
*/ */
fun MavenPublication.mavenDependencies(action: MavenDependencySpec.() -> Unit) { internal fun excludeMavenDependencies(project: Project, publication: MavenPublication, excluded: Provider<out List<Dependency>>) {
val spec = MavenDependencySpec() val excludedSpecs = excluded.map { xs -> xs.map { MavenDependency(project, it) } }
action(spec)
pom.withXml { publication.pom.withXml {
val dependencies = XmlUtil.findChild(asNode(), "dependencies") ?: return@withXml val dependencies = XmlUtil.findChild(asNode(), "dependencies") ?: return@withXml
dependencies.children().map { it as groovy.util.Node }.forEach { dependencies.children().map { it as groovy.util.Node }.forEach {
val dep = MavenDependency( val dep = MavenDependency(
groupId = XmlUtil.findChild(it, "groupId")?.text(), groupId = XmlUtil.findChild(it, "groupId")?.text(),
artifactId = XmlUtil.findChild(it, "artifactId")?.text(), artifactId = XmlUtil.findChild(it, "artifactId")?.text(),
version = XmlUtil.findChild(it, "version")?.text(),
scope = XmlUtil.findChild(it, "scope")?.text(),
) )
if (!spec.isIncluded(dep)) it.parent().remove(it) if (excludedSpecs.get().contains(dep)) it.parent().remove(it)
} }
} }
} }

View File

@@ -11,10 +11,12 @@ import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.dsl.DependencyHandler import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.plugins.BasePlugin import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.publish.tasks.GenerateModuleMetadata
import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.withType
/** /**
* This sets up a separate client-only source set, and extends that and the main/common source set with additional * This sets up a separate client-only source set, and extends that and the main/common source set with additional
@@ -95,6 +97,12 @@ class MinecraftConfigurations private constructor(private val project: Project)
sourceDirectories.add(SourceSetReference.internal(client)) sourceDirectories.add(SourceSetReference.internal(client))
} }
// We can't create accurate module metadata for our additional capabilities,
// so disable module metadata.
project.tasks.withType(GenerateModuleMetadata::class.java).configureEach {
isEnabled = false
}
// Register a task to check there are no conflicts with the core project. // Register a task to check there are no conflicts with the core project.
val checkDependencyConsistency = val checkDependencyConsistency =
project.tasks.register("checkDependencyConsistency", DependencyCheck::class.java) { project.tasks.register("checkDependencyConsistency", DependencyCheck::class.java) {

View File

@@ -126,5 +126,3 @@ val runData by tasks.registering(MergeTrees::class) {
val runExampleData by tasks.registering(MergeTrees::class) { val runExampleData by tasks.registering(MergeTrees::class) {
configureForDatagen(sourceSets.examples.get(), "src/examples/generatedResources") configureForDatagen(sourceSets.examples.get(), "src/examples/generatedResources")
} }
tasks.withType(GenerateModuleMetadata::class).configureEach { isEnabled = false }

View File

@@ -288,17 +288,6 @@ modPublishing {
output = tasks.remapJar output = tasks.remapJar
} }
tasks.withType(GenerateModuleMetadata::class).configureEach { isEnabled = false }
publishing {
publications {
named("maven", MavenPublication::class) {
mavenDependencies {
cct.configureExcludes(this)
}
}
}
}
modrinth { modrinth {
required.project("fabric-api") required.project("fabric-api")
} }

View File

@@ -230,19 +230,6 @@ tasks.register("checkClient") {
dependsOn(runGametestClient) dependsOn(runGametestClient)
} }
// Upload tasks
modPublishing { modPublishing {
output = tasks.reobfJar output = tasks.reobfJar
} }
publishing {
publications {
named("maven", MavenPublication::class) {
mavenDependencies {
cct.configureExcludes(this)
exclude(libs.jei.forge.get())
}
}
}
}