Springboot完全可执行的JAR内部没有依赖关系



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

官方的Spring Boot文档描述了如何构建完全可执行的JAR。然后,生成的JAR文件可以从/etc/init.d/链接,并在没有其他脚本或JSVC(例如JSVC(的情况下开始/停止/重新启动/统计作为普通Unix服务。

但是生成的jar包含所有库,并且大小可能足够大(在我的情况下为70MB (。

我想生成这样的完全无库的可执行jar,但是能够在linux上以systemv和链接外部库(JARS(的方式运行它。

update

我想减小工件大小,以加快部署 -> test->修复周期。有时我通过移动网络工作,大文件大小可以大大降低我的工作速度。

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

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

然后,我需要以某种方式将其重新包装为完全可执行文件,并用库指向文件夹。

我认为jar实用程序无法完成,因为file实用程序将完全可执行的JAR识别为data

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

与通常的jar

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

您可能需要考虑使用Spring Boot薄启动器。它使用您的应用程序代码创建一个JAR文件,但没有其依赖性。它添加了一个特殊的薄启动器,该启动器知道如何从远程Maven存储库或执行JAR执行时从本地高速缓存中解析您的应用程序的依赖。从您想做的事情的描述来看,您将使用本地缓存选项。

Spring Boot的Maven插件的配置,以产生一个完全可执行的JAR,该JAR使用薄启动器看起来像这样:

<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>

我可以使用此设置创建弹簧引导罐而无需依赖性罐子。

复制依赖性罐子为dist/lib

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeScope>runtime</includeScope>
                <outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

将'lib'指定为classpath前缀,因此将创建MainFest.mf:

class-path:lib/httpcore-nio-4.4.4.14.jar lib/guava-24.1.1-jre.jar.jar ...

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.xxx.MyMainClass</mainClass>
            </manifest>
        </archive>
        <outputDirectory>${project.build.directory}/dist</outputDirectory>
    </configuration>
</plugin>

令Spring Boot插件包含依赖性的依赖,将导致Spring Boot Jar排除所有依赖关系。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <layout>ZIP</layout>
        <includes>
            <include>
                <groupId>not-exist-in-my-project</groupId>
                <artifactId>not-exist-in-my-project</artifactId>
            </include>
        </includes>
        <outputDirectory>${project.build.directory}/dist</outputDirectory>
    </configuration>
</plugin>

最新更新