Skip to content

Commit

Permalink
support configuration cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens authored and tnielens-centrica committed Jan 25, 2024
1 parent 5e231e1 commit 6174963
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
16 changes: 10 additions & 6 deletions src/main/kotlin/com/intershop/gradle/jaxb/JaxbPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,7 +63,7 @@ open class JaxbPlugin: Plugin<Project> {

addJaxbConfiguration(this)

project.rootProject.gradle.sharedServices.registerIfAbsent(
val jaxbCodeGenRegistryProvider = project.rootProject.gradle.sharedServices.registerIfAbsent(
JAXB_REGISTRY,
JaxbCodeGenRegistry::class.java) {
it.maxParallelUsages.set(1)
Expand All @@ -72,8 +73,8 @@ open class JaxbPlugin: Plugin<Project> {
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)
}
}

Expand All @@ -87,7 +88,8 @@ open class JaxbPlugin: Plugin<Project> {
*/
private fun configureJavaCodeGenTasks(project: Project,
extension: JaxbExtension,
jaxbTask: Task) {
jaxbTask: Task,
jaxbCodeGenRegistryProvider: Provider<JaxbCodeGenRegistry>) {
extension.javaGen.all { schemaToJava: SchemaToJava ->
project.tasks.maybeCreate(schemaToJava.taskName, SchemaToJavaTask::class.java).apply {
description = "Generate java code for " + schemaToJava.name
Expand All @@ -106,6 +108,7 @@ open class JaxbPlugin: Plugin<Project> {
provideSchema(schemaToJava.schemaProvider)
provideBinding(schemaToJava.bindingProvider)
provideCatalog(schemaToJava.catalogProvider)
usesService(jaxbCodeGenRegistryProvider)

schemas = schemaToJava.schemas
bindings = schemaToJava.bindings
Expand Down Expand Up @@ -137,7 +140,8 @@ open class JaxbPlugin: Plugin<Project> {
*/
private fun configureSchemaCodeGenTasks(project: Project,
extension: JaxbExtension,
jaxbTask: Task) {
jaxbTask: Task,
jaxbCodeGenRegistryProvider: Provider<JaxbCodeGenRegistry>) {
extension.schemaGen.all { javaToSchema: JavaToSchema ->
project.tasks.maybeCreate(javaToSchema.taskName, JavaToSchemaTask::class.java).apply {
description = "Generate Schema for " + javaToSchema.name
Expand All @@ -149,7 +153,7 @@ open class JaxbPlugin: Plugin<Project> {
provideNamespaceconfigs(javaToSchema.namespaceconfigsProvider)
provideEpisode(javaToSchema.episodeProvider)
provideAntTaskClassName(javaToSchema.antTaskClassNameProvider)

usesService(jaxbCodeGenRegistryProvider)
jaxbTask.dependsOn(this)
}
}
Expand Down
42 changes: 6 additions & 36 deletions src/main/kotlin/com/intershop/gradle/jaxb/task/SchemaToJavaTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -370,24 +359,6 @@ open class SchemaToJavaTask @Inject constructor(
*/
fun provideOutputDir(outputDir: Provider<Directory>) = outputDirProperty.set(outputDir)

@Internal
override fun getSharedResources(): List<ResourceLock> {
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 BuildService<*>,
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.
Expand Down Expand Up @@ -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")
Expand All @@ -444,7 +415,6 @@ open class SchemaToJavaTask @Inject constructor(
"classpath" to taskClassPath.asPath)



"jaxb"(argMap) {
"classpath" {
"pathelement"( "path" to classpathfiles.asPath)
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuildServiceParameters.None>

0 comments on commit 6174963

Please sign in to comment.