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

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:亿速云. If there is any infringement, please contact admin@php.cn delete
What aspects of Java development are platform-dependent?What aspects of Java development are platform-dependent?Apr 26, 2025 am 12:19 AM

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Are there performance differences when running Java code on different platforms? Why?Are there performance differences when running Java code on different platforms? Why?Apr 26, 2025 am 12:15 AM

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

What are some limitations of Java's platform independence?What are some limitations of Java's platform independence?Apr 26, 2025 am 12:10 AM

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Explain the difference between platform independence and cross-platform development.Explain the difference between platform independence and cross-platform development.Apr 26, 2025 am 12:08 AM

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?Apr 26, 2025 am 12:02 AM

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Why is Java a popular choice for developing cross-platform desktop applications?Why is Java a popular choice for developing cross-platform desktop applications?Apr 25, 2025 am 12:23 AM

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Discuss situations where writing platform-specific code in Java might be necessary.Discuss situations where writing platform-specific code in Java might be necessary.Apr 25, 2025 am 12:22 AM

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

What are the future trends in Java development that relate to platform independence?What are the future trends in Java development that relate to platform independence?Apr 25, 2025 am 12:12 AM

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages ​​such as Python and JavaScript to enhance cross-language interoperability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools