简体   繁体   English

在使用 Gradle 构建的 Java 项目中管理 javaagent 依赖项

[英]Manage javaagent dependencies in a Java project built with Gradle

For systems that require a javaagent (say, OpenTelemetry) the docs often start with "download the agent JAR from this URL and add it to your command line".对于需要 javaagent(例如 OpenTelemetry) 的系统,文档通常以“从此 URL 下载代理 JAR 并将其添加到命令行”开头。 In a world where library dependencies are handled quite well using Maven Central, with stable versioning etc., the "download a JAR" approach seems primitive and insecure by comparison.在使用 Maven Central 可以很好地处理库依赖关系的世界中,具有稳定的版本控制等,相比之下,“下载 JAR”方法似乎原始且不安全。

What is the best practice for acquiring javaagent libraries in a project built with Gradle?在使用 Gradle 构建的项目中获取 javaagent 库的最佳实践是什么? Is "download this jar" really the current state of the art? “下载这个 jar”真的是当前 state 的艺术吗?

I'm specifically interested in OpenTelemetry right now.我现在对 OpenTelemetry 特别感兴趣。 If there's an answer (eg. a Gradle plugin) that only works for OpenTelemetry, I'm all ears.如果有一个仅适用于 OpenTelemetry 的答案(例如 Gradle 插件),我会全力以赴。

From what I have done research, there is one gradle plugin available specifically for attaching a maven dependency as javaagent.根据我所做的研究,有一个gradle 插件可专门用于将 maven 依赖项附加为 javaagent。

Quoting from plugin github repository:引用插件 github 存储库:

This Gradle plugin tightly integrates with the Gradle application plugin to make instrumenting your application build by Gradle easy!这个 Gradle 插件与 Gradle 应用程序插件紧密集成,使您可以轻松地检测由 Gradle 构建的应用程序! Simply register the javaagent-application plugin and then specify the javaagent you would like to attach in the dependencies block只需注册javaagent-application插件,然后在依赖项块中指定要附加的 javaagent

Example usage with otel java agent can found in the same repository here otel java 代理的示例用法可以在此处的同一存储库中找到

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)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM