Skip to content

Commit

Permalink
fix: support gradle cache (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Nielens <[email protected]>
  • Loading branch information
tnielens-centrica and tnielens authored Nov 28, 2024
1 parent 62b748f commit 383b3d3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ import org.gradle.api.file.FileCollection
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.CompileClasspath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.withGroovyBuilder
import java.io.File
Expand All @@ -42,6 +45,7 @@ import javax.inject.Inject
* This task generates a schema file from
* existing sources.
*/
@CacheableTask
abstract class JavaToSchemaTask @Inject constructor(
objectFactory: ObjectFactory):DefaultTask() {

Expand Down Expand Up @@ -79,6 +83,7 @@ abstract class JavaToSchemaTask @Inject constructor(
* @property inputDir
*/
@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
var inputDir: File
get() = inputDirProperty.get().asFile
set(value) = inputDirProperty.set(value)
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/intershop/gradle/jaxb/task/SchemaToJavaTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ 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.tasks.CacheableTask
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.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.withGroovyBuilder
import java.io.File
Expand All @@ -40,6 +43,7 @@ import javax.inject.Inject
* This task generates a java code from an
* existing jaxb configuration.
*/
@CacheableTask
open class SchemaToJavaTask @Inject constructor(
objectFactory: ObjectFactory): DefaultTask() {

Expand Down Expand Up @@ -204,6 +208,7 @@ open class SchemaToJavaTask @Inject constructor(
*/
@get:Optional
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
var schema: File?
get() {
return if(schemaProperty.isPresent) {
Expand All @@ -226,6 +231,7 @@ open class SchemaToJavaTask @Inject constructor(
*/
@get:Optional
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
var binding: File?
get() {
return if(bindingProperty.isPresent) {
Expand All @@ -248,6 +254,7 @@ open class SchemaToJavaTask @Inject constructor(
*/
@get:Optional
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
var catalog: File?
get() {
return if(catalogProperty.isPresent) {
Expand All @@ -270,6 +277,7 @@ open class SchemaToJavaTask @Inject constructor(
*/
@get:Optional
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
var schemas: FileCollection
get() = schemasProperty
set(value) {
Expand All @@ -283,6 +291,7 @@ open class SchemaToJavaTask @Inject constructor(
*/
@get:Optional
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
var bindings: FileCollection
get() = bindingsProperty
set(value) {
Expand Down Expand Up @@ -311,6 +320,7 @@ open class SchemaToJavaTask @Inject constructor(
* @property jaxbClasspathfiles
*/
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
val jaxbClasspathfiles : FileCollection by lazy {
val returnFiles = objectFactory.fileCollection()
// find files of original JASPER and Eclipse compiler
Expand Down
94 changes: 93 additions & 1 deletion src/test/groovy/com/intershop/gradle/jaxb/SamplesSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package com.intershop.gradle.jaxb


import com.intershop.gradle.test.AbstractIntegrationGroovySpec

import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS as SUCCESS
import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE
import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE as UP_TO_DATE

class SamplesSpec extends AbstractIntegrationGroovySpec {

Expand Down Expand Up @@ -1196,6 +1198,96 @@ class SamplesSpec extends AbstractIntegrationGroovySpec {
result.output.contains('Reusing configuration cache.')
}

def 'JavaToSchema supports the gradle build cache'() {
given:
copyResources('samples/character-escape')

buildFile << """
plugins {
id 'java'
id 'com.intershop.gradle.jaxb'
}
jaxb {
javaGen {
test {
schema = file('simple.xsd')
}
}
}
${DEPENDENCIES}
repositories {
mavenCentral()
}
""".stripIndent()

when:
List<String> args = ["clean", "jaxbJavaGenTest", "--build-cache"]

getPreparedGradleRunner()
.withArguments(args)
.withGradleVersion(gradleVersion)
.build()

def result = getPreparedGradleRunner()
.withArguments(args)
.withGradleVersion(gradleVersion)
.build()


then:
result.task(':jaxbJavaGenTest').outcome == FROM_CACHE

where:
gradleVersion << supportedGradleVersions
}

def 'SchemaToJava supports the gradle build cache'() {
given:
copyResources('samples/j2s-create-marshal')

buildFile << """
plugins {
id 'java'
id 'com.intershop.gradle.jaxb'
}
jaxb {
schemaGen {
test {
inputDir = file('src')
excludes = [ 'Main.java' ]
}
}
}
repositories {
mavenCentral()
}
""".stripIndent()


when:
List<String> args = ["clean", "jaxb", "--build-cache"]

getPreparedGradleRunner()
.withArguments(args)
.withGradleVersion(gradleVersion)
.build()

def result = getPreparedGradleRunner()
.withArguments(args)
.withGradleVersion(gradleVersion)
.build()

then:
result.task(':jaxbSchemaGenTest').outcome == FROM_CACHE

where:
gradleVersion << supportedGradleVersions
}

private boolean fileExists(String path) {
File f = new File(testProjectDir, path)
return f.isFile() && f.exists()
Expand Down

0 comments on commit 383b3d3

Please sign in to comment.