Skip to content

Instantly share code, notes, and snippets.

project.afterEvaluate {
tasks.withType(MergeJavaResourceTask).configureEach { task ->
def projectJavaResOriginal = task.projectJavaRes
// This file collection will hold all of the kotlin_module files
def projectJavaResWithKotlinModuleFiles = project.files(task.projectJavaRes).filter { it.name.endsWith(".kotlin_module") }
// This file collection will hold everything but the kotlin_module files
def projectJavaResWithoutKotlinModuleFiles = project.files(task.projectJavaRes).filter { !it.name.endsWith(".kotlin_module") }
@ghale
ghale / build.gradle
Created April 17, 2020 15:57
Compiler argument provider example
plugins {
id 'java'
}
tasks.withType(JavaCompile).configureEach {
def resourcesDir = project.file("${projectDir}/resources")
options.compilerArgumentProviders.add(MyAnnotationArgumentProvider.from(resourcesDir))
// This captures the argument provider as a custom value in the build scan for troubleshooting purposes only
doFirst {
@ghale
ghale / build.gradle
Last active April 27, 2020 19:37
Tags based on task name pattern matching
// Key is the tag to add if includes or excludes match. Value is a map containing include and/or exclude patterns.
// Pattern is a regex that will be matched against the task name.
def tags = [
'dexGuard': [include: 'dexGuardDebug'],
'incremental': [include: 'assemble.*', exclude: 'clean']
]
gradle.taskGraph.whenReady { taskGraph ->
tags.each { tag, criteria ->
if (matchesInclude(taskGraph, criteria) && doesNotMatchExclude(taskGraph, criteria)) {
// Creaate an implementation of the task using all of the types below
task reverseFiles(type: ReverseFiles) {
outputDir = file("$buildDir/reversed")
source("sources")
}
// The parameters for a single unit of work
interface ReverseParameters extends WorkParameters {
RegularFileProperty getFileToReverse()
DirectoryProperty getDestinationDir()
@ghale
ghale / build.gradle
Last active January 18, 2021 02:39
Inserting weaving task into Android pipeline
android {
libraryVariants.all {
def kotlinCompileProvider = tasks.named("compile${name.capitalize()}Kotlin")
kotlinCompileProvider.configure {
// We do this because the way Kotlin wires itself into the Android build pipeline,
// it maps directly to the destinationDir property. This means if we change it, the
// Android configuration just follows it around. Instead, we let it generate the
// class files where it wants to, then we move them to where we want them, set this
// new location as an output directory (for caching/incrementality) and delete the
@ghale
ghale / build.gradle
Last active June 18, 2020 17:14
Variant aware dependency management
// Declare a new attribute type to use for selection
def usageAttribute = Attribute.of("com.adyen.usage", String)
// In both the producer and consumer, register the new attribute and set
// up a configuration associated to the attribute
subprojects {
dependencies.attributesSchema {
attribute(usageAttribute)
}
configurations {
web {
@ghale
ghale / build.gradle
Last active June 24, 2020 18:53
Create arbitrary pipelines within a build
// Set up two different pipelines
createPipeline("foo")
createPipeline("bar")
// Insert a cleanup task that is automatically slotted into the foo "prepare" phase
tasks.register("cleanupFoo", Cleanup) { pipelineName = "foo" }
// Insert a start task that is automatically slotted into the foo "start" phase
tasks.register("startFooTestContainer", StartContainer) { pipelineName = "foo" }
@ghale
ghale / kotlinOptions.gradle
Created June 25, 2020 21:42
Capture Kotlin processor options as custom value
import java.lang.reflect.Field
tasks.withType(getKaptWithoutKotlincTaskClass()).configureEach { task ->
doFirst {
buildScan.value "${task.path}.processorOptions", getCompilerPluginOptions(task).collect { "${it.key} = ${it.value}" }.join("\n")
}
}
static def getCompilerPluginOptions(Task task) {
def processorOptionsField = getAccessibleField(task.class, "processorOptions")
@ghale
ghale / build.gradle
Last active June 25, 2020 22:32
Workaround for old version of Apollo graphql
tasks.withType(com.apollographql.apollo.gradle.ApolloCodegenTask).configureEach { task ->
FileTree originalSource
// Create a synthetic input with the original source value and RELATIVE path sensitivity
project.gradle.taskGraph.beforeTask {
if (it == task) {
originalSource = task.getSource()
// Set the source to something static for the purpose of input calculation
task.source = fileTree("foo")
task.inputs.files(originalSource)
.withPathSensitivity(PathSensitivity.RELATIVE)
@ghale
ghale / build.gradle
Last active July 11, 2020 12:53
Workaround for MergeResources cache miss
tasks.withType(com.android.build.gradle.tasks.MergeResources) { task ->
Map<String, FileCollection> originalResources = [:]
// Create a synthetic input with the original value and RELATIVE path sensitivity
project.gradle.taskGraph.beforeTask {
if (it == task) {
originalResources.putAll(task.resourcesComputer.resources)
task.resourcesComputer.resources.clear()
task.inputs.files(originalResources.values())
.withPathSensitivity(PathSensitivity.RELATIVE)
.withPropertyName("rawLocalResources.workaround")