Home  >  Article  >  Java  >  How to use springboot packaging plug-in to remove jar packages and slim down

How to use springboot packaging plug-in to remove jar packages and slim down

王林
王林forward
2023-05-16 17:13:061156browse

1. Pom file configuration

1.1 Add the maven-dependency-plugin plug-in to copy the referenced jar package to the specified path

Facilitates the subsequent tomcat startup to specify the dependency package path

<!--拷贝依赖到jar外面的lib目录-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>                <!--指定的依赖路径-->
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

After using this plug-in to build, the directory structure has an additional lib directory (that is, the path specified by the outputDirectory configured above), which contains the dependent jar package:

How to use springboot packaging plug-in to remove jar packages and slim down

How to use springboot packaging plug-in to remove jar packages and slim down

1.2 The springboot project uses spring-boot-maven-plugin to package the plug-in

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
     <layout>ZIP</layout>
        <mainClass>
            com.iasp.BasicStarter
        </mainClass>
        <!--只包含自己-->
        <includes>
            <include>
                <groupId>${groupId}</groupId>
                <artifactId>${artifactId}</artifactId>
            </include>
         <!--或者-->
           <!--依赖jar不打进项目jar包中-->
             <!--<include>
                  <groupId>nothing</groupId>
                  <artifactId>nothing</artifactId>
             </include>-->
        </includes>
        <!--不包含哪些-->
        <!--<excludeGroupIds>-->
            <!--com.hundsun.jrescloud,-->
            <!--org.springframework.boot,-->
            <!--org.springframework-->
        <!--</excludeGroupIds>-->
    </configuration>

    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

After configuring the above packaging, the corresponding jar package will be excluded, making the Flat package made by the plug-in The size of the jar package has become smaller, making it easier to upload to the server for publishing. The effect is as follows. The lib directory under the BOOT-INF directory is gone:

How to use springboot packaging plug-in to remove jar packages and slim down

The structure of the original jar package is

How to use springboot packaging plug-in to remove jar packages and slim down

Then specify the jar package path -Dloader.path="../lib" when starting the project, so that the slimming effect can be achieved, and the dependencies are placed in D: In the develop/shared/fjar directory, execute the run command

java -Dloader.path="D:develop/shared/fjar" -jar mytest.jar

Note: Another startup solution is to specify the path without adding -Dloader.path="D:develop/shared/fjar", and use it directly as follows Instruction startup

java -jar mytest.jar

If you use the above startup, you need to add the maven-jar-plugin plug-in and configure the attribute. In addition, when processing some files that read executable jars, you can use maven-jar-plugin The plug-in replaces spring-boot-maven-plugin for packaging operation

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <!--addClasspath表示需要加入到类构建路径-->
                <addClasspath>true</addClasspath>
                <!--classpathPrefix指定生成的Manifest文件中Class-Path依赖lib前面都加上路径,构建出lib/xx.jar-->
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.common.util.CommonUtilsApplication</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

The effect of the above plug-in is to add the Class-path corresponding jar to the MANIFEST.MF file under the META_INF directory in the package, so that the just-used jar can be used later At startup, only the required version dependencies will be loaded according to the Class-Path (to solve the problem of multi-version loading reference conflicts in the shared directory). This effect is equivalent to adding the parameter -classpath xxx (specific jar).

At this time, just put the required jar directory lib in the same directory as the xxx.jar to be run. You can not add the -Dloader.path parameter when starting. If the lib directory is the same as the xxx.jar to be run, If xxx.jar is not in the same-level directory, you need to use -Dloader.path to start

How to use springboot packaging plug-in to remove jar packages and slim down

as follows: Start in the same-level directory

How to use springboot packaging plug-in to remove jar packages and slim down

Do not start in the same level directory:

How to use springboot packaging plug-in to remove jar packages and slim down

Among them, -Dloader.path can specify multiple directories, so that when there are multiple microservices, some common The jars used are placed in a shared directory. The jars unique to each microservice can be placed in the private directory of the microservice (to solve the problem of jar version conflicts). The example is as follows:

How to use springboot packaging plug-in to remove jar packages and slim down

Note:

1. When using -Dloader.path, you need to add ZIP when packaging. If not specified, -Dloader.path will not take effect.

For slimming packaging of multiple microservices, it is recommended to use maven-jar-plugin packaging to avoid some application startup problems caused by the spring-boot-maven-plugin packaging mechanism (trapped)

2. If there are different version dependencies:

For example, project A depends on version 1.0 of Y library, and project B depends on version 2.0 of Y library, then there may be a version dependency conflict (when the two versions are incompatible) , Solution:

2.1. If the version can be consistent, keep using the same version to ensure that the version is consistent. You can use maven's version dependency management for processing, that is, use in the parent pom file to uniformly manage dependency versions

2.2. Let the projects depend on the required versions and put them into the war package, and put other same dependencies into the war package. version of the jar package under the same shared package

The test found that the dependency was searched from top to bottom when searching. If it matches, the first one will be used. As shown below, the comm-0.0.1.jar version will be used.

How to use springboot packaging plug-in to remove jar packages and slim down

Note:

Using the spring-boot-maven-plugin plug-in, all dependent jar packages will be packaged, so You can directly run the generated JAR package, which simplifies our development operations.

使用spring-boot-maven-plugin插件如果不指定程序主运行入口类的话默认为Main-Class: org.springframework.boot.loader.JarLauncher

How to use springboot packaging plug-in to remove jar packages and slim down

这个可以自定义执行主入口类,有以下几种方式:

1.POM继承spring-boot-starter-parent

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<properties>
        <!-- The main class to start by executing java -jar -->
        <start-class>ccom.notes.JavaNotesApplication</start-class>
</properties>

2.POM不是继承spring-boot-starter-parent时需指定

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>2.1.9.RELEASE</version>
  <configuration>
    <mainClass>com.notes.JavaNotesApplication</mainClass>
    <layout>ZIP</layout>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

3.POM不是继承spring-boot-starter-paren,且使用maven-jar-plugin插件来指定执行的类

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <configuration>
      <archive>
         <manifest>
            <addClasspath>true</addClasspath>
            <useUniqueVersions>false</useUniqueVersions>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.notes.JavaNotesApplication</mainClass>
         </manifest>
         <manifestEntries>
            <version>${project.version}</version>
         </manifestEntries>
      </archive>
   </configuration>
</plugin>

The above is the detailed content of How to use springboot packaging plug-in to remove jar packages and slim down. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete