Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Gradle 9 Support [#937](https://github.com/JLLeitschuh/ktlint-gradle/pull/937)

## [13.0.0] - 2025-07-07

- remove support for ktlint < 1 [#889](https://github.com/JLLeitschuh/ktlint-gradle/pull/889)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.gradle.api.Task
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.util.GradleVersion
import org.jlleitschuh.gradle.ktlint.reporter.CustomReporter
import org.jlleitschuh.gradle.ktlint.tasks.BaseKtLintCheckTask
import org.jlleitschuh.gradle.ktlint.tasks.KtLintCheckTask
import org.jlleitschuh.gradle.ktlint.tasks.KtLintFormatTask
Expand Down Expand Up @@ -47,7 +46,6 @@ open class KtlintBasePlugin : Plugin<Project> {
"ktlint",
KtlintExtension::class.java,
target.objects,
target.container(CustomReporter::class.java) { CustomReporter(it) },
filterTargetApplier,
kotlinScriptAdditionalPathApplier
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jlleitschuh.gradle.ktlint

import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.file.ConfigurableFileTree
Expand All @@ -11,27 +10,24 @@ import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.util.ConfigureUtil
import org.gradle.kotlin.dsl.domainObjectContainer
import org.gradle.kotlin.dsl.newInstance
import org.jlleitschuh.gradle.ktlint.reporter.CustomReporter
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
import javax.inject.Inject

/**
* Extension class for configuring the [KtlintPlugin].
* @param filterTargetApplier When [KtlintExtension.filter] is called, this function is executed.
*/
@Suppress("UnstableApiUsage")
open class KtlintExtension
internal constructor(
open class KtlintExtension @Inject internal constructor(
objectFactory: ObjectFactory,
projectLayout: ProjectLayout,
customReportersContainer: NamedDomainObjectContainer<CustomReporter>,
private val filterTargetApplier: FilterApplier,
kotlinScriptAdditionalPathApplier: KotlinScriptAdditionalPathApplier
) {
internal val reporterExtension = ReporterExtension(
customReportersContainer,
objectFactory
)
val reporterExtension = objectFactory.newInstance(ReporterExtension::class)

/**
* The version of KtLint to use.
Expand Down Expand Up @@ -154,13 +150,14 @@ internal constructor(
}
}

class ReporterExtension(
val customReporters: NamedDomainObjectContainer<CustomReporter>,
open class ReporterExtension @Inject constructor(
objectFactory: ObjectFactory
) {
internal val reporters: SetProperty<ReporterType> = objectFactory.setProperty {
set(emptySet())
}
val customReporters: NamedDomainObjectContainer<CustomReporter> =
objectFactory.domainObjectContainer(CustomReporter::class) { CustomReporter(it) }

/**
* Use one of default Ktlint output reporter
Expand All @@ -176,10 +173,8 @@ internal constructor(
/**
* Add 3rd party reporters.
*/
fun customReporters(configuration: Closure<NamedDomainObjectContainer<CustomReporter>>) {
// This method is needed for Groovy interop
// See https://discuss.gradle.org/t/multi-level-dsl-for-plugin-extension/19029/16
ConfigureUtil.configure(configuration, customReporters)
fun customReporters(configuration: Action<NamedDomainObjectContainer<CustomReporter>>) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a binary API breaking change? IE. if projects are depending upon this plugin as a direct dependency rather than as a script, anything compiled against our APIs won't work anymore, correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly... I know gradle has some stuff to auto convert between SAMs and Closures, but I'm not sure if it would help here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in any case, this is the way is is supposed to be done in Gradle now, so I think at most, I'll make a note about it in release notes

configuration.execute(customReporters)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ class KtlintPluginAndroidTest : AbstractPluginTest() {
minimumJava = 11,
maximumJava = 17
),
MAX_GRADLE_MIN_AGP(
GradleVersion.version(TestVersions.maxSupportedGradleVersion),
TestVersions.minAgpVersion,
TestVersions.minSupportedKotlinPluginVersion
),
Comment on lines -91 to -95
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why drop this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old AGP doesnt work with Gradle 9, so this combination no longer works

MAX(
GradleVersion.version(TestVersions.maxSupportedGradleVersion),
TestVersions.maxAgpVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jlleitschuh.gradle.ktlint.testdsl

import net.swiftzer.semver.SemVer
import org.gradle.util.GradleVersion
import org.jlleitschuh.gradle.ktlint.KtlintPlugin
import org.junit.jupiter.api.extension.ExtensionContext
Expand All @@ -8,15 +9,26 @@ import org.junit.jupiter.params.provider.ArgumentsProvider
import java.util.stream.Stream
import kotlin.streams.asStream

fun getCurrentJavaVersion(): String {
return Runtime::class.java.getPackage().specificationVersion // java 8
?: Runtime::class.java.getMethod("version").invoke(null).toString() // java 9+
}

@Suppress("ConstPropertyName")
object TestVersions {
const val minSupportedGradleVersion = "7.6.3" // lowest version for testing
const val maxSupportedGradleVersion = "8.13"
val maxSupportedGradleVersion =
// gradle 9 requires Java 17
if (SemVer.parse(getCurrentJavaVersion()).major >= 17) {
"9.0.0"
} else {
"8.14.3"
}
val pluginVersion = System.getProperty("project.version")
?: KtlintPlugin::class.java.`package`.implementationVersion
?: error("Unable to determine plugin version.")
const val minSupportedKotlinPluginVersion = "1.6.21"
const val maxSupportedKotlinPluginVersion = "2.1.21"
const val maxSupportedKotlinPluginVersion = "2.2.0"
const val minAgpVersion = "4.1.0"
const val maxAgpVersion = "8.8.0"
}
Expand All @@ -25,7 +37,6 @@ object TestVersions {
@Retention(AnnotationRetention.RUNTIME)
annotation class GradleTestVersions(
val minVersion: String = TestVersions.minSupportedGradleVersion,
val maxVersion: String = TestVersions.maxSupportedGradleVersion,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were never overriding this anyway, and making it an annotation property was preventing me from making it dynamic based Java version

val additionalVersions: Array<String> = []
)

Expand Down Expand Up @@ -55,7 +66,7 @@ open class GradleArgumentsProvider : ArgumentsProvider {
} else {
GradleVersion.version(versionsAnnotation.minVersion)
}
val maxGradleVersion = GradleVersion.version(versionsAnnotation.maxVersion)
val maxGradleVersion = GradleVersion.version(TestVersions.maxSupportedGradleVersion)
val additionalGradleVersions = versionsAnnotation
.additionalVersions
.map(GradleVersion::version)
Expand Down
Loading