spring boot 项目搭建就不介绍了,本篇侧重介绍打包部署方面。
〇、首先献上项目的resources截图
一、上面的三个文件内容
- assembly.xml
- start.sh
- stop.sh
内容分别是
assembly.xml
<assembly
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>application.yml</include>
<include>start.sh</include>
<include>stop.sh</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<!-- 将scope为runtime的依赖包打包到lib目录下。 -->
<!--runtime 包括依赖。packege 只是程序本身-->
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<!--设置程序依赖文件打包后位置-->
<outputDirectory>/</outputDirectory>
<!-- 将程序入口放到outputDirectory目录下 -->
<scope>packege</scope>
</dependencySet>
</dependencySets>
</assembly>
start.sh
pid=`ps -ef | grep tan-0.0.1-SNAPSHOT.jar| grep -v grep |awk '{print $2}'`
if [ $pid ]; then
echo :App is running pid=$pid
kill -9 $pid
fi
nohup java -jar lib/tan-0.0.1-SNAPSHOT.jar > logs/tan.log & tail -f logs/tan.log
注意:上面的 tan-0.0.1-SNAPSHOT.jar 是Sring boot 的main 所在的jar包 (stop文件同理)
stop.sh
pid=`ps -ef | grep tan-0.0.1-SNAPSHOT.jar | grep -v grep |awk '{print $2}'`
if [ $pid ]; then
kill -9 $pid
echo pid=$pid[tan-0.0.1-SNAPSHOT.jar] is stop
echo
else
echo tan is not running!
echo
fi
二、pom文件加上相关打包插件及配置
<build>
<plugins>
...
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions> <!--执行器 mvn assembly:assembly -->
<execution>
<id>make-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<descriptors> <!--描述文件路径 -->
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>./</classpathPrefix><!-- 这里会影响主程序里的MANIFEST.MF 里面的依赖jar路径,需要配好,不然启动不了-->
<mainClass>com.xx.zz.TanApplication</mainClass> <!-- spring boot main函数包路径-->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
...
<plugins>
</build>
三、这样运行打包之后在工程的target目录下就回得到 zip 的压缩包 内容如下:
四、复制该zip到Linux制定目录,解压,进如该目录执行
chmod 777 *.sh
然后执行 启动 和 停止服务即可
./start.sh
./stop.sh
谢谢阅读 ^_^