Spring 引导:如何制作小jar包并从外部文件夹共享依赖项

Spring Boot : How to make small jar package and share dependencies from external folder

我必须在客户端位置的同一个 Linux 服务器上部署多个 spring 启动应用程序。每个应用程序包含相同的依赖项,每个应用程序文件大小为 50mb,因此我们如何创建一个公共库文件夹,以便所有 spring 引导应用程序从一个位置共享相同的依赖项。这可能会使我的应用程序 jar 或 war 文件变小。请帮我解决这个问题。

任何建议或博客链接都会有所帮助。

提前致谢。

您可以使用 spring-boot-thin-launcher 构建可执行文件和瘦 Spring 引导 jar。它适用于 mavengradle

马文:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-thin-layout</artifactId>
            <version>1.0.28.BUILD-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>

Gradle:

plugins {
    //other plugins goes here ...
    id 'org.springframework.boot.experimental.thin-launcher' version '1.0.28.BUILD-SNAPSHOT'
}

你可以运行 Spring 通过命令启动瘦 JAR

java -jar my-app-1.0.jar --thin.root=path/to/dependency/jars

你可以看看:

您好,您问了这个问题

so how we can create a common library folder so that all spring boot application shares the same dependencies from one location

在 pom.xml 中添加 maven-dependency-plugin 目标为 copy-dependencies 并且在此之下你可以设置公共库文件夹的输出目录

例子

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <phase>install</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                   <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
       </execution>
    </executions>
</plugin>

现在您可以使用下面的方法创建两个 jar,一个包含所有依赖项(名为 appname-exec.jar),另一个仅包含 class 个文件(appname.jar)

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <classifier>exec</classifier>
    </configuration>
</plugin>

现在您只能将 jar 与 class 文件 (appname.jar) 一起使用,因为 大小会小很多,因为它没有包含任何依赖项

最后一步需要配置才能使用 jar (appname.jar),即添加 class 路径条目,即公共位置 jar 并添加您的标签内的主要 class 名称。

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <archive>
        <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>fully.qualified.MainClass</mainClass>
       </manifest>
    </archive>
   </configuration>
</plugin>

现在您可以根据您的项目结构将这三个放在一起 pom.xml 在构建部分下。