-
Notifications
You must be signed in to change notification settings - Fork 336
Centralize spotless setup as a buildSrc convention plugin. #11384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
04cfa1a
d6439af
f3d3a5e
faf291a
e13795a
0fe5e87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,43 @@ | ||
| import org.gradle.api.attributes.java.TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE | ||
| import org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8 | ||
|
|
||
| plugins { | ||
| `java-gradle-plugin` | ||
| `kotlin-dsl` | ||
| `jvm-test-suite` | ||
| id("com.diffplug.spotless") version "8.4.0" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: This actually removes spotless from the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved it inside spotless convention plugin, see
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imho this looks incorrect to modify these project from the main dd-trace-java. |
||
| } | ||
|
|
||
| // The buildSrc still needs to target Java 8 as build time instrumentation and muzzle plugin | ||
| // allow to schedule workers on different JDK version. | ||
| // Compile with a modern JDK toolchain (21), but keep the emitted bytecode at Java 8. | ||
| // Rationale: the build-time instrumentation and muzzle plugins schedule Gradle workers | ||
| // on different JDK versions, including Java 8, so any class loaded from buildSrc must | ||
| // remain Java 8 compatible. | ||
| java { | ||
| toolchain { | ||
| languageVersion = JavaLanguageVersion.of(21) | ||
| } | ||
|
|
||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
|
|
||
| // Same split for Kotlin: compile on JDK 21 but target the Java 8 bytecode level so the | ||
| // Kotlin output can be loaded by the same Java-8 workers described above. | ||
| kotlin { | ||
| jvmToolchain(21) | ||
|
|
||
| compilerOptions { | ||
| jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8) | ||
| jvmTarget.set(JVM_1_8) | ||
| } | ||
| } | ||
|
|
||
| // Advertise Java 17 as the target JVM version when *resolving* dependencies. Several | ||
| // modern Gradle plugins (notably Spotless 8.x) publish only Java 17+ variants via Gradle | ||
| // Module Metadata; without this attribute, resolution fails with "no matching variant" | ||
| // because our targetCompatibility above declares Java 8. This affects dependency | ||
| // resolution only — it does not change the bytecode level we emit, which stays at Java 8. | ||
| configurations.configureEach { | ||
| if (isCanBeResolved) { | ||
| attributes.attribute(TARGET_JVM_VERSION_ATTRIBUTE, 17) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -63,6 +86,11 @@ gradlePlugin { | |
| id = "dd-trace-java.instrumentation-naming" | ||
| implementationClass = "datadog.gradle.plugin.naming.InstrumentationNamingPlugin" | ||
| } | ||
|
|
||
| create("spotless-conventions") { | ||
| id = "dd-trace-java.spotless-conventions" | ||
| implementationClass = "datadog.gradle.plugin.spotless.SpotlessConventionsPlugin" | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -78,6 +106,7 @@ dependencies { | |
| implementation(gradleApi()) | ||
|
|
||
| implementation("net.bytebuddy", "byte-buddy-gradle-plugin", "1.18.8") | ||
| implementation("com.diffplug.spotless:spotless-plugin-gradle:8.5.0") | ||
|
|
||
| implementation("org.eclipse.aether", "aether-connector-basic", "1.1.0") | ||
| implementation("org.eclipse.aether", "aether-transport-http", "1.1.0") | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,203 @@ | ||||||||||||||||||||||||||||||
| package datadog.gradle.plugin.spotless | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import com.diffplug.gradle.spotless.SpotlessExtension | ||||||||||||||||||||||||||||||
| import com.diffplug.gradle.spotless.SpotlessExtensionPredeclare | ||||||||||||||||||||||||||||||
| import org.gradle.api.Plugin | ||||||||||||||||||||||||||||||
| import org.gradle.api.Project | ||||||||||||||||||||||||||||||
| import org.gradle.kotlin.dsl.configure | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class SpotlessConventionsPlugin : Plugin<Project> { | ||||||||||||||||||||||||||||||
| private companion object { | ||||||||||||||||||||||||||||||
| const val GOOGLE_JAVA_FORMAT_VERSION = "1.35.0" | ||||||||||||||||||||||||||||||
| const val TABLE_TEST_FORMATTER_VERSION = "1.1.1" | ||||||||||||||||||||||||||||||
| const val KTLINT_VERSION = "1.8.0" | ||||||||||||||||||||||||||||||
| const val SCALAFMT_VERSION = "3.11.1" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| val KTLINT_EDITOR_CONFIG_OVERRIDE = | ||||||||||||||||||||||||||||||
| mapOf( | ||||||||||||||||||||||||||||||
| "ktlint_standard_trailing-comma-on-call-site" to "disabled", | ||||||||||||||||||||||||||||||
| "ktlint_standard_trailing-comma-on-declaration-site" to "disabled" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| val EXCLUDED_PROJECTS = setOf(":dd-java-agent:agent-jmxfetch") | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: This looks wrong. Spotless was applied to this module before.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, as far as I understand it is external project that injected into
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| override fun apply(project: Project) { | ||||||||||||||||||||||||||||||
| if (project.path in EXCLUDED_PROJECTS) { | ||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| project.pluginManager.apply("com.diffplug.spotless") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (project == project.rootProject) { | ||||||||||||||||||||||||||||||
| configurePredeclaredDependencies(project) | ||||||||||||||||||||||||||||||
| configureRootFormatting(project) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| configureProjectFormatting(project) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private fun configurePredeclaredDependencies(project: Project) { | ||||||||||||||||||||||||||||||
| project.extensions.configure<SpotlessExtensionPredeclare> { | ||||||||||||||||||||||||||||||
| java { | ||||||||||||||||||||||||||||||
| tableTestFormatter(TABLE_TEST_FORMATTER_VERSION) | ||||||||||||||||||||||||||||||
| googleJavaFormat(GOOGLE_JAVA_FORMAT_VERSION) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| groovyGradle { | ||||||||||||||||||||||||||||||
| greclipse() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| groovy { | ||||||||||||||||||||||||||||||
| greclipse() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| kotlinGradle { | ||||||||||||||||||||||||||||||
| ktlint(KTLINT_VERSION) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| kotlin { | ||||||||||||||||||||||||||||||
| ktlint(KTLINT_VERSION) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| scala { | ||||||||||||||||||||||||||||||
| scalafmt(SCALAFMT_VERSION) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private fun configureRootFormatting(project: Project) { | ||||||||||||||||||||||||||||||
| project.extensions.configure<SpotlessExtension> { | ||||||||||||||||||||||||||||||
| configureSkipSpotless(project, this) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| val rootExcludes = listOf( | ||||||||||||||||||||||||||||||
| "build/**", | ||||||||||||||||||||||||||||||
| "buildSrc/build/**", | ||||||||||||||||||||||||||||||
| "buildSrc/**/build/**", | ||||||||||||||||||||||||||||||
| "test-published-dependencies/**/build/**" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| kotlinGradle { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target( | ||||||||||||||||||||||||||||||
| "*.gradle.kts", | ||||||||||||||||||||||||||||||
| "buildSrc/**/*.gradle.kts", | ||||||||||||||||||||||||||||||
| "gradle/**/*.gradle.kts", | ||||||||||||||||||||||||||||||
| "test-published-dependencies/**/*.gradle.kts" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| targetExclude(rootExcludes) | ||||||||||||||||||||||||||||||
| ktlint(KTLINT_VERSION) | ||||||||||||||||||||||||||||||
| .editorConfigOverride(KTLINT_EDITOR_CONFIG_OVERRIDE) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| groovyGradle { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target( | ||||||||||||||||||||||||||||||
| "*.gradle", | ||||||||||||||||||||||||||||||
| "buildSrc/**/*.gradle", | ||||||||||||||||||||||||||||||
| "gradle/**/*.gradle", | ||||||||||||||||||||||||||||||
| "test-published-dependencies/**/*.gradle" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| targetExclude(rootExcludes) | ||||||||||||||||||||||||||||||
| greclipse().configFile("${project.rootDir}/gradle/enforcement/spotless-groovy.properties") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| kotlin { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("buildSrc/**/*.kt") | ||||||||||||||||||||||||||||||
| targetExclude(rootExcludes) | ||||||||||||||||||||||||||||||
| ktlint(KTLINT_VERSION) | ||||||||||||||||||||||||||||||
| .editorConfigOverride(KTLINT_EDITOR_CONFIG_OVERRIDE) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| java { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("buildSrc/**/*.java", "test-published-dependencies/**/src/**/*.java") | ||||||||||||||||||||||||||||||
| targetExclude(rootExcludes) | ||||||||||||||||||||||||||||||
| tableTestFormatter(TABLE_TEST_FORMATTER_VERSION) | ||||||||||||||||||||||||||||||
| googleJavaFormat(GOOGLE_JAVA_FORMAT_VERSION) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| format("markdown") { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("*.md", ".github/**/*.md", "src/**/*.md", "app*/**/*.md") | ||||||||||||||||||||||||||||||
| leadingTabsToSpaces() | ||||||||||||||||||||||||||||||
| endWithNewline() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| format("misc") { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target(".gitignore", "*.sh", "tooling/*.sh", ".gitlab/*.sh") | ||||||||||||||||||||||||||||||
| leadingTabsToSpaces() | ||||||||||||||||||||||||||||||
| trimTrailingWhitespace() | ||||||||||||||||||||||||||||||
| endWithNewline() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private fun configureProjectFormatting(project: Project) { | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: It seems that project-local markdown and misc formats were not carried over.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what is wrong, but I can see in my branch the following changes
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just that I don;t sse markdown and misc being configured, so that seems a bit strange, compared to previous dd-trace-java/gradle/spotless.gradle Lines 77 to 90 in 198a35d
|
||||||||||||||||||||||||||||||
| project.extensions.configure<SpotlessExtension> { | ||||||||||||||||||||||||||||||
| configureSkipSpotless(project, this) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| val commonExcludes = listOf("build/**", "src/test/resources/**") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| kotlinGradle { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("*.gradle.kts") | ||||||||||||||||||||||||||||||
| ktlint(KTLINT_VERSION) | ||||||||||||||||||||||||||||||
| .editorConfigOverride(KTLINT_EDITOR_CONFIG_OVERRIDE) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| groovyGradle { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("*.gradle") | ||||||||||||||||||||||||||||||
| greclipse().configFile("${project.rootDir}/gradle/enforcement/spotless-groovy.properties") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| project.pluginManager.withPlugin("java") { | ||||||||||||||||||||||||||||||
| java { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("src/**/*.java", "app*/**/*.java") | ||||||||||||||||||||||||||||||
| targetExclude(commonExcludes) | ||||||||||||||||||||||||||||||
| tableTestFormatter(TABLE_TEST_FORMATTER_VERSION) | ||||||||||||||||||||||||||||||
| googleJavaFormat(GOOGLE_JAVA_FORMAT_VERSION) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| project.pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { | ||||||||||||||||||||||||||||||
| kotlin { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("src/**/*.kt", "app*/**/*.kt") | ||||||||||||||||||||||||||||||
| targetExclude(commonExcludes) | ||||||||||||||||||||||||||||||
| ktlint(KTLINT_VERSION) | ||||||||||||||||||||||||||||||
| .editorConfigOverride(KTLINT_EDITOR_CONFIG_OVERRIDE) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| project.pluginManager.withPlugin("scala") { | ||||||||||||||||||||||||||||||
| scala { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("src/**/*.scala", "app*/**/*.scala") | ||||||||||||||||||||||||||||||
| targetExclude(commonExcludes) | ||||||||||||||||||||||||||||||
| scalafmt(SCALAFMT_VERSION) | ||||||||||||||||||||||||||||||
| .configFile("${project.rootDir}/gradle/enforcement/spotless-scalafmt.conf") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| project.pluginManager.withPlugin("groovy") { | ||||||||||||||||||||||||||||||
| groovy { | ||||||||||||||||||||||||||||||
| toggleOffOn() | ||||||||||||||||||||||||||||||
| target("src/**/*.groovy", "app*/**/*.groovy") | ||||||||||||||||||||||||||||||
| targetExclude(commonExcludes) | ||||||||||||||||||||||||||||||
| greclipse().configFile("${project.rootDir}/gradle/enforcement/spotless-groovy.properties") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private fun configureSkipSpotless(project: Project, spotless: SpotlessExtension) { | ||||||||||||||||||||||||||||||
| if (project.rootProject.hasProperty("skipSpotless")) { | ||||||||||||||||||||||||||||||
| spotless.setEnforceCheck(false) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.