小Hub领读:

很实用的shell脚本,值得珍藏和学习哈!


  • 作者:神牛003
  • https://www.cnblogs.com/wangr...

本篇和大家分享的是 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://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsdhttp://maven.apache.org/ASSEMBLY/2.0.0 ">    <id>${activeProfile}</id>    <!--打包成一个用于公布的zip文件-->    <formats>        <format>zip</format>    </formats>    <!--true:zip中生成一级目录(此处屏蔽,配合脚本须要profiles后缀)-->    <includeBaseDirectory>false</includeBaseDirectory>    <dependencySets>        <dependencySet>            <!--打包进zip文件的lib目录-->            <useProjectArtifact>false</useProjectArtifact>            <outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory>            <unpack>false</unpack>        </dependencySet>    </dependencySets>    <fileSets>        <!-- 配置文件打包进zip文件的conf目录 -->        <fileSet>            <directory>${project.basedir}/src/main/profiles/${activeProfile}</directory>            <outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory>            <includes>                <include>**/*</include>                <!--<include>*.xml</include>-->                <!--<include>*.properties</include>-->                <!--<include>*.yml</include>-->            </includes>        </fileSet>        <!--启动脚本打包进zip文件-->        <fileSet>            <directory>${project.basedir}/src/main/scripts</directory>            <outputDirectory></outputDirectory>            <includes>                <include>**/*</include>            </includes>            <!-- 文件文件权限为777 -->            <fileMode>777</fileMode>            <!-- 目录权限为777  -->            <directoryMode>777</directoryMode>            <!--脚本中参数变量为pom中的值 要害-->            <filtered>true</filtered>        </fileSet>        <!-- 我的项目编译进去的jar打包进zip文件 -->        <fileSet>            <directory>${project.build.directory}</directory>            <outputDirectory>${package-name}-${activeProfile}/</outputDirectory>            <includes>                <include>*.jar</include>            </includes>        </fileSet>    </fileSets></assembly>

重点节点介绍:

  • formats 节点:把配置文件和 jar 包等压缩成什么文件格式,这里能够有:zip,tar 等
  • fileMode 节点:指定 scripts 目录下脚本文件 (这里是:shenniu_publish.sh) 在 linux 上文件权限为 777
  • filtered 节点:脚本中参数变量为 pom 的 profiles 中 properties 的值 (该配置,是把 mvn 中属性值映射生成到 sh 文件中,如:${package-name})

实现下面配置后,此时咱们能够通过 idea 上勾选切换不同环境来打 zip 包,如图:

分享 shenniu_publish.sh 程序启动工具

下面步骤实现了 zip 格局的公布包,咱们再分享下启动程序的 shell 脚本,该脚本具备的性能如:

  • 解压 zip + 启动 jar 包
  • 启动 jar 包
  • 进行对应 jar 运行
  • 重启 jar 程序

目前该 shell 中封装了两种启动 jar 命令的形式:

  • java -cp
  • java -jar

如图命令格局:

来看全副的 shell 代码:

#!/usr/bin/env bash#可变参数变量languageType="javac" #反对 java,javac,netcore 公布#参数值由pom文件传递baseZipName="${package-name}-${activeProfile}" #压缩包名称  publish-test.zip的publishpackageName="${package-name}" #命令启动包名 xx.jar的xxmainclass="${boot-main}" #java -cp启动时,指定main入口类;命令:java -cp conf;lib\*.jar;${packageName}.jar ${mainclass}#例子# baseZipName="publish-test" #压缩包名称  publish-test.zip的publish# packageName="publish" #命令启动包名 publish.jar的xx#固定变量basePath=$(cd `dirname $0`/; pwd)baseZipPath="${basePath}/${baseZipName}.zip"  #压缩包门路baseDirPath="${basePath}" #解压部署磁盘门路pid= #过程pid#解压function shenniu_unzip(){    echo "解压---------------------------------------------"    echo "压缩包门路:${baseZipPath}"    if [ ! `find ${baseZipPath}` ]    then        echo "不存在压缩包:${baseZipPath}"    else        echo "解压磁盘门路:${baseDirPath}/${baseZipName}"        echo "开始解压..."        #解压命令        unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath}        #设置执行权限        chmod +x ${baseDirPath}/${baseZipName}/${packageName}        echo "解压实现。"      fi}#检测pidfunction getPid(){    echo "检测状态---------------------------------------------"    pid=`ps -ef | grep -n ${packageName} | grep -v grep | awk '{print $2}'`    if [ ${pid} ]     then        echo "运行pid:${pid}"    else        echo "未运行"    fi}#启动程序function start(){    #启动前,先进行之前的    stop    if [ ${pid} ]    then        echo "进行程序失败,无奈启动"    else        echo "启动程序---------------------------------------------"                #抉择语言类型        read -p "输出程序类型(java,javac,netcore),下一步按回车键(默认:${languageType}):" read_languageType        if [ ${read_languageType} ]        then            languageType=${read_languageType}        fi        echo "抉择程序类型:${languageType}"        #进入运行包目录        cd ${baseDirPath}/${baseZipName}        #分类启动        if [ "${languageType}" == "javac" ]         then            if [ ${mainclass} ]             then                nohup java -cp conf:lib\*.jar:${packageName}.jar ${mainclass} >${baseDirPath}/${packageName}.out 2>&1 &               #nohup java -cp conf:lib\*.jar:${packageName}.jar ${mainclass} >/dev/null 2>&1 &            fi        elif [ "${languageType}" == "java" ]         then            nohup java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar >/dev/null 2>&1 &            # java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar        elif [ "${languageType}" == "netcore" ]         then            #nohup dotnet run ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &            nohup ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &        fi        #查问是否有启动过程        getPid        if [ ${pid} ]        then            echo "已启动"            #nohup日志            tail -n 50 -f ${baseDirPath}/${packageName}.out        else            echo "启动失败"        fi    fi}#进行程序function stop(){    getPid    if [ ${pid} ]     then        echo "进行程序---------------------------------------------"        kill -9 ${pid}                getPid        if [ ${pid} ]         then            #stop            echo "进行失败"        else            echo "进行胜利"        fi    fi}#启动时带参数,依据参数执行if [ ${#} -ge 1 ] then    case ${1} in        "start")             start        ;;        "restart")             start        ;;        "stop")             stop        ;;        "unzip")             #执行解压            shenniu_unzip            #执行启动            start        ;;        *)             echo "${1}无任何操作"        ;;    esacelse    echo "    command如下命令:    unzip:解压并启动    start:启动    stop:进行过程    restart:重启    示例命令如:./shenniu_publish start    "fi

正如下面大节说的,shell 中的参数 package-name,activeProfile,boot-main 都是由 mvn 中 profiles 的 properties 中提供,是可变的参数,脚本代码自身不须要人工去批改,只须要变的是 mvn 的参数即可;其实在咱们生成 zip 包的时候,shell 中的参数就被替换了,能够看 zip 中 shell 文件内容如:

linux 上应用 shenniu_publish.sh 启动程序

把生成的 zip 上传到 linux 上,通过命令解压:

1 unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip

其实 shell 脚本中蕴含有解压命令,然而我在打包时放在了 zip 中,所以只能通过手动解压了,当然能够调整;此时进入加压目录如此:

注:这里第一次执行./shenniu_publish.sh 脚本时候,提醒了错误信息;是因为我是在 windows 上编辑的这个脚本,其空格等和 linux 上不一样,所以运行会有问题,要解决能够应用 vim 命令在 linux 把该文件转成 linux 格局,如下命令:

vim shenniu_publish.shset ff=unix:wq

执行完后,再来运行脚本./shenniu_publish.sh,此时有如下提醒:

此刻咱们文件是解压状态,因而只须要 start 命令启动程序即可:

到这里 shenniu_publish.sh 脚本应用就实现了,只有脚本没有提醒谬误,根本都能启动 jar 服务;其余 restart 和 stop 命令也如此执行就行:

能够去钻研下 shell 代码,心愿该脚本能给你带来效率和好的学习思路,上面是测试用例 git 地址,脚本在 eureka-server 我的项目中:https://github.com/shenniubuxing3/springcloud-Finchley.SR2

举荐浏览

太赞了,这个Java网站,什么我的项目都有!https://markerhub.com

这个B站的UP主,讲的java真不错!

太赞了!最新版Java编程思维能够在线看了!