From 2bab98dbc9b7322fff911cae0006b79e62253293 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Mon, 6 Jan 2025 16:53:54 +0100 Subject: [PATCH] Add support for configuration cache Resolves #8 Replaces #148 --- CHANGELOG.md | 1 + .../MavenPluginDevelopmentPlugin.kt | 36 ++++++++++++++----- .../AbstractMavenPluginDevelopmentTask.kt | 22 +++--------- .../development/task/DependencyDescriptor.kt | 18 ++++++++++ .../task/GenerateHelpMojoSourcesTask.kt | 9 +++-- .../ConfigurationCacheFuncTest.groovy | 18 ++++++++++ 6 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 src/main/kotlin/de/benediktritter/maven/plugin/development/task/DependencyDescriptor.kt create mode 100644 src/test/groovy/de/benediktritter/maven/plugin/development/ConfigurationCacheFuncTest.groovy diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e8fc51..11afbef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Lower versions of Gradle might be supported but are not tested. * [Fixed] [278](https://github.com/britter/maven-plugin-development/issues/278) Remove deprecated code. In particular this the `generateHelpMojo` property was removed. Users should configure `helpMojoPackage` with the desired target package for the generated help mojo. +* [Fixed] [8](https://github.com/britter/maven-plugin-development/issues/278) Support configuration cache. ## Version 0.4.3 diff --git a/src/main/kotlin/de/benediktritter/maven/plugin/development/MavenPluginDevelopmentPlugin.kt b/src/main/kotlin/de/benediktritter/maven/plugin/development/MavenPluginDevelopmentPlugin.kt index f561f9b..656c244 100644 --- a/src/main/kotlin/de/benediktritter/maven/plugin/development/MavenPluginDevelopmentPlugin.kt +++ b/src/main/kotlin/de/benediktritter/maven/plugin/development/MavenPluginDevelopmentPlugin.kt @@ -17,20 +17,18 @@ package de.benediktritter.maven.plugin.development import de.benediktritter.maven.plugin.development.internal.MavenPluginDescriptor +import de.benediktritter.maven.plugin.development.task.DependencyDescriptor import de.benediktritter.maven.plugin.development.task.GenerateHelpMojoSourcesTask import de.benediktritter.maven.plugin.development.task.GenerateMavenPluginDescriptorTask import de.benediktritter.maven.plugin.development.task.UpstreamProjectDescriptor import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.SourceSetContainer import org.gradle.jvm.tasks.Jar -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.create -import org.gradle.kotlin.dsl.get -import org.gradle.kotlin.dsl.getByType -import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.* class MavenPluginDevelopmentPlugin : Plugin { @@ -51,14 +49,16 @@ class MavenPluginDevelopmentPlugin : Plugin { it.version.convention(project.provider { project.version.toString() }) it.name.convention(project.provider { project.name }) it.description.convention(project.provider { project.description }) - it.dependencies.convention(project.provider { project.configurations["runtimeClasspath"] }) + it.dependencies.convention(configurations["runtimeClasspath"]) } val generateHelpMojoTask = tasks.register("generateMavenPluginHelpMojoSources") { group = TASK_GROUP_NAME description = "Generates a Maven help mojo that documents the usage of the Maven plugin" - onlyIf { extension.helpMojoPackage.isPresent } + // capture helpMojoPackage property here for configuration cache compatibility + val helpMojoPkg = extension.helpMojoPackage + onlyIf { helpMojoPkg.isPresent } helpMojoPackage.set(extension.helpMojoPackage) outputDirectory.set(helpMojoDir) @@ -73,7 +73,16 @@ class MavenPluginDevelopmentPlugin : Plugin { extension.goalPrefix.orNull ) }) - runtimeDependencies.set(extension.dependencies) + runtimeDependencies.set(extension.dependencies.map { + it.resolvedConfiguration.resolvedArtifacts.map { artifact -> + DependencyDescriptor( + artifact.moduleVersion.id.group, + artifact.moduleVersion.id.name, + artifact.moduleVersion.id.version, + artifact.extension + ) + } + }) } val main = project.extensions.getByType()["main"] @@ -110,7 +119,16 @@ class MavenPluginDevelopmentPlugin : Plugin { extension.goalPrefix.orNull ) }) - runtimeDependencies.set(extension.dependencies) + runtimeDependencies.set(extension.dependencies.map { + it.resolvedConfiguration.resolvedArtifacts.map { artifact -> + DependencyDescriptor( + artifact.moduleVersion.id.group, + artifact.moduleVersion.id.name, + artifact.moduleVersion.id.version, + artifact.extension + ) + } + }) dependsOn(main.output, generateHelpMojoTask) } diff --git a/src/main/kotlin/de/benediktritter/maven/plugin/development/task/AbstractMavenPluginDevelopmentTask.kt b/src/main/kotlin/de/benediktritter/maven/plugin/development/task/AbstractMavenPluginDevelopmentTask.kt index f302104..0605fb6 100644 --- a/src/main/kotlin/de/benediktritter/maven/plugin/development/task/AbstractMavenPluginDevelopmentTask.kt +++ b/src/main/kotlin/de/benediktritter/maven/plugin/development/task/AbstractMavenPluginDevelopmentTask.kt @@ -21,12 +21,9 @@ import org.apache.maven.plugin.descriptor.PluginDescriptor import org.apache.maven.project.MavenProject import org.apache.maven.tools.plugin.DefaultPluginToolsRequest import org.apache.maven.tools.plugin.PluginToolsRequest -import org.codehaus.plexus.component.repository.ComponentDependency import org.gradle.api.DefaultTask -import org.gradle.api.artifacts.Configuration +import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property -import org.gradle.api.tasks.Classpath -import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Nested abstract class AbstractMavenPluginDevelopmentTask : DefaultTask() { @@ -34,8 +31,8 @@ abstract class AbstractMavenPluginDevelopmentTask : DefaultTask() { @get:Nested abstract val pluginDescriptor: Property - @get:[InputFiles Classpath] - abstract val runtimeDependencies: Property + @get:Nested + abstract val runtimeDependencies: ListProperty protected fun createPluginDescriptor(): PluginDescriptor { val pluginDescriptor = pluginDescriptor.get() @@ -47,18 +44,7 @@ abstract class AbstractMavenPluginDevelopmentTask : DefaultTask() { it.goalPrefix = pluginDescriptor.goalPrefix ?: PluginDescriptor.getGoalPrefixFromArtifactId(artifactId) it.name = pluginDescriptor.name it.description = pluginDescriptor.description - it.dependencies = getComponentDependencies() - } - } - - private fun getComponentDependencies(): List { - return runtimeDependencies.get().resolvedConfiguration.resolvedArtifacts.map { artifact -> - ComponentDependency().also { - it.groupId = artifact.moduleVersion.id.group - it.artifactId = artifact.moduleVersion.id.name - it.version = artifact.moduleVersion.id.version - it.type = artifact.extension - } + it.dependencies = runtimeDependencies.get().map { it.toComponentDependency() } } } diff --git a/src/main/kotlin/de/benediktritter/maven/plugin/development/task/DependencyDescriptor.kt b/src/main/kotlin/de/benediktritter/maven/plugin/development/task/DependencyDescriptor.kt new file mode 100644 index 0000000..e774f76 --- /dev/null +++ b/src/main/kotlin/de/benediktritter/maven/plugin/development/task/DependencyDescriptor.kt @@ -0,0 +1,18 @@ +package de.benediktritter.maven.plugin.development.task + +import org.codehaus.plexus.component.repository.ComponentDependency +import org.gradle.api.tasks.Input + +data class DependencyDescriptor( + @get:Input val groupId: String, + @get:Input val artifactId: String, + @get:Input val version: String, + @get:Input val type: String? +) { + fun toComponentDependency(): ComponentDependency = ComponentDependency().also { + it.groupId = this.groupId + it.artifactId = this.artifactId + it.version = this.version + it.type = this.type + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/benediktritter/maven/plugin/development/task/GenerateHelpMojoSourcesTask.kt b/src/main/kotlin/de/benediktritter/maven/plugin/development/task/GenerateHelpMojoSourcesTask.kt index 298dcca..f686120 100644 --- a/src/main/kotlin/de/benediktritter/maven/plugin/development/task/GenerateHelpMojoSourcesTask.kt +++ b/src/main/kotlin/de/benediktritter/maven/plugin/development/task/GenerateHelpMojoSourcesTask.kt @@ -43,13 +43,12 @@ abstract class GenerateHelpMojoSourcesTask : AbstractMavenPluginDevelopmentTask( private val loggerAdapter = MavenLoggerAdapter(logger) - private val generator = PluginHelpGenerator().also { gen -> - gen.enableLogging(loggerAdapter) - gen.setVelocityComponent(createVelocityComponent()) - } - @TaskAction fun generateHelpMojo() { + val generator = PluginHelpGenerator().also { gen -> + gen.enableLogging(loggerAdapter) + gen.setVelocityComponent(createVelocityComponent()) + } generator.setHelpPackageName(helpMojoPackage.get()) generator.setMavenProject(mavenProject()) generator.execute(outputDirectory.get().asFile) diff --git a/src/test/groovy/de/benediktritter/maven/plugin/development/ConfigurationCacheFuncTest.groovy b/src/test/groovy/de/benediktritter/maven/plugin/development/ConfigurationCacheFuncTest.groovy new file mode 100644 index 0000000..8af963f --- /dev/null +++ b/src/test/groovy/de/benediktritter/maven/plugin/development/ConfigurationCacheFuncTest.groovy @@ -0,0 +1,18 @@ +package de.benediktritter.maven.plugin.development + +class ConfigurationCacheFuncTest extends AbstractPluginFuncTest { + def "supports configuration cache"() { + given: + javaMojo() + buildFile << "mavenPlugin.helpMojoPackage.set('org.example.help')" + + and: + run("build", "--configuration-cache") + + when: + def result = run("build", "--configuration-cache") + + then: + result.output.contains("Reusing configuration cache.") + } +}