Skip to content

Commit

Permalink
Dependency rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Dec 4, 2023
1 parent af12c73 commit 7dd1005
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gradle/plugins/dependency-rules-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
plugins {
`kotlin-dsl`
}

dependencies {
implementation(platform(project(":plugins-platform")))

implementation("dev.jacomet.gradle.plugins:logging-capabilities")
implementation("org.gradlex:java-ecosystem-capabilities")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.example.metadatarules.fixdependencies.TypesafeConfigGuiceRule
import org.example.metadatarules.status.ComponentStatusRule
import org.example.metadatarules.versionalignment.HttpComponentsPlatformRule
import org.example.metadatarules.versionalignment.PoiPlatformRule
import org.example.metadatarules.versionalignment.Slf4jPlatformRule
import org.example.metadatarules.versionalignment.ParentPomAsPlatformRule

plugins {
id("dev.jacomet.logging-capabilities")
id("org.gradlex.java-ecosystem-capabilities")
}

// Configure logging capabilities plugin to default to Slf4JSimple
loggingCapabilities.enforceSlf4JSimple()

dependencies.components {
// Versions that are not final get the 'integration' status
all<ComponentStatusRule>()

// Fix dependencies
withModule<TypesafeConfigGuiceRule>(TypesafeConfigGuiceRule.TYPESAFE_CONFIG_GUICE_MODULE)

// Define 'Alignment Platforms' (platforms for multi-component libraries without published BOM)
withModule<HttpComponentsPlatformRule>(HttpComponentsPlatformRule.HTTP_COMPONENTS_CLIENT)
withModule<PoiPlatformRule>(PoiPlatformRule.POI_COMMON)
withModule<Slf4jPlatformRule>(Slf4jPlatformRule.SLF4J_PARENT)

// Make parents usable as pure 'Alignment Platforms' - remove all constraints that do not concern the alignment
withModule<ParentPomAsPlatformRule>(HttpComponentsPlatformRule.HTTP_COMPONENTS_CLIENT)
withModule<ParentPomAsPlatformRule>(Slf4jPlatformRule.SLF4J_PARENT)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example.metadatarules.fixdependencies

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule

/**
* Removes the 'no_aop' classifier from the Guice dependency, because it
* no longer exists with Guice 5.0 to which we upgrade the dependency.
*/
@CacheableRule
abstract class TypesafeConfigGuiceRule : ComponentMetadataRule {
companion object {
const val TYPESAFE_CONFIG_GUICE_MODULE = "com.github.racc:typesafeconfig-guice"
}

override fun execute(context: ComponentMetadataContext) {
context.details.allVariants {
withDependencies {
removeIf { it.group == "com.google.inject" }
add("com.google.inject:guice")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.metadatarules.status

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule

/**
* Make sure versions that are not final released have a different status than 'release'.
* Otherwise, they will be considered when asking for the 'latest.release' version.
*
* (POM metadata does not support the 'status' concept and thus Gradle assumes everything is a 'release' by default)
*/
@CacheableRule
abstract class ComponentStatusRule : ComponentMetadataRule {
override fun execute(context: ComponentMetadataContext) {
val version = context.details.id.version
val lcVersion = version.lowercase()
if (lcVersion.contains("alpha")
|| lcVersion.contains("-b")
|| lcVersion.contains("beta")
|| lcVersion.contains("cr")
|| lcVersion.contains("m")
|| lcVersion.contains("rc")
|| lcVersion.startsWith("200")) {

context.details.status = "integration"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.example.metadatarules.versionalignment

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule

/**
* Extend the Parent POM 'httpcomponents-client' with constraints so that it can be used as platform
* to align the versions of all 'org.apache.httpcomponents' components.
*
* See:
* https://repo1.maven.org/maven2/org/apache/httpcomponents/httpcomponents-client/4.5.13/httpcomponents-client-4.5.13.pom
*
* <modules>
* <module>httpclient</module>
* <module>httpmime</module>
* <module>fluent-hc</module>
* <module>httpclient-cache</module>
* <module>httpclient-win</module>
* <module>httpclient-osgi</module>
* </modules>
*/
@CacheableRule
abstract class HttpComponentsPlatformRule : ComponentMetadataRule {
companion object {
const val HTTP_COMPONENTS_CLIENT = "org.apache.httpcomponents:httpcomponents-client"
}

override fun execute(context: ComponentMetadataContext) {
val version = context.details.id.version
context.details.allVariants {
withDependencyConstraints {
add("org.apache.httpcomponents:httpclient:$version")
add("org.apache.httpcomponents:httpmime:$version")
add("org.apache.httpcomponents:fluent-hc:$version")
add("org.apache.httpcomponents:httpclient-cache:$version")
add("org.apache.httpcomponents:httpclient-win:$version")
add("org.apache.httpcomponents:httpclient-osgi:$version")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.example.metadatarules.versionalignment

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule

/**
* Sometime libraries consisting of multiple components do not offer a BOM but have
* a 'Parent POM' that contains the information for alignment.
* However, these often also contain constraints on other components that we do not
* want to include. This Rule removes these additional constraints.
*/
@CacheableRule
abstract class ParentPomAsPlatformRule : ComponentMetadataRule {
override fun execute(context: ComponentMetadataContext) {
val group = context.details.id.group
context.details.allVariants {
withDependencyConstraints {
removeAll { it.group != group }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.example.metadatarules.versionalignment

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule

/**
* Extend the central 'poi' component with constraints so that it can be used as platform
* to align the versions of all 'org.apache.poi' components.
*
* See:
* https://repo1.maven.org/maven2/org/apache/poi/poi/5.2.0/poi-5.2.0.pom
*/
@CacheableRule
abstract class PoiPlatformRule : ComponentMetadataRule {
companion object {
const val POI_COMMON = "org.apache.poi:poi"
}

override fun execute(context: ComponentMetadataContext) {
val version = context.details.id.version
context.details.allVariants {
withDependencyConstraints {
add("org.apache.poi:poi-excelant:$version")
add("org.apache.poi:poi-ooxml:$version")
add("org.apache.poi:poi-scratchpad:$version")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.example.metadatarules.versionalignment

import org.gradle.api.artifacts.CacheableRule
import org.gradle.api.artifacts.ComponentMetadataContext
import org.gradle.api.artifacts.ComponentMetadataRule

/**
* Extend the Parent POM 'slf4j-parent' with constraints so that it can be used as platform
* to align the versions of all 'org.slf4j' components.
*
* See:
* https://repo1.maven.org/maven2/org/slf4j/slf4j-parent/1.7.36/slf4j-parent-1.7.36.pom
*/
@CacheableRule
abstract class Slf4jPlatformRule : ComponentMetadataRule {
companion object {
const val SLF4J_PARENT = "org.slf4j:slf4j-parent"
}

override fun execute(context: ComponentMetadataContext) {
val version = context.details.id.version
context.details.allVariants {
withDependencyConstraints {
add("org.slf4j:slf4j-api:$version")
add("org.slf4j:slf4j-simple:$version")
add("org.slf4j:slf4j-nop:$version")
add("org.slf4j:slf4j-jdk14:$version")
add("org.slf4j:slf4j-log4j12:$version")
add("org.slf4j:slf4j-reload4j:$version")
add("org.slf4j:slf4j-jcl:$version")
add("org.slf4j:slf4j-android:$version")
add("org.slf4j:slf4j-ext:$version")
add("org.slf4j:jcl-over-slf4j:$version")
add("org.slf4j:log4j-over-slf4j:$version")
add("org.slf4j:jul-to-slf4j:$version")
add("org.slf4j:osgi-over-slf4j:$version")
}
}
}
}
5 changes: 5 additions & 0 deletions gradle/plugins/plugins-platform/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
plugins {
id("java-platform")
}

dependencies.constraints {
api("dev.jacomet.gradle.plugins:logging-capabilities:0.11.1")
api("org.gradlex:java-ecosystem-capabilities:1.3.1")
}

0 comments on commit 7dd1005

Please sign in to comment.