1.在传统jar包中,所有文件都打包到一个jar包中,jar非常臃肿,在进行服务更新部署时非常不便,而且传输不稳定时导致传输失败。如果过实行文件分离、在依赖不改变的情况下,仅仅上传更新后的 编译文件是非常方便的。
maven-jar-plugin 插件打包jar
在pom文件中配置,但是这样 依赖的jar并不会打进来(后面会有解决方法),适用不需要依赖文件的项目。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3</version> <configuration> <archive> <manifest> <!--是否添加依赖--> <addClasspath>true</addClasspath> <!--设置启动类--> <mainClass>xxx.xxx.Main</mainClass> </manifest> </archive> <!--设置生成jar输出位置--> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin>
maven-assembly-plugin 插件打包jar
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!--不添加AssemblyId--> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <!--配置打包的时候一并打包依赖jar--> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <!--入口类--> <mainClass>xxx.xxx.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!--绑定生命周期--> <phase>package</phase> <goals> <!--执行assembly --> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
方案一、
<plugins> <!--打包jar--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <!--MANIFEST.MF 中 Class-Path 加入前缀--> <classpathPrefix>lib/</classpathPrefix> <!--jar包不包含唯一版本标识--> <useUniqueVersions>false</useUniqueVersions> <!--指定入口类--> <mainClass>xxx.xxx.Application</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录--> <Class-Path>/resources</Class-Path> </manifestEntries> </archive> <outputDirectory>${project.build.directory}/dis</outputDirectory> </configuration> </plugin> <!--拷贝依赖 copy-dependencies--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/dis/lib/ </outputDirectory> </configuration> </execution> </executions> </plugin> <!--拷贝资源文件 copy-resources--> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <outputDirectory>${project.build.directory}/dis/resources</outputDirectory> </configuration> </execution> </executions> </plugin> <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layout>ZIP</layout> <!--使用外部配置文件,jar包里没有资源文件--> <addResources>true</addResources> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
方案二
这里依赖assembly.xml 描述文件
`
<!-- 打包成jar文件,并指定lib文件夹以及resources资源文件夹 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>xxx.xxx.Application</mainClass> <!--依赖前缀--> <classpathPrefix>lib/</classpathPrefix> <addClasspath>true</addClasspath> </manifest> <manifestEntries> <Class-Path>resources/</Class-Path> </manifestEntries> </archive> </configuration> </plugin> </plugins>
> assembly.xml
<id>distribution</id> <!--输出格式 zip 最终结果生成zip --> <formats> <format>zip</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <!--设置需要输出文件--> <fileSets> <fileSet> <directory>src/main/resources/</directory> <outputDirectory>/resources</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <!--依赖包的输出目录--> <outputDirectory>/lib</outputDirectory> <scope>runtime</scope> <excludes> <exclude>${project.groupId}:${project.artifactId}</exclude> </excludes> </dependencySet> <dependencySet> <!--jar包的输出目录-->Spring Boot Jar 包 配置、依赖文件分离 <outputDirectory>/</outputDirectory> <includes> <include>${project.groupId}:${project.artifactId}</include> </includes> </dependencySet> </dependencySets>