How 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:
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:
The structure of the original jar package is
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
<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
as follows: Start in the same-level directory
Do not start in the same level directory:
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:
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
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.
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
这个可以自定义执行主入口类,有以下几种方式:
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!

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

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.

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

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

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

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

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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
Useful JavaScript development tools
