Maven 打包的时候,如果包含了第三方的jar包,这个打出来的jar文件会很大,所有有时候我们希望在打包的过程中不包含第三方的jar文件。
1. 如何打一个不包含第三方jar文件的包呢?
只需在pom文件中加入下面的代码即可
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <!-- generate jar without third part jars --> <groupId>org.springframework.boot.experimental</groupId> <artifactId>spring-boot-thin-layout</artifactId> <version>1.0.12.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
2. 如果打出来的包不包含第三方jar是没法运行的,所以我们想把第三方的jar也自动生成到某个目录下
<!-- Copy all third part jars to lib directory--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
3. 有时候想要同时打出包含第三方jar文件的包和不包含第三方jar文件的包,加入下面代码就能打出包含第三方文件的包
<!-- generate package include third part jar--> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass></mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>