More lazy Gradle configuration (#2973)

This commit is contained in:
Anuraag Agrawal 2021-05-13 16:24:40 +09:00 committed by GitHub
parent 1b345e399c
commit a3be8e3613
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 65 deletions

View File

@ -12,8 +12,6 @@ plugins {
id 'org.unbroken-dome.test-sets' apply false
id 'com.github.ben-manes.versions'
id 'com.dorongold.task-tree'
id "com.github.johnrengelman.shadow" apply false
id "com.diffplug.spotless"

View File

@ -29,9 +29,8 @@ import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.TaskProvider
/**
* muzzle task plugin which runs muzzle validation against a range of dependencies.
*/
@ -48,20 +47,20 @@ class MuzzlePlugin implements Plugin<Project> {
// compileMuzzle compiles all projects required to run muzzle validation.
// Not adding group and description to keep this task from showing in `gradle tasks`.
def compileMuzzle = project.task('compileMuzzle') {
def compileMuzzle = project.tasks.register('compileMuzzle') {
dependsOn(':javaagent-bootstrap:classes')
dependsOn(':javaagent-tooling:classes')
dependsOn(':javaagent-extension-api:classes')
dependsOn(project.tasks.classes)
}
def muzzle = project.task('muzzle') {
def muzzle = project.tasks.register('muzzle') {
group = 'Muzzle'
description = "Run instrumentation muzzle on compile time dependencies"
dependsOn(compileMuzzle)
}
project.task('printMuzzleReferences') {
project.tasks.register('printMuzzleReferences') {
group = 'Muzzle'
description = "Print references created by instrumentation muzzle"
dependsOn(compileMuzzle)
@ -89,7 +88,7 @@ class MuzzlePlugin implements Plugin<Project> {
project.afterEvaluate {
// use runAfter to set up task finalizers in version order
Task runAfter = muzzle
TaskProvider runAfter = muzzle
for (MuzzleDirective muzzleDirective : project.muzzle.directives) {
project.getLogger().info("configured $muzzleDirective")
@ -315,7 +314,7 @@ class MuzzlePlugin implements Plugin<Project> {
*
* @return The created muzzle task.
*/
private static Task addMuzzleTask(MuzzleDirective muzzleDirective, Artifact versionArtifact, Project instrumentationProject, Task runAfter) {
private static TaskProvider addMuzzleTask(MuzzleDirective muzzleDirective, Artifact versionArtifact, Project instrumentationProject, TaskProvider runAfter) {
def taskName
if (muzzleDirective.coreJdk) {
taskName = "muzzle-Assert$muzzleDirective"
@ -345,7 +344,7 @@ class MuzzlePlugin implements Plugin<Project> {
})
}
def muzzleTask = instrumentationProject.task(taskName) {
def muzzleTask = instrumentationProject.tasks.register(taskName) {
dependsOn(instrumentationProject.configurations.named("runtimeClasspath"))
doLast {
ClassLoader instrumentationCL = createInstrumentationClassloader(instrumentationProject)
@ -375,7 +374,9 @@ class MuzzlePlugin implements Plugin<Project> {
}
}
}
runAfter.finalizedBy(muzzleTask)
runAfter.configure {
finalizedBy(muzzleTask)
}
return muzzleTask
}

View File

@ -8,12 +8,15 @@ package io.opentelemetry.instrumentation.gradle.bytebuddy;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import net.bytebuddy.build.gradle.ByteBuddySimpleTask;
import net.bytebuddy.build.gradle.Transformation;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.AbstractCompile;
/**
@ -56,23 +59,34 @@ public class ByteBuddyPluginConfigurator {
public void configure() {
String taskName = getTaskName();
Task byteBuddyTask =
List<TaskProvider<?>> languageTasks =
LANGUAGES.stream()
.map(
language -> {
if (project.fileTree("src/" + sourceSet.getName() + "/" + language).isEmpty()) {
return null;
}
String compileTaskName = sourceSet.getCompileTaskName(language);
if (!project.getTasks().getNames().contains(compileTaskName)) {
return null;
}
final TaskProvider<?> compileTask = project.getTasks().named(compileTaskName);
// We also process resources for SPI classes.
return createLanguageTask(
compileTask, taskName + language, sourceSet.getProcessResourcesTaskName());
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
TaskProvider<?> byteBuddyTask =
project
.getTasks()
// TODO(anuraaga): Use lazy configuration to create muzzle task.
.create(
.register(
taskName,
task -> {
for (String language : LANGUAGES) {
AbstractCompile compile = getCompileTask(language);
if (compile != null) {
Task languageTask = createLanguageTask(compile, taskName + language);
// We also process resources for SPI classes.
languageTask.dependsOn(sourceSet.getProcessResourcesTaskName());
task.dependsOn(languageTask);
}
}
task.dependsOn(languageTasks);
});
project
@ -81,48 +95,39 @@ public class ByteBuddyPluginConfigurator {
.configure(task -> task.dependsOn(byteBuddyTask));
}
private Task createLanguageTask(AbstractCompile compileTask, String name) {
private TaskProvider<?> createLanguageTask(
TaskProvider<?> compileTaskProvider, String name, String processResourcesTaskName) {
return project
.getTasks()
.create(
.register(
name,
ByteBuddySimpleTask.class,
task -> {
task.setGroup("Byte Buddy");
task.getOutputs().cacheIf(unused -> true);
File classesDirectory = compileTask.getDestinationDir();
File rawClassesDirectory =
new File(classesDirectory.getParent(), classesDirectory.getName() + "raw")
.getAbsoluteFile();
Task maybeCompileTask = compileTaskProvider.get();
if (maybeCompileTask instanceof AbstractCompile) {
AbstractCompile compileTask = (AbstractCompile) maybeCompileTask;
File classesDirectory = compileTask.getDestinationDir();
File rawClassesDirectory =
new File(classesDirectory.getParent(), classesDirectory.getName() + "raw")
.getAbsoluteFile();
task.dependsOn(compileTask);
compileTask.setDestinationDir(rawClassesDirectory);
task.dependsOn(compileTask);
compileTask.setDestinationDir(rawClassesDirectory);
task.setSource(rawClassesDirectory);
task.setTarget(classesDirectory);
task.setClassPath(compileTask.getClasspath());
task.setSource(rawClassesDirectory);
task.setTarget(classesDirectory);
task.setClassPath(compileTask.getClasspath());
task.dependsOn(compileTask);
task.dependsOn(compileTask, processResourcesTaskName);
}
task.getTransformations().add(createTransformation(inputClasspath, pluginClassName));
});
}
private AbstractCompile getCompileTask(String language) {
Task task = project.getTasks().findByName(sourceSet.getCompileTaskName(language));
if (task instanceof AbstractCompile) {
AbstractCompile compile = (AbstractCompile) task;
if (!compile.getSource().isEmpty()) {
return compile;
}
}
return null;
}
private String getTaskName() {
if (SourceSet.MAIN_SOURCE_SET_NAME.equals(sourceSet.getName())) {
return "byteBuddy";

View File

@ -68,14 +68,11 @@ configurations {
if (testLatestDeps) {
afterEvaluate {
// Try/catch seems to be the only way to access an optional task with lazy configuration API.
try {
if (tasks.names.contains('latestDepTest')) {
def latestDepTest = tasks.named('latestDepTest')
tasks.named('test').configure {
dependsOn(latestDepTest)
}
} catch (UnknownDomainObjectException e) {
// Ignore
}
}
}

View File

@ -4,7 +4,7 @@ apply plugin: 'signing'
publishing {
publications {
maven(MavenPublication) {
if (project.tasks.findByName("shadowJar") != null && !findProperty('noShadowPublish')) {
if (project.tasks.names.contains('shadowJar') && !findProperty('noShadowPublish')) {
project.shadow.component(it)
//These two are here just to satisfy Maven Central
artifact sourcesJar
@ -92,11 +92,11 @@ rootProject.tasks.named('release').configure {
finalizedBy tasks.publishToSonatype
}
tasks.withType(Sign).configureEach {
onlyIf { System.getenv("CI") != null }
}
signing {
useInMemoryPgpKeys(System.getenv("GPG_PRIVATE_KEY"), System.getenv("GPG_PASSWORD"))
sign publishing.publications.maven
// Stub out entire signing block off of CI since Gradle provides no way of lazy configuration of
// signing tasks.
if (System.getenv("CI") != null) {
signing {
useInMemoryPgpKeys(System.getenv("GPG_PRIVATE_KEY"), System.getenv("GPG_PASSWORD"))
sign publishing.publications.maven
}
}

View File

@ -1,6 +1,6 @@
plugins {
id "java-library"
id "com.google.protobuf" version "0.8.13"
id "com.google.protobuf" version "0.8.16"
}
apply from: "$rootDir/gradle/java.gradle"

View File

@ -21,7 +21,9 @@ subprojects {
afterEvaluate { Project subProj ->
if (subProj.getPlugins().hasPlugin('java')) {
// Make it so all instrumentation subproject tests can be run with a single command.
instr_project.tasks.test.dependsOn(subProj.tasks.test)
instr_project.tasks.named('test').configure {
dependsOn(subProj.tasks.test)
}
if (subProj.name == 'javaagent') {
instr_project.dependencies {

View File

@ -1,7 +1,6 @@
pluginManagement {
plugins {
id "com.diffplug.spotless" version "5.8.2"
id 'com.dorongold.task-tree' version '1.5'
id 'com.github.ben-manes.versions' version '0.27.0'
id "com.github.johnrengelman.shadow" version "6.1.0"
id "com.github.spotbugs" version "4.6.0"