|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.gradle.agent; |
| 10 | + |
| 11 | +import org.gradle.api.Plugin; |
| 12 | +import org.gradle.api.Project; |
| 13 | +import org.gradle.api.artifacts.Configuration; |
| 14 | +import org.gradle.api.tasks.Copy; |
| 15 | +import org.gradle.api.tasks.TaskProvider; |
| 16 | +import org.gradle.api.tasks.testing.Test; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +/** |
| 22 | + * Gradle plugin to automatically configure the OpenSearch Java agent |
| 23 | + * for test tasks in OpenSearch plugin projects. |
| 24 | + */ |
| 25 | +public class JavaAgent implements Plugin<Project> { |
| 26 | + |
| 27 | + /** |
| 28 | + * Plugin implementation that sets up java agent configuration and applies it to test tasks. |
| 29 | + */ |
| 30 | + @Override |
| 31 | + public void apply(Project project) { |
| 32 | + Configuration agentConfiguration = project.getConfigurations().findByName("agent"); |
| 33 | + if (agentConfiguration == null) { |
| 34 | + agentConfiguration = project.getConfigurations().create("agent"); |
| 35 | + } |
| 36 | + |
| 37 | + project.afterEvaluate(p -> { |
| 38 | + String opensearchVersion = getOpensearchVersion(p); |
| 39 | + p.getDependencies().add("agent", "org.opensearch:opensearch-agent-bootstrap:" + opensearchVersion); |
| 40 | + p.getDependencies().add("agent", "org.opensearch:opensearch-agent:" + opensearchVersion); |
| 41 | + }); |
| 42 | + |
| 43 | + Configuration finalAgentConfiguration = agentConfiguration; |
| 44 | + TaskProvider<Copy> prepareJavaAgent = project.getTasks().register("prepareJavaAgent", Copy.class, task -> { |
| 45 | + task.from(finalAgentConfiguration); |
| 46 | + task.into(new File(project.getBuildDir(), "agent")); |
| 47 | + }); |
| 48 | + |
| 49 | + project.getTasks().withType(Test.class).configureEach(testTask -> { |
| 50 | + testTask.dependsOn(prepareJavaAgent); |
| 51 | + |
| 52 | + final String opensearchVersion = getOpensearchVersion(project); |
| 53 | + |
| 54 | + testTask.doFirst(task -> { |
| 55 | + File agentJar = new File(project.getBuildDir(), "agent/opensearch-agent-" + opensearchVersion + ".jar"); |
| 56 | + |
| 57 | + testTask.jvmArgs("-javaagent:" + agentJar.getAbsolutePath()); |
| 58 | + }); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the OpenSearch version from project properties, with a fallback default. |
| 64 | + * |
| 65 | + * @param project The Gradle project |
| 66 | + * @return The OpenSearch version to use |
| 67 | + */ |
| 68 | + private String getOpensearchVersion(Project project) { |
| 69 | + return Objects.requireNonNull(project.property("opensearch_version")).toString(); |
| 70 | + } |
| 71 | +} |
0 commit comments