diff --git a/README.md b/README.md index 05a29c7..20fb5a3 100644 --- a/README.md +++ b/README.md @@ -179,3 +179,11 @@ organizations. `telenor.dcapi()` Will add the legacy DC API repository from Prima Nexus. + +### Auto Exclude + +This plugin will automatically exclude the following +dependencies from all configurations: + +- `commons-logging:commons-logging` + unless `AUTO_EXCLUDE_KEEP` contains `commons-logging` diff --git a/src/main/kotlin/sh/tnn/gradle/TelenorNextPlugin.kt b/src/main/kotlin/sh/tnn/gradle/TelenorNextPlugin.kt index c773f2a..4d02d5b 100644 --- a/src/main/kotlin/sh/tnn/gradle/TelenorNextPlugin.kt +++ b/src/main/kotlin/sh/tnn/gradle/TelenorNextPlugin.kt @@ -2,6 +2,7 @@ package sh.tnn.gradle import org.gradle.api.Plugin import org.gradle.api.Project +import sh.tnn.gradle.plugins.auto_exclude.AutoExcludePlugin import sh.tnn.gradle.plugins.auto_version.AutoVersionPlugin import sh.tnn.gradle.plugins.dotenv.DotEnvPlugin import sh.tnn.gradle.plugins.github.GitHubPlugin @@ -18,6 +19,7 @@ class TelenorNextPlugin : Plugin { GitHubPlugin::class, TelenorPlugin::class, OpenTelemetryPlugin::class, + AutoExcludePlugin::class, ).forEach { project.pluginManager.apply(it.java) } diff --git a/src/main/kotlin/sh/tnn/gradle/plugins/auto_exclude/AutoExcludePlugin.kt b/src/main/kotlin/sh/tnn/gradle/plugins/auto_exclude/AutoExcludePlugin.kt new file mode 100644 index 0000000..41f30f1 --- /dev/null +++ b/src/main/kotlin/sh/tnn/gradle/plugins/auto_exclude/AutoExcludePlugin.kt @@ -0,0 +1,26 @@ +package sh.tnn.gradle.plugins.auto_exclude + +import org.gradle.api.Plugin +import org.gradle.api.Project +import sh.tnn.gradle.plugins.dotenv.DotEnvPlugin + +class AutoExcludePlugin : Plugin { + companion object { + val excluded = arrayOf( + "commons-logging:commons-logging" unlessKeep "commons-logging", + ) + } + + override fun apply(project: Project) { + val keep = (DotEnvPlugin.get(project, "AUTO_EXCLUDE_KEEP") ?: "").split(",").map { it.trim().lowercase() } + project.configurations.all { conf -> + excluded.forEach { notation -> + if (notation.second in keep) return@forEach + val (group, module) = notation.first.split(":") + conf.exclude(mapOf("group" to group, "module" to module)) + } + } + } +} + +private infix fun String.unlessKeep(other: String): Pair = this to other.lowercase()