精通Spring Boot ——第二十二篇:打包方式的選擇

1.背景介紹

新建Spring Boot後,會自帶打包方式,如今通常都是打包成jar包,固然你想打包成war包也能夠,我就不介紹了! 本文主要想談談自帶的打包方式和assembly打包方式,這二者有什麼區別和優缺點呢?spring

2.自帶打包方式

使用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

3.assembly打包方式

第一步: 排除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

  • 1.include必定要將全部的配置文件都包含進去。
    1. exclude 必定要排除jar包下的全部配置文件,不然,將會默認先使用jar包中的配置,這也就是爲何修改了conf目錄下的配置文件後,沒有生效的緣由!

4.總結一下

兩種方式各有利弊吧,默認的方式方便快捷,更適合用來開發,測試。 assembly打包方式則是去服務化和工程化的,更適用公司的流程和生產。 若是公司大部分項目部署,是由開發來完成的那麼推薦採用自帶的方式,若是有運維專門維護上線,用assembly更爲規範一些。

相關文章
相關標籤/搜索