Skip to content

Instantly share code, notes, and snippets.

@ghale
ghale / build.gradle
Last active May 20, 2021 00:00
Detecting overlapping outputs
task checkForOverlappingOutputs {
doLast {
def rootNode = new TreeNode()
rootNode.name = ''
// Gather the outputs of all tasks
allprojects {
tasks.all { task ->
try {
task.outputs.files.each { file ->
@ghale
ghale / build.gradle
Last active December 21, 2023 13:41
Build Service for restricting only one test task to execute at a time
import org.gradle.api.services.*
subprojects {
apply plugin: OneTestTaskOnly
}
class OneTestTaskOnly implements Plugin<Project> {
void apply(Project project) {
// Register a service to constrain parallelism of test tasks to 1
Provider<SharedResource> testLimitingService = project.gradle.sharedServices.registerIfAbsent("testLimitingService", SharedResource) { spec ->
@ghale
ghale / build.gradle
Last active December 29, 2020 14:59
Annotation processor with output arguments
plugins {
id 'java'
}
tasks.withType(JavaCompile).configureEach {
// Set up the localization values
def localizationMethod = "foo"
def localizationOutputDir = file("${buildDir}/localizationTool")
// Add the argument provider to the compiler arguments
@ghale
ghale / settings.gradle
Created November 25, 2020 14:29
Add tags for build cache enabled/disabled
gradle.settingsEvaluated {
def buildCacheEnabled = gradle.startParameter.buildCacheEnabled
gradleEnterprise {
buildScan {
tag buildCacheEnabled ? "CACHED" : "NOTCACHED"
value "Local Build Cache", (buildCacheEnabled && buildCache.local?.enabled) ? "enabled" : "disabled"
value "Remote Build Cache", (buildCacheEnabled && buildCache.remote?.enabled) ? "enabled" : "disabled"
}
}
@ghale
ghale / build.gradle
Created November 23, 2020 15:42
Tag builds where tests are filtered
tasks.withType(Test).configureEach { task ->
project.gradle.taskGraph.beforeTask {
if (task == it) {
if (!includes.empty || !excludes.empty || !filter.includePatterns.empty || !filter.excludePatterns.empty || !filter.commandLineIncludePatterns.empty) {
buildScan.tag 'FILTERED_TESTS'
}
}
}
}
@ghale
ghale / build.gradle
Created November 12, 2020 19:38
Workaround for commonSourceSet issue in kotlin compile tasks
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompileWithWorkers) { task ->
project.gradle.taskGraph.beforeTask {
if (task == it) {
// Get the 'commonSourceSet' property and make it accessible
def field = task.class.superclass.superclass.superclass.getDeclaredField('commonSourceSet')
field.setAccessible(true)
// Store the current value, and if the 'source' property contains everything in 'commonSourceSet',
// clear it out for the purposes of snapshotting (i.e. the directories are already tracked by a different
// input property)
@ghale
ghale / gist:ce897f0f764c1121dbaab5dc3760d4b6
Last active November 11, 2020 21:17
Investigate commonSourceSet difference in Kotlin compile tasks
allprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompileWithWorkers) { task ->
project.gradle.taskGraph.beforeTask {
if (task == it) {
def field = task.class.superclass.superclass.superclass.getDeclaredField('commonSourceSet')
field.setAccessible(true)
project.rootProject.buildScan.value "${task.path}.commonSourceSets", field.get(task).files.join("\n")
def compilationBySourceSet = task.project.ext."kotlin.compilations.bySourceSets".get()
project.rootProject.buildScan.value "${task.path}.compilations",
@ghale
ghale / build.gradle
Last active December 7, 2020 16:26
Fix ProGuardTask to be cacheable
tasks.withType(ProGuardTask).configureEach { task ->
// Enable caching for the proguard tasks
outputs.cacheIf { true }
// Fix inputs and outputs so that caching works
def originalOutJars = []
def originalInJars = []
def originalLibraryJars = []
def originalConfigurationFiles = []
project.gradle.taskGraph.beforeTask {
@ghale
ghale / build.gradle
Created October 22, 2020 14:43
Add path sensitivity to lombok.config input on JavaCompile tasks
import java.lang.reflect.Field
import java.lang.reflect.Modifier
allprojects {
pluginManager.withPlugin('io.freefair.lombok') {
def javaExtension = project.getConvention().getPlugin(JavaPluginConvention.class)
// Reconfigure the lombok config input for each compile task
javaExtension.sourceSets.all { sourceSet ->
def compileJavaTask = project.tasks.named(sourceSet.compileJavaTaskName)
@ghale
ghale / captureFingerprints.gradle
Last active March 13, 2025 17:59
Capture task classpath fingerprints
def fingerprinter = services.get(org.gradle.internal.fingerprint.classpath.ClasspathFingerprinter)
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { task ->
doFirst {
ClassLoader classLoader = task.getClass().classLoader
while (classLoader instanceof URLClassLoader) {
def fingerprints = [] as Set
def allFiles = [] as Set
classLoader.getURLs().each {
fingerprints.add(["${task.path}:${file(it.file).name}", "${fingerprinter.fingerprint(files(it.file)).hash}"])
allFiles.add(file(it.file))