对于需要 javaagent(比如 OpenTelemetry)的系统 the docs通常以“从此 URL 下载代理 JAR 并将其添加到命令行”开始。在使用 Maven Central 和稳定的版本控制等很好地处理库依赖关系的世界中,相比之下,“下载 JAR”方法似乎原始且不安全。
在使用 Gradle 构建的项目中获取 javaagent 库的最佳做法是什么? “下载这个 jar”真的是当前最先进的技术吗?
我现在对 OpenTelemetry 特别感兴趣。如果有仅适用于 OpenTelemetry 的答案(例如 Gradle 插件),我会洗耳恭听。
最佳答案
根据我所做的研究,有一个 gradle plugin专门用于将 Maven 依赖项附加为 javaagent。
引用自插件github仓库:
This Gradle plugin tightly integrates with the Gradle application plugin to make instrumenting your application build by Gradle easy! Simply register the
javaagent-application
plugin and then specify the javaagent you would like to attach in the dependencies block
可以在同一存储库中找到使用 otel java 代理的示例 here
plugins {
id("com.ryandens.javaagent.example.java-application-conventions")
id("com.ryandens.javaagent-otel-modification")
id("com.ryandens.javaagent-application")
}
dependencies {
otel("io.opentelemetry.javaagent:opentelemetry-javaagent:1.13.1")
otelExtension("io.opentelemetry.contrib:opentelemetry-samplers:1.13.0-alpha")
otelInstrumentation(project(":custom-instrumentation", "shadow"))
}
application {
// Define the main class for the application.
mainClass.set("com.ryandens.javaagent.example.App")
applicationDefaultJvmArgs = listOf("-Dotel.javaagent.debug=true", "-Dotel.metrics.exporter=none")
}
/*
see https://github.com/johnrengelman/shadow/issues/713
Currently, tasks that consume the output of the extendedAgent shadowJar task need to be made aware of
the implicit dependency (https://docs.gradle.org/7.4.2/userguide/validation_problems.html#implicit_dependency)
due to an issue with the shadowJar plugin
*/
setOf(tasks.distTar, tasks.distZip).forEach {
it.configure {
dependsOn(tasks.extendedAgent)
}
}
关于java - 在使用 Gradle 构建的 Java 项目中管理 javaagent 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73176095/