Skip to content

Commit

Permalink
Add support for configuration cache
Browse files Browse the repository at this point in the history
Resolves #8
Replaces #148
  • Loading branch information
britter committed Jan 6, 2025
1 parent d9313cc commit 2bab98d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {

Expand All @@ -51,14 +49,16 @@ class MavenPluginDevelopmentPlugin : Plugin<Project> {
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<GenerateHelpMojoSourcesTask>("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)
Expand All @@ -73,7 +73,16 @@ class MavenPluginDevelopmentPlugin : Plugin<Project> {
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<SourceSetContainer>()["main"]
Expand Down Expand Up @@ -110,7 +119,16 @@ class MavenPluginDevelopmentPlugin : Plugin<Project> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ 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() {

@get:Nested
abstract val pluginDescriptor: Property<MavenPluginDescriptor>

@get:[InputFiles Classpath]
abstract val runtimeDependencies: Property<Configuration>
@get:Nested
abstract val runtimeDependencies: ListProperty<DependencyDescriptor>

protected fun createPluginDescriptor(): PluginDescriptor {
val pluginDescriptor = pluginDescriptor.get()
Expand All @@ -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<ComponentDependency> {
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() }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.")
}
}

0 comments on commit 2bab98d

Please # to comment.