我正在使用 maven 编写自己的扩展,我想嵌入该扩展并创建一个 jar。我在https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/examples/extension/README.md#embed-extensions-in-the-opentelemetry-agent中看到了一个示例,但是这个在 gradle 中。有没有人有maven的例子?
我尝试使用 maven-shade-plugin,但我不确定如何输出嵌入了扩展 jar 的 opentelemetry-javaagent jar
我通过将我的 Maven 项目分成两个模块来完成:
定制库
在此输入您的定制代码。如果您使用的任何依赖项尚未成为 opentelemetry-javaagent 的一部分,则必须将它们嵌入到最终的 JAR 中。这里显示的是您的
maven-shade-plugin
中的 pom.xml
:
<groupId>org.example</groupId>
<artifactId>customization-lib</artifactId>
<dependencies>
<dependency>
<groupId>io.opentelemetry.contrib</groupId>
<artifactId>opentelemetry-samplers</artifactId>
<version>1.32.0-alpha</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
javaagent
此模块将生成基于
opentelemetry-javaagent
的 JAR,但在 customization-lib
目录中包含您的 /extensions
JAR。为此,您可以将 maven-assembly-plugin
与自定义描述符文件一起使用(此处:assemble-javaagent.xml
):
首先,配置您的
pom.xml
:
<groupId>org.example</groupId>
<artifactId>javaagent</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>customization-lib</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.javaagent</groupId>
<artifactId>opentelemetry-javaagent</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>embed-customization-in-javaagent</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>assemble-javaagent.xml</descriptors>
<mergeManifestMode>merge</mergeManifestMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这里有几点需要注意:
customization-lib
和 opentelemetry-javaagent
作为依赖项maven-assembly-plugin
并定义:
<appendAssemblyId>false</appendAssemblyId>
使用输出作为您的 Maven 项目工件<mergeManifestMode>merge</mergeManifestMode>
不覆盖 MANIFEST.MF
的
opentelemetry-javaagent
然后在
assemble-javaagent.xml
旁边创建一个文件 pom.xml
:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 https://maven.apache.org/xsd/assembly-2.2.0.xsd">
<id>javaagent</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<!-- 1. Step: Unpack the opentelemetry-javaagent -->
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>io.opentelemetry.javaagent:opentelemetry-javaagent</include>
</includes>
</dependencySet>
<!-- 2. Step: Copy our customization jar into the /extensions directory -->
<dependencySet>
<outputDirectory>/extensions</outputDirectory>
<unpack>false</unpack>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>org.example:customization-lib</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
这是基于其
build.gradle文件的
extendedAgent
任务。