【问题标题】:How to bundle a JAR file with its dependencies using maven如何使用 maven 将 JAR 文件与其依赖项捆绑在一起
【发布时间】: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
  • 文档 (1, 2) 建议使用 shade 插件或程序集插件,但两者都没有在最终的 jar 文件中包含与我的依赖项相关的任何文件......所以使用程序集插件的stackoverflow帖子链接并不能解决我的问题。
  • 没有看到你的 pom.xml 和你的程序集插件配置,很难说出了什么问题。
  • 我刚刚编辑了我的帖子以包含它

标签: java maven jar


【解决方案1】:

听起来您需要将“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

【讨论】:

  • Daaaaaaaaamn,问题的根源是我将 bytebuddy 作为本地 jar 文件而不是依赖项中的 maven repo 提供。使用 maven repo 它可以工作,我的 jar 重 3.9MB。我不明白为什么它只有在它是一个 maven repo 时才被合并到最终的 jar 中,而不是当它是一个本地 jar 文件时,但是伙计,非常感谢!
猜你喜欢
  • 2018-04-18
  • 2011-10-12
  • 1970-01-01
  • 2011-04-03
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
相关资源
最近更新 更多