前言
本篇和大家分享的是springboot打包并结合shell脚本命令部署,重点在分享一个shell程序启动工具,希望能便利工作;
- profiles指定不同环境的配置
- maven-assembly-plugin打发布压缩包
- 分享shenniu_publish.sh程序启动工具
- linux上使用shenniu_publish.sh启动程序
profiles指定不同环境的配置
通常一套程序分为了很多个部署环境:开发,测试,uat,线上 等,我们要想对这些环境区分配置文件,可以通过两种方式:
- 通过application.yml中编码指定 profile.active=uat 方式指定
- 通过mvn中profiles来区分不同环境对应的配置文件夹,人工可以手动在idea勾选生成不同环境的包(推荐)
这里我们要讲的是第二种,首先在mvn中配置如下内容:
<profiles> <profile> <id>node</id> <properties> <!--传递给脚本的参数值--> <activeProfile>node</activeProfile> <package-name>${scripts_packageName}</package-name> <boot-main>${scripts_bootMain}</boot-main> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>node1</id> <properties> <activeProfile>node1</activeProfile> <package-name>${scripts_packageName}</package-name> <boot-main>${scripts_bootMain}</boot-main> </properties> </profile> <profile> <id>node2</id> <properties> <activeProfile>node2</activeProfile> <package-name>${scripts_packageName}</package-name> <boot-main>${scripts_bootMain}</boot-main> </properties> </profile> </profiles>节点粗解:
id:用来指定不同环境配置文件所在的目录,如下我这里:
properties:
该节点中的节点是可作为参数传递给其他配置文件,如我这里的package-name节点值就可以在另外的assembly.xml或者shell脚本文件中通过${package-name}获取到,如下:
activeByDefault:
指定默认环境配置文件夹
maven-assembly-plugin打发布压缩包
对于springboot程序打包,可以分为jar和war,这里是jar包;有场景是咋们配置文件或者第三方等依赖包不想放到工程jar中,并且把这些文件压缩成一个zip包,方便上传到linux;此时通过maven-assembly-plugin和maven-jar-plugin就可以做到,mvn的配置如:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>${scripts_bootMain}</mainClass> </manifest> </archive> <!--打包排除项--> <excludes> <exclude>**/*.yml</exclude> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> <exclude>**/*.sh</exclude> </excludes> </configuration> <executions> <execution> <id>make-a-jar</id> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <!-- The configuration of the plugin --> <configuration> <!-- Specifies the configuration file of the assembly plugin --> <descriptors> <descriptor>${project.basedir}/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>值得注意的地方如下几点:
- mainClass节点:用来指定启动main函数入口类路径,如这里的:com.sm.EurekaServerApplication
- excludes节点:排除主jar包中配置等一些列后缀文件,因为我们要包这些配置文件放到主包外面
- descriptor节点:用来指定assembly插件对应的assembly.xml配置文件
有了上面mvn配置,我们还需要assembly.xml的配置,这里提取了结合shell脚本发布程序的配置:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http:///shenniubuxing3/springcloud-Finchley.SR2(本地下载)总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。