maven外掛assembly使用及springboot啟動指令碼start.sh和停止指令碼 stop.sh
阿新 • • 發佈:2020-08-24
我們在專案中都會遇到專案打包,可以通過assembly對我們的專案進行打包。
1、首先看一下在打包前的專案檔案結構。
2、在pom.xml中配置assembly外掛
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <!-- 繫結到package生命週期階段上 --> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
3、在assembly這個目錄下新增assembly.xml這個檔案
<assembly> <id>assembly</id> <formats> <!--打包的檔案型別--> <format>tar.gz</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/assembly/bin</directory> <outputDirectory>/bin</outputDirectory> <includes> <include>*.sh</include> </includes> <!--打包檔案許可權--> <fileMode>0755</fileMode> </fileSet> <fileSet> <!--原檔案目錄--> <directory>src/main/resources</directory> <!--打包的目錄--> <outputDirectory>/conf</outputDirectory> </fileSet> <!--將專案檔案打包為jar檔案--> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>/lib</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <!--新增 jar的打包--> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
4、點選 assembly:assembly 就能進行打包。
5、檢視打好包的檔案
5、附上start.sh,stop.sh檔案
start.sh檔案
#!/usr/bin/env bash #source $(dirname $0)/../../env.sh SERVERJAR="database-project-0.0.1-SNAPSHOT.jar" base_dir=$(dirname $0) cd .. if [ "$JAVA_HOME" != "" ]; then JAVA="$JAVA_HOME/bin/java" else JAVA=java fi JAVA_OPTS="-server -Xms32m -Xmx32m -Xmn24m -Xss256K \ -XX:SurvivorRatio=4 -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection \ -XX:CMSInitiatingOccupancyFraction=60 -XX:+PrintGCDateStamps \ -XX:+PrintGCDetails -Xloggc:$base_dir/gc.log" echo -n "Starting server ..." PID=$(ps -ef | grep database-project-0.0.1-SNAPSHOT.jar | grep -v grep |awk '{print $2}') if [ -z "$PID" ]; then echo Application is already stopped else echo kill $PID kill -9 $PID fi echo `pwd` echo $SERVERJAR echo $JAVA echo $JAVA_OPTS echo $JAVA_DEBUG_OPT nohup $JAVA $JAVA_OPTS $JAVA_DEBUG_OPT -jar lib/$SERVERJAR > $base_dir/nohup.out & if [ $? -eq 0 ];then # echo -n $! > "$PIDFILE" if [ $? -eq 0 ] then sleep 1 echo STARTED else echo FAILED TO WRITE PID exit 1 fi else echo SERVER DID NOT START exit 1 fi
stop.sh
#!/usr/bin/env bash SERVERJAR="database-project-0.0.1-SNAPSHOT.jar" base_dir=$(dirname $0) echo -n "Stopping server ..." PID=$(ps -ef | grep database-project-0.0.1-SNAPSHOT.jar | grep -v grep |awk '{print $2}') if [ -z "$PID" ]; then echo Application is already stopped else echo kill $PID kill -9 $PID fi exit 0
檔案解壓執行start.sh檔案
指令碼意思可以參考
https://www.jb51.net/article/39506.htm
日誌可以參考
https://www.jb51.net/article/152599.htm
啟動執行引數可以參考
https://www.jb51.net/article/161958.htm
https://www.jb51.net/article/107058.htm
在啟動指令碼的的時候可能會 ./start.sh 指令碼的時候可能會報錯
No such file or directory
這是由於在windows下編寫的指令碼檔案,放到Linux中無法識別格式,編輯start.sh和stop.sh指令碼檔案,set ff=unix 設定為linux下環境
set ff=unix
檢視指令碼啟動情況
tail -f -n 500 nohup.out
總結
到此這篇關於maven外掛assembly使用及springboot啟動指令碼start.sh和停止指令碼 stop.sh的文章就介紹到這了,更多相關maven外掛assembly使用springboot啟動停止指令碼內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!