針對使用appassembler-maven-plugin打包插件對項目進行打包的項目,實現使用jenkins進行自動化部署html
搭建過程.......省略java
在jenkins中設置你要部署的服務器linux
從主頁面中進入到配置ssh服務器地址shell
配置完畢之後,點擊Test Configuration測試一下是否能夠鏈接($\color{red}{注意:你要遠程部署的服務器必須給當前jenkins所在的服務器開放互相信任}$)
複製代碼
<build>
<plugins>
<!-- 目前採用wrapper打包部署 -->
<plugin>
<!-- http://www.mojohaus.org/appassembler/appassembler-maven-plugin/usage-daemon-generatorconfig.html -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>generate-jsw-scripts</id>
<phase>package</phase>
<goals>
<goal>generate-daemons</goal>
</goals>
<configuration>
<repositoryLayout>flat</repositoryLayout>
<encoding>${project.build.sourceEncoding}</encoding>
<!--<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>-->
<!-- copy the src/main/resources directory to /src -->
<!--爲系統配置文件指定一個目錄-->
<configurationDirectory>conf</configurationDirectory>
<!--源代碼中對應的系統配置文件的位置-->
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<logsDirectory>logs</logsDirectory>
<daemons>
<daemon>
<id>${daemon-name}</id>
<mainClass>${daemon-mainClass}</mainClass>
<jvmSettings>
<initialMemorySize>${daemon-JAVA_Xmx}</initialMemorySize>
<maxMemorySize>${daemon-JAVA_Xmx}</maxMemorySize>
<extraArguments>
<!-- Note : if the value is empty the plugin will throw NullPointerException-->
<extraArgument>-Djava.wrapper=1 ${daemon-JAVA_OPS}</extraArgument>
<extraArgument>-verbose:gc</extraArgument>
<extraArgument>-XX:+PrintHeapAtGC</extraArgument>
<extraArgument>-XX:+PrintGCDetails</extraArgument>
<extraArgument>-XX:+PrintGCDateStamps</extraArgument>
<extraArgument>-XX:+PrintGCTimeStamps</extraArgument>
<extraArgument>-XX:+PrintTenuringDistribution</extraArgument>
<extraArgument>-XX:+PrintGCApplicationStoppedTime</extraArgument>
<extraArgument>-Xloggc:logs/gc.log</extraArgument>
<extraArgument>-XX:+HeapDumpOnOutOfMemoryError</extraArgument>
<extraArgument>-XX:HeapDumpPath=logs/gc.hprof</extraArgument>
<extraArgument>-XX:ErrorFile=logs/java_error_%p.log</extraArgument>
<!--<extraArgument>-Xbootclasspath/p:lib/alpn-boot-${alpn.boot.version}.jar</extraArgument>-->
</extraArguments>
</jvmSettings>
<platforms>
<platform>jsw</platform>
</platforms>
<generatorConfigurations>
<generatorConfiguration>
<generator>jsw</generator>
<includes>
<include>linux-x86-32</include>
<include>linux-x86-64</include>
<include>macosx-universal-64</include>
<include>macosx-universal-32</include>
<include>windows-x86-32</include>
<include>windows-x86-64</include>
</includes>
<configuration>
<property>
<name>configuration.directory.in.classpath.first</name>
<value>conf</value>
</property>
<property>
<name>set.default.REPO_DIR</name>
<value>lib</value>
</property>
<property>
<name>wrapper.java.command</name>
<value>%JAVA_HOME%/bin/java</value>
</property>
<property>
<name>wrapper.logfile</name>
<value>logs/wrapper.log</value>
</property>
<property>
<name>wrapper.startup.timeout</name>
<value>7200</value><!-- 2小時 -->
</property>
<property>
<name>wrapper.ping.timeout</name>
<value>3600</value><!-- 1小時 -->
</property>
<property>
<name>wrapper.ntservice.name</name>
<value>${wrapper.ntservice.name}</value>
</property>
<property>
<name>wrapper.ntservice.displayname</name>
<value>${wrapper.ntservice.displayname}</value>
</property>
<property>
<name>wrapper.ntservice.description</name>
<value>${wrapper.ntservice.description}</value>
</property>
<property>
<name>wrapper.console.title</name>
<value>${wrapper.ntservice.name}</value>
</property>
<property>
<!-- 若是pid文件已經存在則不啓動程序 -->
<name>wrapper.pidfile.strict</name>
<value>TRUE</value>
</property>
</configuration>
</generatorConfiguration>
</generatorConfigurations>
</daemon>
</daemons>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
複製代碼
注意: 若是手動打包的時候,不要使用idea內置的插件(install指令),有時候會有坑macos
#!/bin/sh
#######變量設置
#輸出間隔標識
DASH_LINE_TAG="------------"
#目標機器
TARGET_SERVER="填寫對應服務器的ip"
#目標用戶
TARGET_USER=root
#jenkins的workspace目錄
JENKINS_WORKSPACE="/${TARGET_USER}/.jenkins/workspace"
#打包出來的目錄地址 詳細的修改根據本身項目的最終打包出來的結構作必定調整 其中data-server-test表示的你在jenkins中構建項目的名稱,後面纔是項目打包的路徑
JENKINS_SOURCE_TARGET_HOME="${JENKINS_WORKSPACE}/jenkins構建工程名稱/打包插件打包出來後的目錄結構"
#修改workspace對應項目conf下的wrapper.conf jdk路徑
sed -i "s\\%JAVA_HOME%/bin/java\\部署機器所在的jdk位置(例如/soft/jdk1.8/java)\\g" ${JENKINS_SOURCE_TARGET_HOME}/conf/wrapper.conf
#app所在路徑
APP_HOME="/soft/data-server"
#備份app的目錄
APP_BACKUP="/soft/Backup/"
#備份文件所在目錄
CONF_BAKUP_HOME="/soft/perioperative-jenkins/data-server/conf_bak"
#執行文件服務名
SERVICE_NAME=data-server
########遠程執行流程
#中止服務
echo "${DASH_LINE_TAG}中止服務: ${APP_HOME}/bin/${SERVICE_NAME} stop"
ssh ${TARGET_USER}@${TARGET_SERVER} "${APP_HOME}/bin/${SERVICE_NAME} stop"
#備份文件
echo "${DASH_LINE_TAG}備份文件到: ${APP_BACKUP}"
ssh ${TARGET_USER}@${TARGET_SERVER} "rm -rf ${APP_BACKUP}data-server"
ssh ${TARGET_USER}@${TARGET_SERVER} "cp -rf ${APP_HOME}/ ${APP_BACKUP}"
#刪除原文件
echo "${DASH_LINE_TAG}刪除原文件:${APP_HOME}"
ssh ${TARGET_USER}@${TARGET_SERVER} "rm -rf ${APP_HOME}/*"
#複製新文件到目錄
echo "${DASH_LINE_TAG}複製新文件到目錄:${JENKINS_SOURCE_TARGET_HOME} ---> ${TARGET_USER}@${TARGET_SERVER}:${APP_HOME}"
scp -r ${JENKINS_SOURCE_TARGET_HOME}/* ${TARGET_USER}@${TARGET_SERVER}:${APP_HOME}
#複製備份的配置文件到目錄 這是你設置的配置文件地址,配置文件會根據部署不一樣機器使用不一樣的配置文件,一開始設置好,自動打包的時候自動替換,就不用每次部署都手動修改了
echo "${DASH_LINE_TAG}複製備份的配置文件到目錄: ${CONF_BAKUP_HOME}/conf"
ssh ${TARGET_USER}@${TARGET_SERVER} "cp -rf ${CONF_BAKUP_HOME}/* ${APP_HOME}/conf/"
#更改執行文件權限
echo "${DASH_LINE_TAG}更改執行文件權限:${APP_HOME}/bin/"
ssh ${TARGET_USER}@${TARGET_SERVER} "chmod +x ${APP_HOME}/bin/*"
#啓動服務
echo "${DASH_LINE_TAG}啓動服務: ${APP_HOME}/bin/${SERVICE_NAME} start"
ssh ${TARGET_USER}@${TARGET_SERVER} "${APP_HOME}/bin/${SERVICE_NAME} start"
複製代碼
填寫完maven的相關配置之後再增長構建步驟,選擇執行shellwindows
很是強調bash
腳本中有一步,是把jenkins打包好的本地文件,複製到遠程服務器中服務器
咱們的遠程服務器必需要有這個文件夾,否則就會報錯提示說找不到這個目錄,jenkins構建就會報錯失敗,這個時候咱們能夠選擇手動去建立(只須要一次,後面就不用了),或者優化上面腳本,把jenkins打包好的文件複製到遠程服務器以前執行對應操做去處理這個目錄app