Last active
May 21, 2021 14:16
-
-
Save ghale/b2c2b04c82ea3b18ced51232c20c0204 to your computer and use it in GitHub Desktop.
Make QuarkusGenerateCode cacheable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
allprojects { | |
plugins.withId('io.quarkus') { | |
tasks.withType(io.quarkus.gradle.tasks.QuarkusGenerateCode).configureEach { task -> | |
def sourcesDirectories = getAccessibleField(task.class, 'sourcesDirectories') | |
def sources = sourcesDirectories.get(task).collect { it.toFile() } | |
task.inputs.files(sources) | |
.withPropertyName('sources') | |
.withPathSensitivity(PathSensitivity.RELATIVE) | |
def test = getAccessibleField(task.class, 'test') | |
def outputDirs = sourceSets.getByName(test.get(task) ? QUARKUS_TEST_GENERATED_SOURCES : QUARKUS_GENERATED_SOURCES).output.dirs | |
task.outputs.dirs(outputDirs) | |
.withPropertyName('generatedSources') | |
outputs.cacheIf { true } | |
} | |
tasks.named('clean').configure { | |
delete sourceSets.getByName(io.quarkus.gradle.tasks.QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES).output.dirs | |
delete sourceSets.getByName(io.quarkus.gradle.tasks.QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES).output.dirs | |
} | |
} | |
} | |
static Field getAccessibleField(Class<?> clazz, fieldName) { | |
for (Field field : clazz.declaredFields) { | |
if (field.name == fieldName) { | |
field.setAccessible(true) | |
return field | |
} | |
} | |
if (clazz.superclass != null) { | |
return getAccessibleField(clazz.superclass, fieldName) | |
} else { | |
throw new RuntimeException("Field '${fieldName}' not found") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment