SpringBoot完全可执行jar,里面没有依赖项 [英] SpringBoot fully executable jar without dependencies inside

查看:1288
本文介绍了SpringBoot完全可执行jar,里面没有依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:在将此问题标记为重复之前,请确保您知道可执行JAR 完全可执行的SpringBoot JAR

NOTE: Please, before marking this question as a duplicate make sure you know the difference between executable JAR and fully executable SpringBoot JAR.

Spring Boot官方文档介绍了如何构建完全可执行JAR。然后生成的JAR文件可以从 /etc/init.d / 链接,并作为普通的unix服务启动/停止/重新启动/统计,而无需其他脚本或工具,如JSVC。

The official Spring Boot documentation describes how to build fully executable JAR. Then generated JAR file can be linked from /etc/init.d/ and started/stopped/restarted/statused as a normal unix service without additional scripts or tools like JSVC.

但生成的JAR包含所有库,并且可以足够大(在我的情况下为70Mb +)。

But the generated JAR contains all libraries and can be big enough in size (in my case 70Mb+).

我想生成没有库的完全可执行JAR,但是能够在Linux上运行它作为SystemV服务并链接外部库(JARs)不知何故。

I want to generate such fully executable JAR without libraries, but then to be able to run it as SystemV service on Linux and link external libraries (JARs) somehow.

UPDATE

我想减少工件尺寸为了加快部署 - >测试 - >修复周期。有时我通过移动网络工作,大文件大小会大大降低我的工作速度。

I want to reduce the artifact size in order to speed up deploy->test->fix cycle. Sometimes I'm working via mobile network and big file size can decrease my job speed dramatically.

如果没有简单的配置属性或配置文件或命令行选项我会使用一种黑客。

In case there is no a simple configuration property or a profile or a command line option I would use a kind of hack.

一开始,我可以生成一个包含所有依赖项的构建。
然后我可以解压缩它并将所有库移动到一个特殊文件夹。

At the beginning, I can generate a build containing all dependencies. Then I can unzip it and move all libraries to a special folder.

然后我需要以某种方式将其作为完全可执行文件再次打包并运行时指向带库的文件夹。

Then I need to pack it again as fully executable somehow and run with pointing to the folder with libraries.

我不认为可以使用 jar 实用程序来完成,因为 file 实用程序将完全可执行的jar识别为 data

I don't think this can be done with jar utility because file utility recognizes fully executable jar as data

$ file fully-executable.jar
file fully-executable: data

与通常的jar不同

$ file usual.jar
usual.jar: Java Jar file data (zip)


推荐答案

您可能需要考虑使用 Spring Boot Thin Launcher 。它使用您的应用程序代码创建一个jar文件,但不包含任何依赖项。它添加了一个特殊的瘦启动器,它知道如何在执行jar时从远程Maven存储库或本地缓存中解析应用程序的依赖性。从您想要做的描述来判断,您将使用本地缓存选项。

You may want to consider using Spring Boot Thin Launcher. It creates a jar file with your application code but none of its dependencies. It adds a special thin launcher that knows how to resolve your application's dependences from a remote Maven repository or from a local cache when the jar is executed. Judging by the description of what you want to do, you'd utilise the local cache option.

配置Spring Boot的Maven插件以生成一个完全可执行的jar使用瘦启动器看起来像这样:

The configuration of Spring Boot's Maven plugin to produce a fully executable jar that uses the thin launcher looks like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot.experimental</groupId>
                    <artifactId>spring-boot-thin-layout</artifactId>
                    <version>1.0.3.RELEASE</version>
                </dependency>
            </dependencies>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

这篇关于SpringBoot完全可执行jar,里面没有依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆