V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nanmu42
V2EX  ›  Java

请问, Java 如何打出自带依赖的 jar 包?

  •  1
     
  •   nanmu42 ·
    nanmu42 · 2021-09-22 18:53:20 +08:00 · 2833 次点击
    这是一个创建于 917 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位好,我想请教,Java 如何打出自带依赖的 jar 包?就是那种拷贝 jar 包到干净的 OpenJDK 环境下就直接可运行的那种?

    我现在是依赖 IDEA 的 build artifacts 功能,但是找不到对应的命令行命令,我想在 CI 里完成这件事情。

    我使用的是 Java 8 和 Maven.

    感谢。

    10 条回复    2021-09-24 14:31:34 +08:00
    billlee
        1
    billlee  
       2021-09-22 18:58:49 +08:00   ❤️ 2
    maven shade plugin
    sutra
        2
    sutra  
       2021-09-22 19:03:37 +08:00   ❤️ 1
    AllenHua
        3
    AllenHua  
       2021-09-22 19:11:39 +08:00 via iPhone   ❤️ 1
    https://hellodk.cn/post/727

    ```
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
    <archive>
    <manifest>
    <!-- 运行 jar 包时运行的主类,要求写全类名 需要包含 package name -->
    <mainClass>MainService</mainClass>
    <!-- 是否指定项目 classpath 下的依赖 -->
    <addClasspath>true</addClasspath>
    </manifest>
    </archive>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>
    ```
    ztcaoll222
        4
    ztcaoll222  
       2021-09-22 19:36:06 +08:00   ❤️ 1
    关键词:fat jar
    Jooooooooo
        5
    Jooooooooo  
       2021-09-22 23:20:20 +08:00   ❤️ 1
    感觉你想要的是 fat jar

    包含所有的东西
    cslive
        6
    cslive  
       2021-09-23 09:13:43 +08:00   ❤️ 1
    springboot 用 spring-boot-maven-plugin 这个插件,不带容器的 java 非 java ee 项目需要自己写配置文件,指定 main 方法才行
    monkeydream
        7
    monkeydream  
       2021-09-23 17:31:26 +08:00   ❤️ 1
    maven-assembly-plugin 插件了解一下
    nanmu42
        8
    nanmu42  
    OP
       2021-09-24 13:13:36 +08:00
    感谢各位的分享,综合意见,最后我使用了 Maven Shade Plugin,目前状态不错。

    https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
    Muyiafan
        9
    Muyiafan  
       2021-09-24 13:35:41 +08:00   ❤️ 1
    如果用的是 gradle 构建工具的话。
    ```
    gradle assemble
    ```
    wucao219101
        10
    wucao219101  
       2021-09-24 14:31:34 +08:00   ❤️ 1
    maven-assembly-plugin / maven-shade-plugin 二选一,如果是 Spring 项目推荐用 maven-shade-plugin 。

    https://xxgblog.com/2015/08/07/maven-create-executable-jar/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5421 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:40 · PVG 16:40 · LAX 01:40 · JFK 04:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.