Skip to content

Commit 86fb978

Browse files
authored
Configure lazily MuzzlePlugin (#9315)
* chore: Configure lazily MuzzlePlugin * chore: Groovy style
1 parent e8faa94 commit 86fb978

File tree

1 file changed

+81
-74
lines changed

1 file changed

+81
-74
lines changed

buildSrc/src/main/groovy/MuzzlePlugin.groovy

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import static MuzzleAction.createClassLoader
2-
31
import org.apache.maven.repository.internal.MavenRepositorySystemUtils
42
import org.eclipse.aether.DefaultRepositorySystemSession
53
import org.eclipse.aether.RepositorySystem
@@ -20,6 +18,7 @@ import org.eclipse.aether.version.Version
2018
import org.gradle.api.Action
2119
import org.gradle.api.DefaultTask
2220
import org.gradle.api.GradleException
21+
import org.gradle.api.NamedDomainObjectProvider
2322
import org.gradle.api.Plugin
2423
import org.gradle.api.Project
2524
import org.gradle.api.Task
@@ -30,6 +29,7 @@ import org.gradle.api.invocation.BuildInvocationDetails
3029
import org.gradle.api.model.ObjectFactory
3130
import org.gradle.api.provider.Property
3231
import org.gradle.api.tasks.SourceSet
32+
import org.gradle.api.tasks.TaskProvider
3333
import org.gradle.jvm.toolchain.JavaLanguageVersion
3434
import org.gradle.jvm.toolchain.JavaToolchainService
3535
import org.gradle.workers.WorkAction
@@ -93,36 +93,31 @@ class MuzzlePlugin implements Plugin<Project> {
9393
def toolingProject = childProjects.get('agent-tooling')
9494
project.extensions.create("muzzle", MuzzleExtension, project.objects)
9595

96-
def muzzleBootstrap = project.configurations.create('muzzleBootstrap', {
96+
def muzzleBootstrap = project.configurations.register('muzzleBootstrap', {
9797
canBeConsumed: false
9898
canBeResolved: true
9999
})
100-
def muzzleTooling = project.configurations.create('muzzleTooling', {
100+
def muzzleTooling = project.configurations.register('muzzleTooling', {
101101
canBeConsumed: false
102102
canBeResolved: true
103103
})
104-
project.dependencies.add('muzzleBootstrap', bootstrapProject)
105-
project.dependencies.add('muzzleTooling', toolingProject)
104+
105+
project.dependencies.add(muzzleBootstrap.name, bootstrapProject)
106+
project.dependencies.add(muzzleTooling.name, toolingProject)
106107

107108
project.evaluationDependsOn ':dd-java-agent:agent-bootstrap'
108109
project.evaluationDependsOn ':dd-java-agent:agent-tooling'
109110

110111
// compileMuzzle compiles all projects required to run muzzle validation.
111112
// Not adding group and description to keep this task from showing in `gradle tasks`.
112-
def compileMuzzle = project.task('compileMuzzle')
113-
compileMuzzle.dependsOn(toolingProject.tasks.named("compileJava"))
114-
project.afterEvaluate {
115-
project.tasks.matching {
116-
it.name =~ /\Ainstrument(Main)?(_.+)?(Java|Scala|Kotlin)/
117-
}.all {
118-
compileMuzzle.dependsOn(it)
119-
}
113+
TaskProvider<Task> compileMuzzle = project.tasks.register('compileMuzzle') {
114+
it.dependsOn(project.tasks.withType(InstrumentTask))
115+
it.dependsOn bootstrapProject.tasks.named("compileJava")
116+
it.dependsOn bootstrapProject.tasks.named("compileMain_java11Java")
117+
it.dependsOn toolingProject.tasks.named("compileJava")
120118
}
121-
compileMuzzle.dependsOn bootstrapProject.tasks.compileJava
122-
compileMuzzle.dependsOn bootstrapProject.tasks.compileMain_java11Java
123-
compileMuzzle.dependsOn toolingProject.tasks.compileJava
124119

125-
project.task(['type': MuzzleTask], 'muzzle') {
120+
def muzzleTask = project.tasks.register('muzzle', MuzzleTask) {
126121
description = "Run instrumentation muzzle on compile time dependencies"
127122
doLast {
128123
if (!project.muzzle.directives.any { it.assertPass }) {
@@ -133,23 +128,23 @@ class MuzzlePlugin implements Plugin<Project> {
133128
dependsOn compileMuzzle
134129
}
135130

136-
project.task(['type': MuzzleTask], 'printReferences') {
131+
project.tasks.register('printReferences', MuzzleTask) {
137132
description = "Print references created by instrumentation muzzle"
138133
doLast {
139134
printMuzzle(project)
140135
}
141136
dependsOn compileMuzzle
142137
}
143-
project.task(['type': MuzzleTask], 'generateMuzzleReport') {
138+
139+
project.tasks.register('generateMuzzleReport', MuzzleTask) {
144140
description = "Print instrumentation version report"
145141
doLast {
146142
dumpVersionRanges(project)
147143
}
148144
dependsOn compileMuzzle
149145
}
150146

151-
152-
project.task(['type': MuzzleTask], 'mergeMuzzleReports') {
147+
project.tasks.register('mergeMuzzleReports', MuzzleTask) {
153148
description = "Merge generated version reports in one unique csv"
154149
doLast {
155150
mergeReports(project)
@@ -174,35 +169,37 @@ class MuzzlePlugin implements Plugin<Project> {
174169
final RepositorySystemSession session = newRepositorySystemSession(system)
175170
project.afterEvaluate {
176171
// use runAfter to set up task finalizers in version order
177-
Task runAfter = project.tasks.muzzle
178-
// runLast is the last task to finish, so we can time the execution
179-
Task runLast = runAfter
172+
TaskProvider<Task> runAfter = muzzleTask
180173
for (MuzzleDirective muzzleDirective : project.muzzle.directives) {
181174
project.getLogger().info("configured $muzzleDirective")
182175

183176
if (muzzleDirective.coreJdk) {
184-
runLast = runAfter = addMuzzleTask(muzzleDirective, null, project, runAfter, muzzleBootstrap, muzzleTooling)
177+
runAfter = addMuzzleTask(muzzleDirective, null, project, runAfter, muzzleBootstrap, muzzleTooling)
185178
} else {
186179
def range = resolveVersionRange(muzzleDirective, system, session)
187-
runLast = muzzleDirectiveToArtifacts(muzzleDirective, range).inject(runLast) { last, Artifact singleVersion ->
180+
for (Artifact singleVersion : muzzleDirectiveToArtifacts(muzzleDirective, range)) {
188181
runAfter = addMuzzleTask(muzzleDirective, singleVersion, project, runAfter, muzzleBootstrap, muzzleTooling)
189182
}
190183
if (muzzleDirective.assertInverse) {
191-
runLast = inverseOf(muzzleDirective, system, session).inject(runLast) { last1, MuzzleDirective inverseDirective ->
192-
muzzleDirectiveToArtifacts(inverseDirective, resolveVersionRange(inverseDirective, system, session)).inject(last1) { last2, Artifact singleVersion ->
184+
for (MuzzleDirective inverseDirective : inverseOf(muzzleDirective, system, session)) {
185+
def inverseRange = resolveVersionRange(inverseDirective, system, session)
186+
for (Artifact singleVersion : (muzzleDirectiveToArtifacts(inverseDirective, inverseRange))) {
193187
runAfter = addMuzzleTask(inverseDirective, singleVersion, project, runAfter, muzzleBootstrap, muzzleTooling)
194188
}
195189
}
196190
}
197191
}
198192
}
199-
def timingTask = project.task("muzzle-end") {
193+
def timingTask = project.tasks.register("muzzle-end") {
200194
doLast {
201195
long endTime = System.currentTimeMillis()
202196
generateResultsXML(project, endTime - startTime)
203197
}
204198
}
205-
runLast.finalizedBy(timingTask)
199+
// last muzzle task to run
200+
runAfter.configure {
201+
finalizedBy(timingTask)
202+
}
206203
}
207204
}
208205

@@ -236,7 +233,7 @@ class MuzzlePlugin implements Plugin<Project> {
236233
Map<String, TestedArtifact> map = new TreeMap<>()
237234
def versionScheme = new GenericVersionScheme()
238235
dir.eachFileMatch(~/.*\.csv/) { file ->
239-
file.eachLine { line, nb ->
236+
file.eachLine { line, nb ->
240237
if (nb == 1) {
241238
// skip header
242239
return
@@ -255,7 +252,6 @@ class MuzzlePlugin implements Plugin<Project> {
255252
dumpVersionsToCsv(project, map)
256253
}
257254

258-
259255
private static void dumpVersionRanges(Project project) {
260256
final RepositorySystem system = newRepositorySystem()
261257
final RepositorySystemSession session = newRepositorySystemSession(system)
@@ -318,9 +314,9 @@ class MuzzlePlugin implements Plugin<Project> {
318314
FileCollection cp = project.files()
319315
project.getLogger().info("Creating muzzle classpath for $muzzleTaskName")
320316
if ('muzzle' == muzzleTaskName) {
321-
cp += project.configurations.compileClasspath
317+
cp += project.configurations.named("compileClasspath").get()
322318
} else {
323-
cp += project.configurations.getByName(muzzleTaskName)
319+
cp += project.configurations.named(muzzleTaskName).get()
324320
}
325321
if (project.getLogger().isInfoEnabled()) {
326322
cp.forEach { project.getLogger().info("-- $it") }
@@ -431,52 +427,61 @@ class MuzzlePlugin implements Plugin<Project> {
431427
*
432428
* @return The created muzzle task.
433429
*/
434-
private static Task addMuzzleTask(MuzzleDirective muzzleDirective, Artifact versionArtifact, Project instrumentationProject, Task runAfter, Configuration muzzleBootstrap, Configuration muzzleTooling) {
435-
def taskName
430+
private static TaskProvider<Task> addMuzzleTask(
431+
MuzzleDirective muzzleDirective,
432+
Artifact versionArtifact,
433+
Project instrumentationProject,
434+
TaskProvider<Task> runAfter,
435+
NamedDomainObjectProvider<Configuration> muzzleBootstrap,
436+
NamedDomainObjectProvider<Configuration> muzzleTooling
437+
) {
438+
def muzzleTaskName
436439
if (muzzleDirective.coreJdk) {
437-
taskName = "muzzle-Assert$muzzleDirective"
440+
muzzleTaskName = "muzzle-Assert$muzzleDirective"
438441
} else {
439-
taskName = "muzzle-Assert${muzzleDirective.assertPass ? "Pass" : "Fail"}-$versionArtifact.groupId-$versionArtifact.artifactId-$versionArtifact.version${muzzleDirective.name ? "-${muzzleDirective.getNameSlug()}" : ""}"
442+
muzzleTaskName = "muzzle-Assert${muzzleDirective.assertPass ? "Pass" : "Fail"}-$versionArtifact.groupId-$versionArtifact.artifactId-$versionArtifact.version${muzzleDirective.name ? "-${muzzleDirective.getNameSlug()}" : ""}"
440443
}
441-
def config = instrumentationProject.configurations.create(taskName)
442-
443-
if (!muzzleDirective.coreJdk) {
444-
def depId = "$versionArtifact.groupId:$versionArtifact.artifactId:$versionArtifact.version"
445-
if (versionArtifact.classifier) {
446-
depId += ":" + versionArtifact.classifier
447-
}
448-
def dep = instrumentationProject.dependencies.create(depId) {
449-
transitive = true
450-
}
451-
// The following optional transitive dependencies are brought in by some legacy module such as log4j 1.x but are no
452-
// longer bundled with the JVM and have to be excluded for the muzzle tests to be able to run.
453-
dep.exclude group: 'com.sun.jdmk', module: 'jmxtools'
454-
dep.exclude group: 'com.sun.jmx', module: 'jmxri'
455-
// Also exclude specifically excluded dependencies
456-
for (String excluded : muzzleDirective.excludedDependencies) {
457-
String[] parts = excluded.split(':')
458-
dep.exclude group: parts[0], module: parts[1]
459-
}
460-
461-
config.dependencies.add(dep)
462-
}
463-
for (String additionalDependency : muzzleDirective.additionalDependencies) {
464-
config.dependencies.add(instrumentationProject.dependencies.create(additionalDependency) { dep ->
444+
instrumentationProject.configurations.register(muzzleTaskName) { Configuration taskConfig ->
445+
if (!muzzleDirective.coreJdk) {
446+
def depId = "$versionArtifact.groupId:$versionArtifact.artifactId:$versionArtifact.version"
447+
if (versionArtifact.classifier) {
448+
depId += ":" + versionArtifact.classifier
449+
}
450+
def dep = instrumentationProject.dependencies.create(depId) {
451+
transitive = true
452+
}
453+
// The following optional transitive dependencies are brought in by some legacy module such as log4j 1.x but are no
454+
// longer bundled with the JVM and have to be excluded for the muzzle tests to be able to run.
455+
dep.exclude group: 'com.sun.jdmk', module: 'jmxtools'
456+
dep.exclude group: 'com.sun.jmx', module: 'jmxri'
457+
// Also exclude specifically excluded dependencies
465458
for (String excluded : muzzleDirective.excludedDependencies) {
466459
String[] parts = excluded.split(':')
467460
dep.exclude group: parts[0], module: parts[1]
468461
}
469-
dep.transitive = true
470-
})
462+
463+
taskConfig.dependencies.add(dep)
464+
}
465+
for (String additionalDependency : muzzleDirective.additionalDependencies) {
466+
taskConfig.dependencies.add(instrumentationProject.dependencies.create(additionalDependency) { dep ->
467+
for (String excluded : muzzleDirective.excludedDependencies) {
468+
String[] parts = excluded.split(':')
469+
dep.exclude group: parts[0], module: parts[1]
470+
}
471+
dep.transitive = true
472+
})
473+
}
471474
}
472475

473-
def muzzleTask = instrumentationProject.task(['type': MuzzleTask], taskName) {
476+
def muzzleTask = instrumentationProject.tasks.register(muzzleTaskName, MuzzleTask) {
474477
doLast {
475478
assertMuzzle(muzzleBootstrap, muzzleTooling, instrumentationProject, muzzleDirective)
476479
}
477480
}
478481

479-
runAfter.finalizedBy(muzzleTask)
482+
runAfter.configure {
483+
finalizedBy(muzzleTask)
484+
}
480485
muzzleTask
481486
}
482487

@@ -710,10 +715,12 @@ abstract class MuzzleTask extends DefaultTask {
710715
@javax.inject.Inject
711716
abstract WorkerExecutor getWorkerExecutor()
712717

713-
void assertMuzzle(Configuration muzzleBootstrap,
714-
Configuration muzzleTooling,
715-
Project instrumentationProject,
716-
MuzzleDirective muzzleDirective = null) {
718+
public void assertMuzzle(
719+
NamedDomainObjectProvider<Configuration> muzzleBootstrap,
720+
NamedDomainObjectProvider<Configuration> muzzleTooling,
721+
Project instrumentationProject,
722+
MuzzleDirective muzzleDirective = null
723+
) {
717724
def workQueue
718725
String javaVersion = muzzleDirective?.javaVersion
719726
if (javaVersion) {
@@ -730,8 +737,8 @@ abstract class MuzzleTask extends DefaultTask {
730737
}
731738
workQueue.submit(MuzzleAction.class, parameters -> {
732739
parameters.buildStartedTime.set(invocationDetails.buildStartedTime)
733-
parameters.bootstrapClassPath.setFrom(muzzleBootstrap)
734-
parameters.toolingClassPath.setFrom(muzzleTooling)
740+
parameters.bootstrapClassPath.setFrom(muzzleBootstrap.get())
741+
parameters.toolingClassPath.setFrom(muzzleTooling.get())
735742
parameters.instrumentationClassPath.setFrom(MuzzlePlugin.createAgentClassPath(instrumentationProject))
736743
parameters.testApplicationClassPath.setFrom(MuzzlePlugin.createMuzzleClassPath(instrumentationProject, name))
737744
if (muzzleDirective) {
@@ -743,7 +750,7 @@ abstract class MuzzleTask extends DefaultTask {
743750
})
744751
}
745752

746-
void printMuzzle(Project instrumentationProject) {
753+
public void printMuzzle(Project instrumentationProject) {
747754
FileCollection cp = instrumentationProject.sourceSets.main.runtimeClasspath
748755
ClassLoader cl = new URLClassLoader(cp*.toURI()*.toURL() as URL[], null as ClassLoader)
749756
Method printMethod = cl.loadClass('datadog.trace.agent.tooling.muzzle.MuzzleVersionScanPlugin')

0 commit comments

Comments
 (0)