Skip to content

Commit a4ba049

Browse files
jeremymailenJeremy Mailenclaude
authored
Fix issue #423: Configure ktlint classpath for custom tasks when no kotlin plugin is applied (#446)
- Moved task classpath configuration outside of plugin-specific block - Fixed dependency issues by using individual ktlint dependencies instead of CLI Co-authored-by: Jeremy Mailen <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent e9d5d6b commit a4ba049

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm
2525

2626
```kotlin
2727
plugins {
28-
id("org.jmailen.kotlinter") version "5.0.1"
28+
id("org.jmailen.kotlinter") version "5.0.2"
2929
}
3030
```
3131

@@ -36,7 +36,7 @@ plugins {
3636

3737
```groovy
3838
plugins {
39-
id "org.jmailen.kotlinter" version "5.0.1"
39+
id "org.jmailen.kotlinter" version "5.0.2"
4040
}
4141
```
4242

@@ -50,7 +50,7 @@ Root `build.gradle.kts`
5050

5151
```kotlin
5252
plugins {
53-
id("org.jmailen.kotlinter") version "5.0.1" apply false
53+
id("org.jmailen.kotlinter") version "5.0.2" apply false
5454
}
5555
```
5656

@@ -70,7 +70,7 @@ Root `build.gradle`
7070

7171
```groovy
7272
plugins {
73-
id 'org.jmailen.kotlinter' version "5.0.1" apply false
73+
id 'org.jmailen.kotlinter' version "5.0.2" apply false
7474
}
7575
```
7676

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ val githubUrl = "https://github.com/jeremymailen/kotlinter-gradle"
2222
val webUrl = "https://github.com/jeremymailen/kotlinter-gradle"
2323
val projectDescription = "Lint and formatting for Kotlin using ktlint with configuration-free setup on JVM and Android projects"
2424

25-
version = "5.0.1"
25+
version = "5.0.2"
2626
group = "org.jmailen.gradle"
2727
description = projectDescription
2828

src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ class KotlinterPlugin : Plugin<Project> {
3535
registerPrePushHookTask()
3636
}
3737

38+
// Configure all tasks including custom user tasks regardless of which plugins are applied
39+
// This ensures that custom tasks work even without a Kotlin plugin
40+
tasks.withType(ConfigurableKtLintTask::class.java).configureEach { task ->
41+
task.ktlintClasspath.from(ktlintConfiguration)
42+
}
43+
3844
// for known kotlin plugins, register tasks by convention.
3945
extendablePlugins.forEach { (pluginId, sourceResolver) ->
4046
pluginManager.withPlugin(pluginId) {
4147
val lintKotlin = registerParentLintTask()
4248
val formatKotlin = registerParentFormatTask()
4349

4450
registerSourceSetTasks(kotlinterExtension, sourceResolver, lintKotlin, formatKotlin)
45-
46-
// Configure all tasks including custom user tasks
47-
tasks.withType(ConfigurableKtLintTask::class.java).configureEach { task ->
48-
task.ktlintClasspath.from(ktlintConfiguration)
49-
}
5051
}
5152
}
5253
}
@@ -69,12 +70,21 @@ class KotlinterPlugin : Plugin<Project> {
6970
isCanBeConsumed = false
7071
isVisible = false
7172

72-
val dependencyProvider = provider {
73-
// Even though we don't use CLI, it bundles all the runtime dependencies we need.
74-
val ktlintVersion = kotlinterExtension.ktlintVersion
75-
this@createKtLintConfiguration.dependencies.create("com.pinterest.ktlint:ktlint-cli:$ktlintVersion")
73+
// Use individual ktlint dependencies rather than the CLI to avoid variant selection issues
74+
val ktlintVersion = kotlinterExtension.ktlintVersion
75+
val deps = listOf(
76+
"com.pinterest.ktlint:ktlint-rule-engine:$ktlintVersion",
77+
"com.pinterest.ktlint:ktlint-ruleset-standard:$ktlintVersion",
78+
"com.pinterest.ktlint:ktlint-cli-reporter-core:$ktlintVersion",
79+
"com.pinterest.ktlint:ktlint-cli-reporter-plain:$ktlintVersion",
80+
"com.pinterest.ktlint:ktlint-cli-reporter-html:$ktlintVersion",
81+
"com.pinterest.ktlint:ktlint-cli-reporter-checkstyle:$ktlintVersion",
82+
"com.pinterest.ktlint:ktlint-cli-reporter-json:$ktlintVersion",
83+
"com.pinterest.ktlint:ktlint-cli-reporter-sarif:$ktlintVersion",
84+
)
85+
deps.forEach { dep ->
86+
dependencies.add(project.dependencies.create(dep))
7687
}
77-
dependencies.addLater(dependencyProvider)
7888
}
7989
return configuration
8090
}

src/test/kotlin/org/jmailen/gradle/kotlinter/functional/CustomTaskTest.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jmailen.gradle.kotlinter.functional
22

3+
import org.gradle.testkit.runner.GradleRunner
34
import org.gradle.testkit.runner.TaskOutcome
45
import org.jmailen.gradle.kotlinter.functional.utils.editorConfig
56
import org.jmailen.gradle.kotlinter.functional.utils.kotlinClass
@@ -256,4 +257,49 @@ class CustomTaskTest : WithGradleTest.Kotlin() {
256257
assertTrue(projectRoot.resolve("build/lint-report.txt").exists())
257258
}
258259
}
260+
261+
@Test
262+
fun `ktLint custom task works without kotlin plugin applied`() {
263+
// Create a new project directory without the kotlin plugin
264+
val noKotlinProjectDir = File(testProjectDir.parentFile, "no-kotlin-project").apply {
265+
mkdirs()
266+
}
267+
268+
noKotlinProjectDir.resolve("settings.gradle") {
269+
writeText(settingsFile)
270+
}
271+
272+
noKotlinProjectDir.resolve("build.gradle") {
273+
// language=groovy
274+
val buildScript =
275+
"""
276+
plugins {
277+
// No kotlin plugin applied
278+
id 'org.jmailen.kotlinter'
279+
}
280+
$repositories
281+
282+
import org.jmailen.gradle.kotlinter.tasks.FormatTask
283+
284+
task customFormatTask(type: FormatTask) {
285+
source files('src')
286+
}
287+
"""
288+
writeText(buildScript)
289+
}
290+
291+
noKotlinProjectDir.resolve("src/main/kotlin/Simple.kt") {
292+
writeText(kotlinClass("Simple"))
293+
}
294+
295+
// Create a custom Gradle runner for this specific test
296+
val runner = GradleRunner.create()
297+
.withProjectDir(noKotlinProjectDir)
298+
.withArguments("customFormatTask", "--stacktrace")
299+
.withPluginClasspath()
300+
.forwardOutput()
301+
302+
val result = runner.build()
303+
assertEquals(TaskOutcome.SUCCESS, result.task(":customFormatTask")?.outcome)
304+
}
259305
}

0 commit comments

Comments
 (0)