Spring Boot 项目打包 + Shell 脚本部署实践,太有用了!
阅读本文大概需要 8.4 分钟。
profiles指定不同环境的配置 maven-assembly-plugin打发布压缩包 分享shenniu_publish.sh程序启动工具 linux上使用shenniu_publish.sh启动程序
# profiles指定不同环境的配置
通过application.yml中编码指定 profile.active=uat 方式指定 通过mvn中profiles来区分不同环境对应的配置文件夹,人工可以手动在idea勾选生成不同环境的包(推荐)
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <profiles></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <profile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <id>node</id></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <properties></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--传递给脚本的参数值--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <activeProfile>node</activeProfile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <package-name>${scripts_packageName}</package-name></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <boot-main>${scripts_bootMain}</boot-main></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </properties></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <activation></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <activeByDefault>true</activeByDefault></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </activation></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </profile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <profile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <id>node1</id></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <properties></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <activeProfile>node1</activeProfile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <package-name>${scripts_packageName}</package-name></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <boot-main>${scripts_bootMain}</boot-main></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </properties></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </profile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <profile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <id>node2</id></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <properties></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <activeProfile>node2</activeProfile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <package-name>${scripts_packageName}</package-name></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <boot-main>${scripts_bootMain}</boot-main></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </properties></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </profile></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </profiles></span>
id:用来指定不同环境配置文件所在的目录,如下我这里: properties:该节点中的节点是可作为参数传递给其他配置文件,如我这里的package-name节点值就可以在另外的assembly.xml或者shell脚本文件中通过${package-name}获取到,如下: activeByDefault:指定默认环境配置文件夹
# maven-assembly-plugin打发布压缩包
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <plugin></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <groupId>org.apache.maven.plugins</groupId></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <artifactId>maven-jar-plugin</artifactId></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <version>2.6</version></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <configuration></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <archive></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <addMavenDescriptor>false</addMavenDescriptor></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <manifest></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <addClasspath>true</addClasspath></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <classpathPrefix>lib/</classpathPrefix></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <mainClass>${scripts_bootMain}</mainClass></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </manifest></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </archive></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--打包排除项--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <excludes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <exclude>**/*.yml</exclude></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <exclude>**/*.properties</exclude></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <exclude>**/*.xml</exclude></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <exclude>**/*.sh</exclude></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </excludes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </configuration></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <executions></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <execution></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <id>make-a-jar</id></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <phase>compile</phase></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <goals></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <goal>jar</goal></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </goals></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </execution></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </executions></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </plugin></span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <plugin></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <groupId>org.apache.maven.plugins</groupId></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <artifactId>maven-assembly-plugin</artifactId></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <version>2.4</version></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!-- The configuration of the plugin --></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <configuration></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!-- Specifies the configuration file of the assembly plugin --></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <descriptors></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <descriptor>${project.basedir}/src/main/assembly/assembly.xml</descriptor></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </descriptors></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </configuration></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <executions></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <execution></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <id>make-assembly</id></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <phase>package</phase></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <goals></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <goal>single</goal></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </goals></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </execution></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </executions></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </plugin></span>
mainClass节点:用来指定启动main函数入口类路径,如这里的:com.sm.EurekaServerApplication excludes节点:排除主jar包中配置等一些列后缀文件,因为我们要包这些配置文件放到主包外面 descriptor节点:用来指定assembly插件对应的assembly.xml配置文件
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> http://maven.apache.org/ASSEMBLY/2.0.0 "</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <id>${activeProfile}</id></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--打包成一个用于发布的zip文件--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <formats></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <format>zip</format></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </formats></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--true:zip中生成一级目录(此处屏蔽,配合脚本需要profiles后缀)--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <includeBaseDirectory>false</includeBaseDirectory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <dependencySets></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <dependencySet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--打包进zip文件的lib目录--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <useProjectArtifact>false</useProjectArtifact></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <unpack>false</unpack></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </dependencySet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </dependencySets></span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <fileSets></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!-- 配置文件打包进zip文件的conf目录 --></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <fileSet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <directory>${project.basedir}/src/main/profiles/${activeProfile}</directory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <includes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <include>**/*</include></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--<include>*.xml</include>--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--<include>*.properties</include>--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--<include>*.yml</include>--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </includes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </fileSet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--启动脚本打包进zip文件--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <fileSet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <directory>${project.basedir}/src/main/scripts</directory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <outputDirectory></outputDirectory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <includes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <include>**/*</include></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </includes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!-- 文件文件权限为777 --></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <fileMode>777</fileMode></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!-- 目录权限为777 --></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <directoryMode>777</directoryMode></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!--脚本中参数变量为pom中的值 关键--></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <filtered>true</filtered></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </fileSet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <!-- 项目编译出来的jar打包进zip文件 --></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <fileSet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <directory>${project.build.directory}</directory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <outputDirectory>${package-name}-${activeProfile}/</outputDirectory></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <includes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> <include>*.jar</include></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </includes></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </fileSet></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </fileSets></span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> </assembly></span>
formats节点:把配置文件和jar包等压缩成什么文件格式,这里可以有:zip,tar等 fileMode节点:指定scripts目录下脚本文件(这里是:shenniu_publish.sh)在linux上文件权限为777 filtered节点:脚本中参数变量为pom的profiles中properties的值(该配置,是把mvn中属性值映射生成到sh文件中,如:${package-name})
# 分享shenniu_publish.sh程序启动工具
解压zip+启动jar包 启动jar包 停止对应jar运行 重启jar程序
java -cp java -jar
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #!/usr/bin/env bash</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #可变参数变量</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> languageType="javac" #支持 java,javac,netcore 发布</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #参数值由pom文件传递</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> baseZipName="${package-name}-${activeProfile}" #压缩包名称 publish-test.zip的publish</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> packageName="${package-name}" #命令启动包名 xx.jar的xx</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> mainclass="${boot-main}" #java -cp启动时,指定main入口类;命令:java -cp conf;lib*.jar;${packageName}.jar ${mainclass}</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #例子</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> # baseZipName="publish-test" #压缩包名称 publish-test.zip的publish</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> # packageName="publish" #命令启动包名 publish.jar的xx</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #固定变量</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> basePath=$(cd `dirname $0`/; pwd)</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> baseZipPath="${basePath}/${baseZipName}.zip" #压缩包路径</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> baseDirPath="${basePath}" #解压部署磁盘路径</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> pid= #进程pid</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #解压</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> function shenniu_unzip()</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> {</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "解压---------------------------------------------"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "压缩包路径:${baseZipPath}"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ! `find ${baseZipPath}` ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "不存在压缩包:${baseZipPath}"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> else</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "解压磁盘路径:${baseDirPath}/${baseZipName}"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "开始解压..."</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #解压命令</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath}</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #设置执行权限</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> chmod +x ${baseDirPath}/${baseZipName}/${packageName}</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "解压完成。"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> }</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #检测pid</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> function getPid()</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> {</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "检测状态---------------------------------------------"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> pid=`ps -ef | grep -n ${packageName} | grep -v grep | awk '{print $2}'`</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${pid} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "运行pid:${pid}"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> else</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "未运行"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> }</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #启动程序</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> function start()</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> {</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #启动前,先停止之前的</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> stop</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${pid} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "停止程序失败,无法启动"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> else</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "启动程序---------------------------------------------"</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #选择语言类型</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> read -p "输入程序类型(java,javac,netcore),下一步按回车键(默认:${languageType}):" read_languageType</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${read_languageType} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> languageType=${read_languageType}</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "选择程序类型:${languageType}"</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #进入运行包目录</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> cd ${baseDirPath}/${baseZipName}</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #分类启动</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ "${languageType}" == "javac" ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${mainclass} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nohup java -cp conf:lib*.jar:${packageName}.jar ${mainclass} >${baseDirPath}/${packageName}.out 2>&1 &</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #nohup java -cp conf:lib*.jar:${packageName}.jar ${mainclass} >/dev/null 2>&1 &</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> elif [ "${languageType}" == "java" ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nohup java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar >/dev/null 2>&1 &</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> # java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> elif [ "${languageType}" == "netcore" ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #nohup dotnet run ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> nohup ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #查询是否有启动进程</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> getPid</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${pid} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "已启动"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #nohup日志 tail -n 50 -f ${baseDirPath}/${packageName}.out</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> else</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "启动失败"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> }</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #停止程序</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> function stop()</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> {</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> getPid</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${pid} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "停止程序---------------------------------------------"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> kill -9 ${pid}</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> getPid</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${pid} ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #stop</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "停止失败"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> else</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "停止成功"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> }</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #启动时带参数,根据参数执行</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> if [ ${#} -ge 1 ]</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> then</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> case ${1} in</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> "start")</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> start</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> ;;</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> "restart")</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> start</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> ;;</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> "stop")</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> stop</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> ;;</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> "unzip")</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #执行解压</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> shenniu_unzip</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> #执行启动</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> start</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> ;;</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> *)</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "${1}无任何操作"</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> ;;</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> esac</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> else</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> echo "</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> command如下命令:</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> unzip:解压并启动</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> start:启动</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> stop:停止进程</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> restart:重启</span>
<span class="code-snippet_outer">
</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> 示例命令如:./shenniu_publish start</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> "</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> fi</span>
# linux上使用shenniu_publish.sh启动程序
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> vim shenniu_publish.sh</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> set ff=unix</span>
<span class="code-snippet_outer" style="max-width: 1000%; box-sizing: border-box !important; overflow-wrap: break-word !important;"> :wq</span>
推荐阅读:
贼好用的Java工具类库,GitHub星标10k+,你在用吗?
微信扫描二维码,关注我的公众号
朕已阅
文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/1254.html