我正在使用ByteBuddy開發 Java 代理,我需要將 ByteBuddy 庫.jar檔案包含在代理.jar檔案中。到目前為止,為了讓代理順利運行,我需要 ByteBuddy 庫.jar檔案在編譯時和運行時都存在于類路徑中。如何捆綁.jar檔案以使代理自包含?
我試著用窗簾插件(如證明這里),以及在網路上發現了一些其他的技術,但沒有人似乎真的包括在依賴.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 的新手,所以我盲目地復制粘貼了一段代碼以包含依賴項,但我沒有發現錯誤......非常感謝!
uj5u.com熱心網友回復:
聽起來您需要將“maven-assembly-plugin”與“jar-with-dependencies”描述符一起使用。
例如,這里是一個完整的示例 pom 檔案,它依賴于 ByteBuddy:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.example</groupId>
<artifactId>packaging-example</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>test.example.TestByteBuddy</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.6</version>
</dependency>
</dependencies>
</project>
為了完整起見 - 這里也是主類:
package test.example;
import net.bytebuddy.ByteBuddy;
public class TestByteBuddy
{
public static void main(String... args) throws Exception
{
System.out.println("Hello Byte Buddy Class - " ByteBuddy.class);
}
}
構建這將在目標目錄中生成一個附加檔案 - Packaging-example-1.0-SNAPSHOT-jar-with-dependencies.jar
然后你應該能夠簡單地運行它:
java -jar packaging-example-1.0-SNAPSHOT-jar-with-dependencies.jar
給你輸出:
Hello Byte Buddy Class - class net.bytebuddy.ByteBuddy
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/399173.html
上一篇:從地圖流中重用lambda
下一篇:合并排序陣列?
