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:
<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:
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)
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
<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>
<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>
<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!