Home > Java > javaTutorial > body text

How to deploy Jar files in SpringBoot

WBOY
Release: 2023-05-24 13:15:23
forward
803 people have browsed it

Main configuration:

<build>
    <finalName>${project.artifactId}</finalName>
    <!--
    特别注意:
    项目仅仅是为了演示配置方便,直接在parent的build部分做了插件配置和运行定义。
    但是实际项目中需要把这些定义只放到spring boot模块项目(可优化使用pluginManagement形式),避免干扰其他util、common等模块项目
    -->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Copy after login

Configuration output:

cd package-optimize-level0
mvn clean install

ls -lh package-optimize-app1/target/package-optimize-app1.jar
-rw-r--r--  1 lixia  wheel    16M Feb 24 21:06 package-optimize-app1/target/package-optimize-app1.jar

java -jar package-optimize-app1/target/package-optimize-app1.jar
Copy after login

Key words:

  • (The current demo application only relies on a few components of spring-boot-starter-web, and all build outputs are only about ten MB). In actual situation, the output jar of a single build is generally between tens of MB and one based on the amount of project dependent components. Two hundred MB or more.

  • If you need to deploy more than a dozen microservices, you will need to transfer several gigabytes of files, which will consume a lot of time. Even a single update to an individual microservice requires the transfer of one to two hundred MB.

Level 1: Common dependency jar separation construction method

Reference project directory: package-optimize-level1

Guide to solve the problem:

  • Reduce the file size of a single microservice jar so that the file can be transferred in seconds during the deployment process. Main configuration:

For key configuration instructions, please see the following notes:

<build>
    <finalName>${project.artifactId}</finalName>
    <!--
    特别注意:
    项目仅仅是为了演示配置方便,直接在parent的build部分做了插件配置和运行定义。
    但是实际项目中需要把这些定义只放到spring boot模块项目(可优化使用pluginManagement形式),避免干扰其他util、common等模块项目
    -->
    <plugins>
        <!-- 拷贝项目所有依赖jar文件到构建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>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <silent>true</silent>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Spring Boot模块jar构建 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includes>
                    <!-- 不存在的include引用,相当于排除所有maven依赖jar,没有任何三方jar文件打入输出jar -->
                    <include>
                        <groupId>null</groupId>
                        <artifactId>null</artifactId>
                    </include>
                </includes>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy after login

Configuration output:

cd package-optimize-level1
mvn clean install

ls -lh package-optimize-app1/target/package-optimize-app1.jar
-rw-r--r--  1 lixia  wheel   149K Feb 24 20:56 package-optimize-app1/target/package-optimize-app1.jar

java -jar -Djava.ext.dirs=lib package-optimize-app1/target/package-optimize-app1.jar
Copy after login

Implementation effect:

  • #The output jar of a single build is generally only one or two hundred KB based on the amount of project dependent components, and it can basically be transferred in seconds.

  • This is the most common optimization solution seen on the Internet, and it is worth further exploring: If there are a dozen microservices, each service has a jar and a lib directory file, the first deployment is similar. One or two GB files need to be transferred.

Level 2: Merge all module dependent jars into the same lib directory

Reference project directory: package-optimize-level2

Solve the problem:

  • Merge all module dependent jars into the same lib directory. Generally, due to the high degree of overlap in the dependent jars of each module project, merge all service deployments The total file size is basically two to three hundred MB

  • But if you use -Djava.ext.dirs=lib to load all jars into each JVM, each JVM will be fully loaded. All jars consume resources. Secondly, different versions of each microservice component may cause version conflicts.

Main configuration:

Please give detailed instructions for key configurations See the following comments:

<build>
    <finalName>${project.artifactId}</finalName>
    <!--
    特别注意:
    项目仅仅是为了演示配置方便,直接在parent的build部分做了插件配置和运行定义。
    但是实际项目中需要把这些定义只放到spring boot模块项目(可优化使用pluginManagement形式),避免干扰其他util、common等模块项目
    -->
    <plugins>
        <!-- 基于maven-jar-plugin插件实现把依赖jar定义写入输出jar的META-INFO/MANIFEST文件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <useUniqueVersions>false</useUniqueVersions>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <!-- 拷贝项目所有依赖jar文件到构建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>
                        <!--
                        各子模块按照实际层级定义各模块对应的属性值,检查所有微服务模块依赖jar文件合并复制到同一个目录
                        详见各子模块中 boot-jar-output 属性定义
                        -->
                        <outputDirectory>${boot-jar-output}/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <silent>false</silent>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Spring Boot模块jar构建 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includes>
                    <!-- 不存在的include引用,相当于排除所有maven依赖jar,没有任何三方jar文件打入输出jar -->
                    <include>
                        <groupId>null</groupId>
                        <artifactId>null</artifactId>
                    </include>
                </includes>
                <layout>ZIP</layout>
                <!--
                基于maven-jar-plugin输出微服务jar文件进行二次spring boot重新打包文件的输出目录
                所有微服务构建输出jar文件统一输出到与lib同一个目录,便于共同引用同一个lib目录
                详见各子模块中boot-jar-output属性定义
                -->
                <!--  -->
                <outputDirectory>${boot-jar-output}</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy after login

All lib directory files and jars built by each microservice are aggregated into the devops public directory.

The META-INFO/MANIFEST file in the microservice jar file will generate the Class-Path attribute based on the module's dependent component list, thus avoiding different versions of jar:

Class-Path: lib/spring-boot-starter-web-2.4.3.jar lib/spring-boot-starte
 r-2.4.3.jar lib/spring-boot-2.4.3.jar lib/spring-boot-autoconfigure-2.4
 .3.jar lib/spring-boot-starter-logging-2.4.3.jar lib/logback-classic-1.
 2.3.jar lib/logback-core-1.2.3.jar lib/slf4j-api-1.7.30.jar lib/log4j-t
 o-slf4j-2.13.3.jar lib/log4j-api-2.13.3.jar lib/jul-to-slf4j-1.7.30.jar
  lib/jakarta.annotation-api-1.3.5.jar lib/spring-core-5.3.4.jar lib/spr
 ing-jcl-5.3.4.jar lib/snakeyaml-1.27.jar lib/spring-boot-starter-json-2
 .4.3.jar lib/jackson-databind-2.11.4.jar lib/jackson-annotations-2.11.4
 .jar lib/jackson-core-2.11.4.jar lib/jackson-datatype-jdk8-2.11.4.jar l
 ib/jackson-datatype-jsr310-2.11.4.jar lib/jackson-module-parameter-name
 s-2.11.4.jar lib/spring-boot-starter-tomcat-2.4.3.jar lib/tomcat-embed-
 core-9.0.43.jar lib/jakarta.el-3.0.3.jar lib/tomcat-embed-websocket-9.0
 .43.jar lib/spring-web-5.3.4.jar lib/spring-beans-5.3.4.jar lib/spring-
 webmvc-5.3.4.jar lib/spring-aop-5.3.4.jar lib/spring-context-5.3.4.jar 
 lib/spring-expression-5.3.4.jar
Copy after login

Configuration Output:

cd package-optimize-level2
mvn clean install

ls -lh devops/
total 912
drwxr-xr-x  34 lixia  wheel   1.1K Feb 24 22:27 lib
-rw-r--r--   1 lixia  wheel   150K Feb 24 22:31 package-optimize-app1.jar
-rw-r--r--   1 lixia  wheel   149K Feb 24 22:31 package-optimize-app2.jar
-rw-r--r--   1 lixia  wheel   149K Feb 24 22:31 package-optimize-app3.jar

java -jar devops/package-optimize-app1.jar
Copy after login

Implementation effect:

  • The startup process no longer requires the -Djava.ext.dirs=lib parameter definition.

  • All microservice jars refer to the common directory of all projects and merged dependent components. The total size of the deployment files is generally two to three hundred MB.

  • Solve the problem of version conflicts of different components of each microservice by customizing the Class-Path in the META-INFO/MANIFEST file in each microservice jar file to clearly indicate the dependent version component class.

Level 3: Support unofficial third-party dependent components introduced by system

Reference project directory: package-optimize-level3

Solution to the problem:

  • One way to do some unofficial third-party jars such as sdk jars is to submit them to the Maven local private server for reference, which is the same as ordinary dependent jars. The processing is the same; but in the absence of a maven private server, a common simplification method is to place the dependent jar directly in the project and then define it in the pom with system scope.

  • For the maven-jar-plugin component that is introduced in the pom through systemPath, there is no direct parameter declaration for the component containing the specified scope. If no special processing is done in META-INFO/MANIFEST Components defined by these scopes will not appear, causing the runtime class to not be found.

Main configuration:

Please refer to the following comments for key configuration instructions:

<build>
    <finalName>${project.artifactId}</finalName>
    <!--
    特别注意:
    项目仅仅是为了演示配置方便,直接在parent的build部分做了插件配置和运行定义。
    但是实际项目中需要把这些定义只放到spring boot模块项目(可优化使用pluginManagement形式),避免干扰其他util、common等模块项目
    -->
    <plugins>
        <!-- 基于maven-jar-plugin插件实现把依赖jar定义写入输出jar的META-INFO/MANIFEST文件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <useUniqueVersions>false</useUniqueVersions>
                    </manifest>
                    <manifestEntries>
                        <!--
                        有些非官方三方的诸如sdk jar在pom中是以systemPath方式引入的,maven-jar-plugin组件没有直接参数声明包含指定scope的组件
                        通过使用额外定义 Class-Path 值来追加指定依赖组件列表,在子模块按实际情况指定 jar-manifestEntries-classpath 值即可
                        例如(注意前面个点字符及各空格分隔符):. lib/xxx-1.0.0.jar lib/yyy-2.0.0.jar
                        详见各子模块中 boot-jar-output 属性定义示例
                        -->
                        <Class-Path>${jar-manifestEntries-classpath}</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        <!-- 拷贝项目所有依赖jar文件到构建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>
                        <!--
                        各子模块按照实际层级定义各模块对应的属性值,检查所有微服务模块依赖jar文件合并复制到同一个目录
                        详见各子模块中 boot-jar-output 属性定义
                        -->
                        <outputDirectory>${boot-jar-output}/lib</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <silent>false</silent>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Spring Boot模块jar构建 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includes>
                    <!-- 不存在的include引用,相当于排除所有maven依赖jar,没有任何三方jar文件打入输出jar -->
                    <include>
                        <groupId>null</groupId>
                        <artifactId>null</artifactId>
                    </include>
                </includes>
                <layout>ZIP</layout>
                <!--
                基于maven-jar-plugin输出微服务jar文件进行二次spring boot重新打包文件的输出目录
                所有微服务构建输出jar文件统一输出到与lib同一个目录,便于共同引用同一个lib目录
                详见各子模块中boot-jar-output属性定义
                -->
                <!--  -->
                <outputDirectory>${boot-jar-output}</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy after login

Main configuration of sub-module:

    <properties>
        <!-- 按各模块实际目录层次定义相对数据,使所有服务模块输出资源汇聚到相同目录 -->
        <boot-jar-output>../devops</boot-jar-output>
        <!--
        有些供应商的sdk jar在pom中是以systemPath方式引入的,maven-jar-plugin组件没有直接参数声明包含指定scope的组件
        通过使用额外定义 Class-Path 值来追加指定依赖组件列表,按实际情况指定 jar-manifestEntries-classpath 值即可
        例如(注意前面个点字符及各空格分隔符,lib后面部分是 artifactId-version.jar 格式而不是实际文件名):. lib/xxx-1.0.0.jar lib/yyy-2.0.0.jar
        -->
        <jar-manifestEntries-classpath>. lib/hik-sdk-1.0.0.jar</jar-manifestEntries-classpath>
    </properties>
    <dependencies>
        <!-- 以相对路径方式定义非官方三方依赖组件 -->
        <dependency>
            <groupId>com.hik</groupId>
            <artifactId>hik-sdk</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/hik-sdk-1.0.0.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
Copy after login

The META-INFO/MANIFEST file in the microservice output jar file will generate the Class-Path attribute based on the module dependent component list, and the jar-manifestEntries-classpath attribute definition value will be appended to the front:

Class-Path: . lib/hik-sdk-1.0.0.jar lib/spring-boot-starter-web-2.4.3.ja
 r lib/spring-boot-starter-2.4.3.jar lib/spring-boot-2.4.3.jar lib/sprin
 g-boot-autoconfigure-2.4.3.jar lib/spring-boot-starter-logging-2.4.3.ja
 r lib/logback-classic-1.2.3.jar lib/logback-core-1.2.3.jar lib/slf4j-ap
 i-1.7.30.jar lib/log4j-to-slf4j-2.13.3.jar lib/log4j-api-2.13.3.jar lib
 /jul-to-slf4j-1.7.30.jar lib/jakarta.annotation-api-1.3.5.jar lib/sprin
 g-core-5.3.4.jar lib/spring-jcl-5.3.4.jar lib/snakeyaml-1.27.jar lib/sp
 ring-boot-starter-json-2.4.3.jar lib/jackson-databind-2.11.4.jar lib/ja
 ckson-annotations-2.11.4.jar lib/jackson-core-2.11.4.jar lib/jackson-da
 tatype-jdk8-2.11.4.jar lib/jackson-datatype-jsr310-2.11.4.jar lib/jacks
 on-module-parameter-names-2.11.4.jar lib/spring-boot-starter-tomcat-2.4
 .3.jar lib/tomcat-embed-core-9.0.43.jar lib/jakarta.el-3.0.3.jar lib/to
 mcat-embed-websocket-9.0.43.jar lib/spring-web-5.3.4.jar lib/spring-bea
 ns-5.3.4.jar lib/spring-webmvc-5.3.4.jar lib/spring-aop-5.3.4.jar lib/s
 pring-context-5.3.4.jar lib/spring-expression-5.3.4.jar
Copy after login

Configuration output:

cd package-optimize-level3
mvn clean install

ls -lh devops/
total 912
drwxr-xr-x  36 lixia  wheel   1.1K Feb 24 23:14 lib
-rw-r--r--@  1 lixia  wheel   150K Feb 24 23:14 package-optimize-app1.jar
-rw-r--r--   1 lixia  wheel   150K Feb 24 23:14 package-optimize-app2.jar
-rw-r--r--   1 lixia  wheel   150K Feb 24 23:14 package-optimize-app3.jar

java -jar devops/package-optimize-app1.jar
Copy after login

Final implementation effect

  • The dependent components of all services are merged into one directory. The total size is two to three hundred MB, and the transmission efficiency is significantly accelerated during the first deployment.

  • Each microservice jar is one to two hundred KB in size. Daily emergency bug fixes and updates to individual jars are basically instantaneous.

  • Each microservice jar defines a list of components that depend on the specified version, so there will be no loading conflicts between different versions of components.

  • Unofficial third-party dependent components can also be referenced and processed normally.

The above is the detailed content of How to deploy Jar files in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template