From 61749637e2c8adea37931e0267804a636f5db32b Mon Sep 17 00:00:00 2001 From: Tim Nielens Date: Wed, 24 Jan 2024 14:29:45 +0100 Subject: [PATCH] support configuration cache --- .../com/intershop/gradle/jaxb/JaxbPlugin.kt | 16 ++++--- .../gradle/jaxb/task/SchemaToJavaTask.kt | 42 +++---------------- .../gradle/jaxb/utils/JaxbCodeGenRegistry.kt | 8 ++++ 3 files changed, 24 insertions(+), 42 deletions(-) diff --git a/src/main/kotlin/com/intershop/gradle/jaxb/JaxbPlugin.kt b/src/main/kotlin/com/intershop/gradle/jaxb/JaxbPlugin.kt index fb585ae..3052f30 100644 --- a/src/main/kotlin/com/intershop/gradle/jaxb/JaxbPlugin.kt +++ b/src/main/kotlin/com/intershop/gradle/jaxb/JaxbPlugin.kt @@ -27,6 +27,7 @@ import org.gradle.api.Task import org.gradle.api.artifacts.DependencySet import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.api.provider.Provider /** * Plugin Class implementation. @@ -62,7 +63,7 @@ open class JaxbPlugin: Plugin { addJaxbConfiguration(this) - project.rootProject.gradle.sharedServices.registerIfAbsent( + val jaxbCodeGenRegistryProvider = project.rootProject.gradle.sharedServices.registerIfAbsent( JAXB_REGISTRY, JaxbCodeGenRegistry::class.java) { it.maxParallelUsages.set(1) @@ -72,8 +73,8 @@ open class JaxbPlugin: Plugin { jaxbTask.group = JaxbExtension.JAXB_TASK_GROUP jaxbTask.description = TASKDESCRIPTION - configureJavaCodeGenTasks(this, extension, jaxbTask) - configureSchemaCodeGenTasks(this, extension, jaxbTask) + configureJavaCodeGenTasks(this, extension, jaxbTask, jaxbCodeGenRegistryProvider) + configureSchemaCodeGenTasks(this, extension, jaxbTask, jaxbCodeGenRegistryProvider) } } @@ -87,7 +88,8 @@ open class JaxbPlugin: Plugin { */ private fun configureJavaCodeGenTasks(project: Project, extension: JaxbExtension, - jaxbTask: Task) { + jaxbTask: Task, + jaxbCodeGenRegistryProvider: Provider) { extension.javaGen.all { schemaToJava: SchemaToJava -> project.tasks.maybeCreate(schemaToJava.taskName, SchemaToJavaTask::class.java).apply { description = "Generate java code for " + schemaToJava.name @@ -106,6 +108,7 @@ open class JaxbPlugin: Plugin { provideSchema(schemaToJava.schemaProvider) provideBinding(schemaToJava.bindingProvider) provideCatalog(schemaToJava.catalogProvider) + usesService(jaxbCodeGenRegistryProvider) schemas = schemaToJava.schemas bindings = schemaToJava.bindings @@ -137,7 +140,8 @@ open class JaxbPlugin: Plugin { */ private fun configureSchemaCodeGenTasks(project: Project, extension: JaxbExtension, - jaxbTask: Task) { + jaxbTask: Task, + jaxbCodeGenRegistryProvider: Provider) { extension.schemaGen.all { javaToSchema: JavaToSchema -> project.tasks.maybeCreate(javaToSchema.taskName, JavaToSchemaTask::class.java).apply { description = "Generate Schema for " + javaToSchema.name @@ -149,7 +153,7 @@ open class JaxbPlugin: Plugin { provideNamespaceconfigs(javaToSchema.namespaceconfigsProvider) provideEpisode(javaToSchema.episodeProvider) provideAntTaskClassName(javaToSchema.antTaskClassNameProvider) - + usesService(jaxbCodeGenRegistryProvider) jaxbTask.dependsOn(this) } } diff --git a/src/main/kotlin/com/intershop/gradle/jaxb/task/SchemaToJavaTask.kt b/src/main/kotlin/com/intershop/gradle/jaxb/task/SchemaToJavaTask.kt index 3b14265..d126475 100644 --- a/src/main/kotlin/com/intershop/gradle/jaxb/task/SchemaToJavaTask.kt +++ b/src/main/kotlin/com/intershop/gradle/jaxb/task/SchemaToJavaTask.kt @@ -16,36 +16,25 @@ */ package com.intershop.gradle.jaxb.task -import com.intershop.gradle.jaxb.JaxbPlugin.Companion.JAXB_REGISTRY import com.intershop.gradle.jaxb.extension.JaxbExtension import org.gradle.api.DefaultTask -import org.gradle.api.GradleException import org.gradle.api.file.Directory import org.gradle.api.file.FileCollection import org.gradle.api.file.RegularFile import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.JavaPlugin import org.gradle.api.provider.Provider -import org.gradle.api.services.BuildService -import org.gradle.api.services.BuildServiceParameters -import org.gradle.api.services.BuildServiceRegistry -import org.gradle.api.services.internal.BuildServiceProvider -import org.gradle.api.services.internal.BuildServiceRegistryInternal import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.CompileClasspath import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.InputFiles -import org.gradle.api.tasks.Internal import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction -import org.gradle.internal.resources.ResourceLock import org.gradle.kotlin.dsl.withGroovyBuilder import java.io.File -import java.util.* import javax.inject.Inject -import kotlin.collections.ArrayList /** * This task generates a java code from an @@ -323,7 +312,7 @@ open class SchemaToJavaTask @Inject constructor( */ @get:InputFiles val jaxbClasspathfiles : FileCollection by lazy { - val returnFiles = project.files() + val returnFiles = objectFactory.fileCollection() // find files of original JASPER and Eclipse compiler returnFiles.from(project.configurations.findByName(JaxbExtension.JAXB_CONFIGURATION_NAME)) returnFiles @@ -336,7 +325,7 @@ open class SchemaToJavaTask @Inject constructor( */ @get:Classpath val addjaxbClasspathfiles : FileCollection by lazy { - val returnFiles = project.files() + val returnFiles = objectFactory.fileCollection() // find files of original JASPER and Eclipse compiler returnFiles.from(project.configurations.findByName(JaxbExtension.ADD_JAXB_CONFIGURATION_NAME)) returnFiles @@ -370,24 +359,6 @@ open class SchemaToJavaTask @Inject constructor( */ fun provideOutputDir(outputDir: Provider) = outputDirProperty.set(outputDir) - @Internal - override fun getSharedResources(): List { - val locks = ArrayList(super.getSharedResources()) - val serviceRegistry = services.get(BuildServiceRegistryInternal::class.java) - val jaxbResourceProvider = getBuildService(serviceRegistry) - val resource = serviceRegistry.forService(jaxbResourceProvider) - locks.add(resource?.resourceLock) - - return Collections.unmodifiableList(locks) - } - - private fun getBuildService(registry: BuildServiceRegistry): BuildServiceProvider, - out BuildServiceParameters> { - val registration = registry.registrations.findByName(JAXB_REGISTRY) - ?: throw GradleException ("Unable to find build service with name '$name'.") - - return registration.getService() as BuildServiceProvider<*,*> - } /** * This creates the java code fron a JAXB configuration. @@ -421,13 +392,13 @@ open class SchemaToJavaTask @Inject constructor( argMap["catalog"] = catalogProperty.get().asFile.absolutePath } - if(project.logger.isInfoEnabled) { - project.logger.info("Argeument for javagen: {}", argMap) + if(logger.isInfoEnabled) { + logger.info("Argeument for javagen: {}", argMap) } val taskClassPath = jaxbClasspathfiles + addjaxbClasspathfiles - project.logger.info(" -> Locked XJC Gradle Task to prevent the parallel execution!") + logger.info(" -> Locked XJC Gradle Task to prevent the parallel execution!") System.setProperty("javax.xml.accessExternalSchema", "all") System.setProperty("javax.xml.accessExternalDTD", "all") @@ -444,7 +415,6 @@ open class SchemaToJavaTask @Inject constructor( "classpath" to taskClassPath.asPath) - "jaxb"(argMap) { "classpath" { "pathelement"( "path" to classpathfiles.asPath) @@ -468,7 +438,7 @@ open class SchemaToJavaTask @Inject constructor( if(! strictValidation) { "arg"( "value" to "-nv") } - if (project.logger.isInfoEnabled || project.logger.isDebugEnabled) { + if (logger.isInfoEnabled || logger.isDebugEnabled) { "arg"( "value" to "-verbose") } else { "arg"( "value" to "-quiet") diff --git a/src/main/kotlin/com/intershop/gradle/jaxb/utils/JaxbCodeGenRegistry.kt b/src/main/kotlin/com/intershop/gradle/jaxb/utils/JaxbCodeGenRegistry.kt index 8965944..62cf4fb 100644 --- a/src/main/kotlin/com/intershop/gradle/jaxb/utils/JaxbCodeGenRegistry.kt +++ b/src/main/kotlin/com/intershop/gradle/jaxb/utils/JaxbCodeGenRegistry.kt @@ -19,4 +19,12 @@ package com.intershop.gradle.jaxb.utils import org.gradle.api.services.BuildService import org.gradle.api.services.BuildServiceParameters +/** + * Prevents interference issues between --configuration-cache, gradle parallelism and ant tasks. + * See JaxbPlugin for maxParallelism of this BuildService. + * To reproduce issues: + * - run this plugin on a project with multiple generation tasks + * - raise the maxParallelism of this BuildService to 2 or more + * - run gradle with --configuration-cache + */ abstract class JaxbCodeGenRegistry: BuildService