Skip to content

Commit 748e6cc

Browse files
committed
Minor tweaks to agent Gradle logic
While doing some other Gradle changes I ran into some really odd failures that ultimately led me down a rabbit hole that resulted in some changes that better match best practices: - Change distribution/build.gradle to use an agent configuration as opposed to taking a direct task dependency. This is how it gets artifacts from other subprojects and is a Gradle best practice. - Defer the evalution of the agent jar file until task execution which also makes the logic less brittle to other Gradle changes. - Make `prepareAgent` depend on `jar` as opposed to the other way around. This removes the need for other projects that need the jar file to depend on anything other than the `jar` task. Signed-off-by: Andrew Ross <[email protected]>
1 parent bbd226e commit 748e6cc

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,11 @@ gradle.projectsEvaluated {
423423

424424
// Add Java Agent for security sandboxing
425425
if (!(project.path in [':build-tools', ":libs:agent-sm:bootstrap", ":libs:agent-sm:agent"])) {
426-
dependsOn(project(':libs:agent-sm:agent').prepareAgent)
427-
jvmArgs += ["-javaagent:" + project(':libs:agent-sm:agent').jar.archiveFile.get()]
426+
dependsOn(':libs:agent-sm:agent:jar')
427+
// Defer resolution of jar file until task execution by using doFirst
428+
doFirst {
429+
jvmArgs += ["-javaagent:" + project(':libs:agent-sm:agent').jar.archiveFile.get()]
430+
}
428431
}
429432
if (BuildParams.isInFipsJvm()) {
430433
def fipsSecurityFile = project.rootProject.file('distribution/src/config/fips_java.security')

distribution/build.gradle

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
312312
* Properties to expand when copying packaging files *
313313
*****************************************************************************/
314314
configurations {
315-
['libs', 'libsPluginCli', 'libsKeystoreCli', 'libsFipsInstallerCli', 'bcFips'].each {
315+
['libs', 'libsPluginCli', 'libsKeystoreCli', 'libsFipsInstallerCli', 'bcFips', 'agent'].each {
316316
create(it) {
317317
canBeConsumed = false
318318
canBeResolved = true
@@ -335,6 +335,8 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
335335
libsKeystoreCli project(path: ':distribution:tools:keystore-cli')
336336
libsFipsInstallerCli project(path: ':distribution:tools:fips-demo-installer-cli')
337337

338+
agent project(':libs:agent-sm:agent')
339+
338340
bcFips libs.bundles.bouncycastle
339341
}
340342

@@ -365,10 +367,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
365367

366368
agentFiles = {
367369
copySpec {
368-
from(project(':libs:agent-sm:agent').prepareAgent) {
369-
include '**/*.jar'
370-
exclude '**/*-javadoc.jar'
371-
exclude '**/*-sources.jar'
370+
from(configurations.agent) {
372371
// strip the version since jvm.options is using agent without version
373372
rename("opensearch-agent-${project.version}.jar", "opensearch-agent.jar")
374373
}

libs/agent-sm/agent/build.gradle

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ dependencies {
1919
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"
2020
}
2121

22+
tasks.register('prepareAgent', Copy) {
23+
from(configurations.runtimeClasspath)
24+
into layout.buildDirectory.dir("distributions")
25+
}
26+
2227
var bootClasspath = configurations.bootstrap.incoming.artifactView { }.files
2328
.getFiles()
2429
.collect { it.name }
2530

2631
jar {
32+
dependsOn prepareAgent
2733
manifest {
2834
attributes(
2935
"Can-Redefine-Classes": "true",
@@ -47,12 +53,6 @@ tasks.named('forbiddenApisMain').configure {
4753
onlyIf { false }
4854
}
4955

50-
task prepareAgent(type: Copy) {
51-
from(configurations.runtimeClasspath)
52-
into "$buildDir/distributions"
53-
dependsOn jar
54-
}
55-
5656
thirdPartyAudit {
5757
ignoreMissingClasses(
5858
'com.sun.jna.FunctionMapper',
@@ -73,15 +73,7 @@ tasks.named('validateNebulaPom') {
7373
}
7474

7575
tasks.test {
76-
dependsOn prepareAgent
76+
dependsOn jar
7777
jvmArgs += ["-javaagent:" + project.jar.archiveFile.get()]
7878
forkEvery = 1
7979
}
80-
81-
tasks.check {
82-
dependsOn test
83-
}
84-
85-
tasks.named('assemble') {
86-
dependsOn prepareAgent
87-
}

plugins/repository-hdfs/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ project(':test:fixtures:krb5kdc-fixture').tasks.preProcessFixture {
130130
// Create HDFS File System Testing Fixtures for HA/Secure combinations
131131
for (String fixtureName : ['hdfsFixture', 'haHdfsFixture', 'secureHdfsFixture', 'secureHaHdfsFixture']) {
132132
def tsk = tasks.register(fixtureName, org.opensearch.gradle.test.AntFixture) {
133-
dependsOn configurations.hdfsFixture, project(':test:fixtures:krb5kdc-fixture').tasks.postProcessFixture, project(':libs:agent-sm:agent').prepareAgent
133+
dependsOn configurations.hdfsFixture, project(':test:fixtures:krb5kdc-fixture').tasks.postProcessFixture, ':libs:agent-sm:agent:jar'
134134
executable = "${BuildParams.runtimeJavaHome}/bin/java"
135135
env 'CLASSPATH', "${-> configurations.hdfsFixture.asPath}"
136136
maxWaitInSeconds = 60

test/framework/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ test {
102102
systemProperty 'tests.gradle_wire_compat_versions', BuildParams.bwcVersions.wireCompatible.join(',')
103103
systemProperty 'tests.gradle_unreleased_versions', BuildParams.bwcVersions.unreleased.join(',')
104104

105-
dependsOn(project(':libs:agent-sm:agent').prepareAgent)
106-
jvmArgs += ["-javaagent:" + project(':libs:agent-sm:agent').jar.archiveFile.get()]
105+
dependsOn(':libs:agent-sm:agent:jar')
106+
// Defer resolution of agent jar file until task execution by using doFirst
107+
doFirst {
108+
jvmArgs += ["-javaagent:" + project(':libs:agent-sm:agent').jar.archiveFile.get()]
109+
}
107110
}
108111

109112
tasks.register("integTest", Test) {

0 commit comments

Comments
 (0)