1. pom.xml中的配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
</configuration>
<!--
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>classes</classifier>
<attach>false</attach>
</configuration>
</execution>
</executions>
-->
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<finalName>saas-wechat-webapp-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
插件 | 說明 |
---|---|
maven-jar-plugin | 通過此配置躬审,可以在生成jar包時(shí)碴巾,不包含resources中的配置文件以及可能存在的網(wǎng)頁(yè)模板等信息 |
spring-boot-maven-plugin | 這個(gè)是spring boot使用的插件鼓寺,通過相關(guān)的配置可以不生成fat jar包,即在jar包中不包含關(guān)聯(lián)的jar包贤壁,其中的executions配置是可以將springboot啟動(dòng)相關(guān)的類單獨(dú)打在一個(gè)包中,不需要的話埠忘,就注釋掉即可 |
maven-assembly-plugin | 這個(gè)assembly插件脾拆,可以根據(jù)需要生成相關(guān)的發(fā)布目錄,并進(jìn)行壓縮打包處理莹妒。使用此插件后名船,可以在命令行運(yùn)行mvn package就會(huì)自動(dòng)進(jìn)行打包處理 |
2. assembly.xml配置文件
assembly插件是根據(jù)配置將代碼進(jìn)行組織,如果我們不使用fatjar包格式旨怠,那么可以使用這個(gè)插件渠驼,組織好發(fā)布運(yùn)行相關(guān)的目錄。
準(zhǔn)備發(fā)布的項(xiàng)目目錄結(jié)構(gòu)如下:
$ ---
|-- config:保存系統(tǒng)相關(guān)的配置文件
|-- static: 保存所有靜態(tài)頁(yè)面的文件鉴腻,如果是前后端分離的情況迷扇,就是前端代碼所在
|-- resources: 保存項(xiàng)目自己的頁(yè)面模板等代碼
|-- lib: 保存所有依賴的jar包
|-- xxxxx-main.jar: 項(xiàng)目主要的運(yùn)行包百揭,包括了springboot所有相關(guān)的啟動(dòng)類
|-- startup.sh: 系統(tǒng)啟動(dòng)腳本
|-- shutdown.sh: 系統(tǒng)停止腳本
- 通過以下配置將配置文件拷貝到發(fā)布包的config目錄下
<fileSet>
<directory>target/classes</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.yml</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
- 通過以下配置將頁(yè)面模板等信息拷貝到發(fā)布包的static目錄下
<fileSet>
<directory>target/classes</directory>
<outputDirectory>resources</outputDirectory>
<includes>
<include>static/**</include>
<include>templates/**</include>
</includes>
</fileSet>
- 通過以下配置將項(xiàng)目所有依賴包拷貝到發(fā)布包的lib目錄下
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
3. 定制run命令
spring boot classpath搜索次序:
1. 當(dāng)前目錄下的 /config 子目錄。
2. 當(dāng)前目錄蜓席。
3. classpath下的 /config 包器一。
4. classpath根路徑(root)。
該列表是按優(yōu)先級(jí)排序的(列表中位置高的路徑下定義的屬性將覆蓋位置低的)
spring boot對(duì)資源文件的處理方式:
默認(rèn)的靜態(tài)資源路徑有:
classpath:/META-INF/resources/瓮床,classpath:/resources/盹舞,classpath:/static/,classpath:/public/
隘庄,都是放在classpath中踢步,在fatjar的情況下,都是直接壓縮在jar包中的丑掺,要做到修改非常不方便获印。-
可以通過修改
spring.resources.static-locations
使用默認(rèn)的配置失效,從而定義自己指定的地址街州,如spring.resources.static-locations=classpath:/META-INF/resources/,\ classpath:/resources/,\ classpath:/static/,\ classpath:/public/,\ file:/data/service/saas-ggb-server/static/,\ file:${web.file.upload.path},\ file:${qr.path}
從spring boot搜索classpath來說兼丰,如果static、public唆缴、resources等目錄在當(dāng)前運(yùn)行目錄下鳍征,也可以搜索到
在啟動(dòng)時(shí)直接使用類似如下的命令即可:
java -Dloader.path=./lib,config -jar xxxxx-main.jar
4. 使用jenkins部署時(shí)的要點(diǎn)
- 在第一次發(fā)布時(shí),可以直接把打包好的zip上傳到服務(wù)器進(jìn)行安裝(解壓即可)
- 以后在發(fā)布時(shí)面徽,基本不需要更新常用的第三方j(luò)ar包艳丛,只需要更新與業(yè)務(wù)相關(guān)的自己的jar包即可,所以在對(duì)maven項(xiàng)目命名時(shí)趟紊,最好設(shè)計(jì)相關(guān)便于識(shí)別的jar包的名稱