【发布时间】:2021-12-31 09:52:08
【问题描述】:
我正在使用ByteBuddy 开发Java 代理,我需要将ByteBuddy 库.jar 文件包含在代理.jar 文件中。到目前为止,为了让代理顺利运行,我需要 ByteBuddy 库 .jar 文件在编译时和运行时都存在于类路径中。如何捆绑.jar 文件以使代理独立?
我尝试使用 shade 插件(如 here 所示)以及在网络上找到的其他一些技术,但它们似乎都没有真正包含 .jar 文件中的依赖项,仅作为参考。
对于每种技术,我查看了生成的 .jar 文件(每次重约 5kB),只找到了与我编写的类对应的 .class 文件,没有与 ByteBuddy 相关的类文件。需要明确的是,ByteBuddy 库 .jar 文件重约 3MB,因此我希望我的独立代理 .jar 文件重约 3MB,因为我的代码很轻。
以下是我的pom.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.captainhook.agent</groupId>
<artifactId>agent</artifactId>
<version>1.0-SNAPSHOT</version>
<name>agent</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>bytebuddy</artifactId>
<version>1.12.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/byte-buddy-1.12.3.jar</systemPath>
</dependency>
<dependency>
<groupId>net.bytebuddy.agent</groupId>
<artifactId>bytebuddy-agent</artifactId>
<version>1.12.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/byte-buddy-agent-1.12.3.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我运行mvn package 后得到的输出:
[...]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ agent ---
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (default) @ agent ---
[INFO] Building jar: /home/bluesheet/svn/stages/captainhook/2021/ijp-frida-jdi-bytebuddy/1/dbg/shared/agent/maven-test/agent/target/agent-1.0-SNAPSHOT-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.339 s
[INFO] Finished at: 2021-12-31T12:26:59+01:00
[INFO] ------------------------------------------------------------------------
编辑: 所以,之前所有的技术都不起作用的原因是我指定依赖项的方式。这不包括在内:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>bytebuddy</artifactId>
<version>1.12.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/byte-buddy-1.12.3.jar</systemPath>
</dependency>
虽然这样做:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.6</version>
</dependency>
我是 maven 新手,所以我盲目地复制粘贴了一段代码以包含依赖项,但我没有发现错误... 非常感谢!
【问题讨论】:
-
文档如何建议您这样做?
-
这能回答你的问题吗? Including dependencies in a jar with Maven
-
没有看到你的 pom.xml 和你的程序集插件配置,很难说出了什么问题。
-
我刚刚编辑了我的帖子以包含它