新建Spring Boot后,会自带打包方式,如今通常都是打包成jar包,固然你想打包成war包也能够,我就不介绍了! 本文主要想谈谈自带的打包方式和assembly打包方式,这二者有什么区别和优缺点呢?spring
使用IDEA 的 spring initializr
或者start.spring.io
建立 Spring Boot
项目后,能够在 pom.xml
文件中看到自带的 maven
打包方式数据库
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
使用命令 mvn clean package
便可将项目打包成jar包,但这种打包的方式将全部的配置文件和模板文件(若是存在template的话)都打包在jar中,若是更改,必须从新打包。apache
思考一下运维
这样的打包方式确实很是简单和方便,可是当咱们修改配置时,就须要从新打包发布,还有个问题就是,线上数据库地址是在配置文件中的,开发人员通常是不知道的(固然运维也不会告诉你,省得误操做),那难道让运维去打包??明显不可能!因此咱们能够采起下面的assembly
打包方式!maven
第一步: 排除Spring Boot 自带的打包插件:注释或删除pom.xml中的代码ide
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>com.joyreach.base.JoyBaseServerApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
第二步: 添加assembly打包插件,在pom.xml中添加spring-boot
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <finalName>${project.artifactId}</finalName> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
第三步 配置assembly: 首先在pom.xml中,添加以下代码,分离配置文件:测试
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.2</version> <configuration> <excludes> <exclude>*.*</exclude> </excludes> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> <addClasspath>true</addClasspath> <classpathPrefix>../lib/</classpathPrefix> <mainClass>com.guuidea.basesave.BaseSaveApplication</mainClass> </manifest> <manifestEntries> <Class-Path>../conf/</Class-Path> </manifestEntries> </archive> </configuration> </plugin>
其次,在assemble.xml中配置ui
<?xml version="1.0" encoding="UTF-8" ?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>${project.version}</id> <formats> <format>zip</format> </formats> <dependencySets> <!-- 项目的依赖 --> <dependencySet> <!-- 排除当前项目的构件 --> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> <fileSet> <directory>lib/</directory> <outputDirectory>${file.separator}lib</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>${file.separator}bin</outputDirectory> <includes> <include>${project.artifactId}-${project.version}.jar</include> </includes> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>${file.separator}conf</outputDirectory> </fileSet> </fileSets> </assembly>
说下我遇到的坑:idea
两种方式各有利弊吧,默认的方式方便快捷,更适合用来开发,测试。 assembly打包方式则是去服务化和工程化的,更适用公司的流程和生产。 若是公司大部分项目部署,是由开发来完成的那么推荐采用自带的方式,若是有运维专门维护上线,用assembly更为规范一些。