• profiles指定不同环境的配置
  • maven-assembly-plugin打发布压缩包
  • 分享shenniu_publish.sh程序启动工具
  • linux上使用shenniu_publish.sh启动程序

    profiles指定不同环境的配置

    通常一套程序分为了很多个部署环境:
    开发(dev),测试(uat),线上(prod) 等,我们要想对这些环境区分配置文件,可以通过两种方式:

    1、通过application.yml中编码指定

    通过指定profile.active=uat 方式指定开发环境

2、通过mvn中profiles来区分不同环境

通过mvn中profiles来区分不同环境对应的配置文件夹,人工可以手动在idea勾选生成不同环境的包(推荐)

建立不同的环境的文件

目录如图所示:
image.png

在pom文件之中设置环境

  1. <profiles>
  2. <profile>
  3. <id>node</id>
  4. <properties>
  5. <!--传递给脚本的参数值-->
  6. <activeProfile>node</activeProfile>
  7. <package-name>${scripts_packageName}</package-name>
  8. <boot-main>${scripts_bootMain}</boot-main>
  9. </properties>
  10. <activation>
  11. <activeByDefault>true</activeByDefault>
  12. </activation>
  13. </profile>
  14. <profile>
  15. <id>node1</id>
  16. <properties>
  17. <activeProfile>node1</activeProfile>
  18. <package-name>${scripts_packageName}</package-name>
  19. <boot-main>${scripts_bootMain}</boot-main>
  20. </properties>
  21. </profile>
  22. <profile>
  23. <id>node2</id>
  24. <properties>
  25. <activeProfile>node2</activeProfile>
  26. <package-name>${scripts_packageName}</package-name>
  27. <boot-main>${scripts_bootMain}</boot-main>
  28. </properties>
  29. </profile>
  30. </profiles>

节点:

  • id:指定不同环境配置文件所在目录
  • properties:该节点中的节点是可作为参数传递给其他配置文件,如我这里的package-name节点值就可以在另外的assembly.xml或者shell脚本文件中通过${package-name}获取到
  • activeByDefault:指定默认环境配置文件夹


maven-assembly-plugin打包

pom添加插件

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.6</version>
  5. <configuration>
  6. <archive>
  7. <addMavenDescriptor>false</addMavenDescriptor>
  8. <manifest>
  9. <addClasspath>true</addClasspath>
  10. <classpathPrefix>lib/</classpathPrefix>
  11. <mainClass>${scripts_bootMain}</mainClass>
  12. </manifest>
  13. </archive>
  14. <!--打包排除项-->
  15. <excludes>
  16. <exclude>**/*.yml</exclude>
  17. <exclude>**/*.properties</exclude>
  18. <exclude>**/*.xml</exclude>
  19. <exclude>**/*.sh</exclude>
  20. </excludes>
  21. </configuration>
  22. <executions>
  23. <execution>
  24. <id>make-a-jar</id>
  25. <phase>compile</phase>
  26. <goals>
  27. <goal>jar</goal>
  28. </goals>
  29. </execution>
  30. </executions>
  31. </plugin>

节点:

  • mainClass节点:用来指定启动main函数入口类路径,如这里的:com.sm.EurekaServerApplication
  • excludes节点:排除主jar包中配置等一些列后缀文件,因为我们要包这些配置文件放到主包外面
  • descriptor节点:用来指定assembly插件对应的assembly.xml配置文件

有了上面mvn配置,我们还需要assembly.xml的配置,这里提取了结合shell脚本发布程序的配置:

assembly.xml

  1. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd
  3. http://maven.apache.org/ASSEMBLY/2.0.0 ">
  4. <id>${activeProfile}</id>
  5. <!--打包成一个用于发布的zip文件-->
  6. <formats>
  7. <format>zip</format>
  8. </formats>
  9. <!--true:zip中生成一级目录(此处屏蔽,配合脚本需要profiles后缀)-->
  10. <includeBaseDirectory>false</includeBaseDirectory>
  11. <dependencySets>
  12. <dependencySet>
  13. <!--打包进zip文件的lib目录-->
  14. <useProjectArtifact>false</useProjectArtifact>
  15. <outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory>
  16. <unpack>false</unpack>
  17. </dependencySet>
  18. </dependencySets>
  19. <fileSets>
  20. <!-- 配置文件打包进zip文件的conf目录 -->
  21. <fileSet>
  22. <directory>${project.basedir}/src/main/profiles/${activeProfile}</directory>
  23. <outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory>
  24. <includes>
  25. <include>**/*</include>
  26. <!--<include>*.xml</include>-->
  27. <!--<include>*.properties</include>-->
  28. <!--<include>*.yml</include>-->
  29. </includes>
  30. </fileSet>
  31. <!--启动脚本打包进zip文件-->
  32. <fileSet>
  33. <directory>${project.basedir}/src/main/scripts</directory>
  34. <outputDirectory></outputDirectory>
  35. <includes>
  36. <include>**/*</include>
  37. </includes>
  38. <!-- 文件文件权限为777 -->
  39. <fileMode>777</fileMode>
  40. <!-- 目录权限为777 -->
  41. <directoryMode>777</directoryMode>
  42. <!--脚本中参数变量为pom中的值 关键-->
  43. <filtered>true</filtered>
  44. </fileSet>
  45. <!-- 项目编译出来的jar打包进zip文件 -->
  46. <fileSet>
  47. <directory>${project.build.directory}</directory>
  48. <outputDirectory>${package-name}-${activeProfile}/</outputDirectory>
  49. <includes>
  50. <include>*.jar</include>
  51. </includes>
  52. </fileSet>
  53. </fileSets>
  54. </assembly>

节点:

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

    分享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的publish packageName=”${package-name}” #命令启动包名 xx.jar的xx mainclass=”${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 “开始解压…”

  1. #解压命令
  2. unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath}
  3. #设置执行权限
  4. chmod +x ${baseDirPath}/${baseZipName}/${packageName}
  5. echo "解压完成。"
  6. fi

}

检测pid

function 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}无任何操作"
    ;;
esac

else echo “ command如下命令: unzip:解压并启动 start:启动 stop:停止进程 restart:重启

示例命令如:./shenniu_publish start
"

fi


<a name="25bc57fc"></a>
# 附录:
注意在Windows上边写的shell脚本可能语法上会出错<br />可以使用vim进行修复
```shell
1 vim shenniu_publish.sh
2 set ff=unix