Skip to content

Instantly share code, notes, and snippets.

@ghale
ghale / build.gradle
Created September 27, 2022 19:33
Custom metadata rule to select a specific minor version of a library
repositories {
mavenCentral()
}
configurations {
foo
}
dependencies {
foo 'org.scala-lang:scala-library:latest.2_12'
@ghale
ghale / build.gradle
Created March 11, 2022 17:07
Capture dynamic version TTL for all configurations
allprojects { p ->
rootProject.buildScan.buildFinished {
def field = getAccessibleField(org.gradle.api.internal.artifacts.ivyservice.resolutionstrategy.DefaultCachePolicy, "keepDynamicVersionsFor")
p.configurations.each { config ->
rootProject.buildScan.value "${p.name}.${config.name}.keepDynamicVersionsFor", "${field.get(config.resolutionStrategy.cachePolicy)/1000}s"
}
}
}
def getAccessibleField(Class<?> clazz, String fieldName) {
@ghale
ghale / gradle-enterprise-custom-user-data.groovy
Created November 4, 2021 14:20
Capturing Maven builds without parallel enabled
def maxThreads = session.request.degreeOfConcurrency
if (maxThreads > 1) {
buildScan.tag "PARALLEL"
}
@ghale
ghale / build.gradle
Created October 13, 2021 17:40
Finding values of file properties for kotlin compile tasks
subprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompileWithWorkers).configureEach { task ->
doFirst {
task.inputs.visitRegisteredProperties(new PropertyVisitor(task))
}
}
}
import org.gradle.api.internal.tasks.properties.*
class PropertyVisitor extends PropertyVisitor.Adapter {
@ghale
ghale / build.gradle
Last active September 13, 2021 20:08
Capture Java compiler arguments
allprojects {
tasks.withType(JavaCompile).configureEach { task ->
doFirst {
rootProject.buildScan.value "${task.path}.compilerArgs", options.compilerArgs.join("\n")
}
}
}
@ghale
ghale / throttle.gradle
Last active June 20, 2022 13:30
Captures OS X CPU throttling as a custom value/tag
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import java.nio.charset.Charset
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
/**
* This Gradle script captures the thermal stats as reported by the OS 'pmset' command,
@ghale
ghale / build.gradle
Last active August 3, 2021 18:23
Removing empty kapt output directory created at configuration time
gradle.taskGraph.whenReady { graph ->
graph.beforeTask { task ->
if (task instanceof org.jetbrains.kotlin.gradle.internal.KaptTask) {
if (task.destinationDir.exists()) {
if (task.destinationDir.listFiles().length == 0) {
logger.warn("Cleaning empty output directory at ${task.destinationDir}")
assert task.destinationDir.delete()
} else {
logger.warn("Not cleaning output directory at ${task.destinationDir} as it exists and is not empty. See the '${task.path}.destinationDir' custom value to see contents.")
captureOutputDirContents(task)
@ghale
ghale / build.gradle
Last active July 28, 2021 20:29
Checking for an untracked output
task checkForUntrackedOutput
gradle.taskGraph.whenReady { graph ->
def kaptTasks = []
graph.allTasks.findAll { it instanceof org.jetbrains.kotlin.gradle.internal.KaptTask }.each { task ->
kaptTasks << task
}
if (graph.hasTask(':checkForUntrackedOutput')) {
@ghale
ghale / build.gradle
Last active May 21, 2021 14:16
Make QuarkusGenerateCode cacheable
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')
@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 ->