Skip to content

Commit 2c4d71d

Browse files
committed
chore: Configure lazily MuzzlePlugin
1 parent 36490a6 commit 2c4d71d

File tree

1 file changed

+81
-79
lines changed

1 file changed

+81
-79
lines changed

buildSrc/src/main/groovy/MuzzlePlugin.groovy

Lines changed: 81 additions & 79 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
@@ -17,19 +15,15 @@ import org.eclipse.aether.spi.connector.transport.TransporterFactory
1715
import org.eclipse.aether.transport.http.HttpTransporterFactory
1816
import org.eclipse.aether.util.version.GenericVersionScheme
1917
import org.eclipse.aether.version.Version
20-
import org.gradle.api.Action
21-
import org.gradle.api.DefaultTask
22-
import org.gradle.api.GradleException
23-
import org.gradle.api.Plugin
24-
import org.gradle.api.Project
25-
import org.gradle.api.Task
18+
import org.gradle.api.*
2619
import org.gradle.api.artifacts.Configuration
2720
import org.gradle.api.file.ConfigurableFileCollection
2821
import org.gradle.api.file.FileCollection
2922
import org.gradle.api.invocation.BuildInvocationDetails
3023
import org.gradle.api.model.ObjectFactory
3124
import org.gradle.api.provider.Property
3225
import org.gradle.api.tasks.SourceSet
26+
import org.gradle.api.tasks.TaskProvider
3327
import org.gradle.jvm.toolchain.JavaLanguageVersion
3428
import org.gradle.jvm.toolchain.JavaToolchainService
3529
import org.gradle.workers.WorkAction
@@ -93,36 +87,31 @@ class MuzzlePlugin implements Plugin<Project> {
9387
def toolingProject = childProjects.get('agent-tooling')
9488
project.extensions.create("muzzle", MuzzleExtension, project.objects)
9589

96-
def muzzleBootstrap = project.configurations.create('muzzleBootstrap', {
90+
def muzzleBootstrap = project.configurations.register('muzzleBootstrap', {
9791
canBeConsumed: false
9892
canBeResolved: true
9993
})
100-
def muzzleTooling = project.configurations.create('muzzleTooling', {
94+
def muzzleTooling = project.configurations.register('muzzleTooling', {
10195
canBeConsumed: false
10296
canBeResolved: true
10397
})
104-
project.dependencies.add('muzzleBootstrap', bootstrapProject)
105-
project.dependencies.add('muzzleTooling', toolingProject)
98+
99+
project.dependencies.add(muzzleBootstrap.name, bootstrapProject)
100+
project.dependencies.add(muzzleTooling.name, toolingProject)
106101

107102
project.evaluationDependsOn ':dd-java-agent:agent-bootstrap'
108103
project.evaluationDependsOn ':dd-java-agent:agent-tooling'
109104

110105
// compileMuzzle compiles all projects required to run muzzle validation.
111106
// 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-
}
107+
TaskProvider<Task> compileMuzzle = project.tasks.register('compileMuzzle') {
108+
it.dependsOn(project.tasks.withType(InstrumentTask))
109+
it.dependsOn bootstrapProject.tasks.named("compileJava")
110+
it.dependsOn bootstrapProject.tasks.named("compileMain_java11Java")
111+
it.dependsOn toolingProject.tasks.named("compileJava")
120112
}
121-
compileMuzzle.dependsOn bootstrapProject.tasks.compileJava
122-
compileMuzzle.dependsOn bootstrapProject.tasks.compileMain_java11Java
123-
compileMuzzle.dependsOn toolingProject.tasks.compileJava
124113

125-
project.task(['type': MuzzleTask], 'muzzle') {
114+
def muzzleTask = project.tasks.register('muzzle', MuzzleTask) {
126115
description = "Run instrumentation muzzle on compile time dependencies"
127116
doLast {
128117
if (!project.muzzle.directives.any { it.assertPass }) {
@@ -133,23 +122,23 @@ class MuzzlePlugin implements Plugin<Project> {
133122
dependsOn compileMuzzle
134123
}
135124

136-
project.task(['type': MuzzleTask], 'printReferences') {
125+
project.tasks.register('printReferences', MuzzleTask) {
137126
description = "Print references created by instrumentation muzzle"
138127
doLast {
139128
printMuzzle(project)
140129
}
141130
dependsOn compileMuzzle
142131
}
143-
project.task(['type': MuzzleTask], 'generateMuzzleReport') {
132+
133+
project.tasks.register('generateMuzzleReport', MuzzleTask) {
144134
description = "Print instrumentation version report"
145135
doLast {
146136
dumpVersionRanges(project)
147137
}
148138
dependsOn compileMuzzle
149139
}
150140

151-
152-
project.task(['type': MuzzleTask], 'mergeMuzzleReports') {
141+
project.tasks.register('mergeMuzzleReports', MuzzleTask) {
153142
description = "Merge generated version reports in one unique csv"
154143
doLast {
155144
mergeReports(project)
@@ -174,35 +163,37 @@ class MuzzlePlugin implements Plugin<Project> {
174163
final RepositorySystemSession session = newRepositorySystemSession(system)
175164
project.afterEvaluate {
176165
// 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
166+
TaskProvider<Task> runAfter = muzzleTask
180167
for (MuzzleDirective muzzleDirective : project.muzzle.directives) {
181168
project.getLogger().info("configured $muzzleDirective")
182169

183170
if (muzzleDirective.coreJdk) {
184-
runLast = runAfter = addMuzzleTask(muzzleDirective, null, project, runAfter, muzzleBootstrap, muzzleTooling)
171+
runAfter = addMuzzleTask(muzzleDirective, null, project, runAfter, muzzleBootstrap, muzzleTooling)
185172
} else {
186173
def range = resolveVersionRange(muzzleDirective, system, session)
187-
runLast = muzzleDirectiveToArtifacts(muzzleDirective, range).inject(runLast) { last, Artifact singleVersion ->
174+
for (Artifact singleVersion : muzzleDirectiveToArtifacts(muzzleDirective, range)) {
188175
runAfter = addMuzzleTask(muzzleDirective, singleVersion, project, runAfter, muzzleBootstrap, muzzleTooling)
189176
}
190177
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 ->
178+
for (MuzzleDirective inverseDirective : inverseOf(muzzleDirective, system, session)) {
179+
def inverseRange = resolveVersionRange(inverseDirective, system, session)
180+
for (Artifact singleVersion : (muzzleDirectiveToArtifacts(inverseDirective, inverseRange))) {
193181
runAfter = addMuzzleTask(inverseDirective, singleVersion, project, runAfter, muzzleBootstrap, muzzleTooling)
194182
}
195183
}
196184
}
197185
}
198186
}
199-
def timingTask = project.task("muzzle-end") {
187+
def timingTask = project.tasks.register("muzzle-end") {
200188
doLast {
201189
long endTime = System.currentTimeMillis()
202190
generateResultsXML(project, endTime - startTime)
203191
}
204192
}
205-
runLast.finalizedBy(timingTask)
193+
// last muzzle task to run
194+
runAfter.configure {
195+
finalizedBy(timingTask)
196+
}
206197
}
207198
}
208199

@@ -236,7 +227,7 @@ class MuzzlePlugin implements Plugin<Project> {
236227
Map<String, TestedArtifact> map = new TreeMap<>()
237228
def versionScheme = new GenericVersionScheme()
238229
dir.eachFileMatch(~/.*\.csv/) { file ->
239-
file.eachLine { line, nb ->
230+
file.eachLine { line, nb ->
240231
if (nb == 1) {
241232
// skip header
242233
return
@@ -318,9 +309,9 @@ class MuzzlePlugin implements Plugin<Project> {
318309
FileCollection cp = project.files()
319310
project.getLogger().info("Creating muzzle classpath for $muzzleTaskName")
320311
if ('muzzle' == muzzleTaskName) {
321-
cp += project.configurations.compileClasspath
312+
cp += project.configurations.named("compileClasspath").get()
322313
} else {
323-
cp += project.configurations.getByName(muzzleTaskName)
314+
cp += project.configurations.named(muzzleTaskName).get()
324315
}
325316
if (project.getLogger().isInfoEnabled()) {
326317
cp.forEach { project.getLogger().info("-- $it") }
@@ -431,52 +422,61 @@ class MuzzlePlugin implements Plugin<Project> {
431422
*
432423
* @return The created muzzle task.
433424
*/
434-
private static Task addMuzzleTask(MuzzleDirective muzzleDirective, Artifact versionArtifact, Project instrumentationProject, Task runAfter, Configuration muzzleBootstrap, Configuration muzzleTooling) {
435-
def taskName
425+
private static TaskProvider<Task> addMuzzleTask(
426+
MuzzleDirective muzzleDirective,
427+
Artifact versionArtifact,
428+
Project instrumentationProject,
429+
TaskProvider<Task> runAfter,
430+
NamedDomainObjectProvider<Configuration> muzzleBootstrap,
431+
NamedDomainObjectProvider<Configuration> muzzleTooling
432+
) {
433+
def muzzleTaskName
436434
if (muzzleDirective.coreJdk) {
437-
taskName = "muzzle-Assert$muzzleDirective"
435+
muzzleTaskName = "muzzle-Assert$muzzleDirective"
438436
} else {
439-
taskName = "muzzle-Assert${muzzleDirective.assertPass ? "Pass" : "Fail"}-$versionArtifact.groupId-$versionArtifact.artifactId-$versionArtifact.version${muzzleDirective.name ? "-${muzzleDirective.getNameSlug()}" : ""}"
440-
}
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)
437+
muzzleTaskName = "muzzle-Assert${muzzleDirective.assertPass ? "Pass" : "Fail"}-$versionArtifact.groupId-$versionArtifact.artifactId-$versionArtifact.version${muzzleDirective.name ? "-${muzzleDirective.getNameSlug()}" : ""}"
462438
}
463-
for (String additionalDependency : muzzleDirective.additionalDependencies) {
464-
config.dependencies.add(instrumentationProject.dependencies.create(additionalDependency) { dep ->
439+
instrumentationProject.configurations.register(muzzleTaskName) { Configuration taskConfig ->
440+
if (!muzzleDirective.coreJdk) {
441+
def depId = "$versionArtifact.groupId:$versionArtifact.artifactId:$versionArtifact.version"
442+
if (versionArtifact.classifier) {
443+
depId += ":" + versionArtifact.classifier
444+
}
445+
def dep = instrumentationProject.dependencies.create(depId) {
446+
transitive = true
447+
}
448+
// The following optional transitive dependencies are brought in by some legacy module such as log4j 1.x but are no
449+
// longer bundled with the JVM and have to be excluded for the muzzle tests to be able to run.
450+
dep.exclude group: 'com.sun.jdmk', module: 'jmxtools'
451+
dep.exclude group: 'com.sun.jmx', module: 'jmxri'
452+
// Also exclude specifically excluded dependencies
465453
for (String excluded : muzzleDirective.excludedDependencies) {
466454
String[] parts = excluded.split(':')
467455
dep.exclude group: parts[0], module: parts[1]
468456
}
469-
dep.transitive = true
470-
})
457+
458+
taskConfig.dependencies.add(dep)
459+
}
460+
for (String additionalDependency : muzzleDirective.additionalDependencies) {
461+
taskConfig.dependencies.add(instrumentationProject.dependencies.create(additionalDependency) { dep ->
462+
for (String excluded : muzzleDirective.excludedDependencies) {
463+
String[] parts = excluded.split(':')
464+
dep.exclude group: parts[0], module: parts[1]
465+
}
466+
dep.transitive = true
467+
})
468+
}
471469
}
472470

473-
def muzzleTask = instrumentationProject.task(['type': MuzzleTask], taskName) {
471+
def muzzleTask = instrumentationProject.tasks.register(muzzleTaskName, MuzzleTask) {
474472
doLast {
475473
assertMuzzle(muzzleBootstrap, muzzleTooling, instrumentationProject, muzzleDirective)
476474
}
477475
}
478476

479-
runAfter.finalizedBy(muzzleTask)
477+
runAfter.configure {
478+
finalizedBy(muzzleTask)
479+
}
480480
muzzleTask
481481
}
482482

@@ -710,10 +710,12 @@ abstract class MuzzleTask extends DefaultTask {
710710
@javax.inject.Inject
711711
abstract WorkerExecutor getWorkerExecutor()
712712

713-
void assertMuzzle(Configuration muzzleBootstrap,
714-
Configuration muzzleTooling,
715-
Project instrumentationProject,
716-
MuzzleDirective muzzleDirective = null) {
713+
public void assertMuzzle(
714+
NamedDomainObjectProvider<Configuration> muzzleBootstrap,
715+
NamedDomainObjectProvider<Configuration> muzzleTooling,
716+
Project instrumentationProject,
717+
MuzzleDirective muzzleDirective = null
718+
) {
717719
def workQueue
718720
String javaVersion = muzzleDirective?.javaVersion
719721
if (javaVersion) {
@@ -730,8 +732,8 @@ abstract class MuzzleTask extends DefaultTask {
730732
}
731733
workQueue.submit(MuzzleAction.class, parameters -> {
732734
parameters.buildStartedTime.set(invocationDetails.buildStartedTime)
733-
parameters.bootstrapClassPath.setFrom(muzzleBootstrap)
734-
parameters.toolingClassPath.setFrom(muzzleTooling)
735+
parameters.bootstrapClassPath.setFrom(muzzleBootstrap.get())
736+
parameters.toolingClassPath.setFrom(muzzleTooling.get())
735737
parameters.instrumentationClassPath.setFrom(MuzzlePlugin.createAgentClassPath(instrumentationProject))
736738
parameters.testApplicationClassPath.setFrom(MuzzlePlugin.createMuzzleClassPath(instrumentationProject, name))
737739
if (muzzleDirective) {
@@ -743,7 +745,7 @@ abstract class MuzzleTask extends DefaultTask {
743745
})
744746
}
745747

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

0 commit comments

Comments
 (0)