SpringBoot 之assembly自定义打包

版权声明:望支持~~~ https://blog.csdn.net/u011663149/article/details/86133473

SpringBoot项目使用assembly.xml进行打包部署

前言:

     对通常的springboot项目,一般我们都是采用的jar的方式进行启动配置相应的参数。但是对于一些web以及配置型项目操作不方便。

     这里我们适用assembly以及自定义脚本进行项目部署:

 assembly.xml:

<assembly xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/assembly-1.0.0.xsd">
    <id>package</id>
	<!--打包方式-->
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
	<!--扫描文件的列表-->   
   <fileSets>
		<!--springboot基础配置-->
        <fileSet>  
            <includes>  
                <include>*.properties</include>  
                <include>**/*.xml</include>  
            </includes>
            <excludes>
            	<exclude>application.properties</exclude>
            	<exclude>config/*.properties</exclude>
            </excludes>
            <outputDirectory>${file.separator}resources</outputDirectory>  
            <directory>src/main/resources</directory>   
        </fileSet> 
        <fileSet>  
            <includes>  
                <include>application.properties</include>  
            	<include>config/*.properties</include>
            </includes>
            <outputDirectory>${file.separator}resources</outputDirectory>  
            <directory>src/${env}/resources</directory>   
        </fileSet>  
		<!--启动脚本配置(需要执行dos2unix)-->
        <fileSet>  
            <outputDirectory>${file.separator}</outputDirectory>  
            <directory>src/script</directory>  
        </fileSet>  
		<!--lib-->
        <fileSet>  
            <outputDirectory>${file.separator}lib</outputDirectory>  
            <directory>lib</directory>  
        </fileSet>  
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

需要在我们的maven项目中加入plugin:

           <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
					<!--打包名称-->
                    <finalName>projectName</finalName>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
                </configuration>
            </plugin>

启动执行的script脚本:

bak.sh

tar -cvf ../projectName/projectName.tar`date +%Y%m%d%H%M%s` *

ps.sh --查看进程

APP_HOME=`pwd`
echo "pid:`cat app.pid`"
ps -ef|grep $APP_HOME

run.sh --启动

/*springboot main方法启动类*/
APP_MAINCLASS=com.projectName.Application

APP_HOME=`pwd`

CLASSPATH=$APP_HOME/resources
for i in "$APP_HOME"/lib/*.jar; do
  CLASSPATH="$CLASSPATH":"$i"
done
java -Djava.awt.headless=true  -classpath $CLASSPATH $APP_MAINCLASS $* > /data/Logs/projectName/app.log &

echo $! > app.pid

stop.sh --停止进程

kill `cat app.pid`

tail.sh --查看正在运行的日志

APP_HOME=/data/Logs/projectName
if [ -n "$1" ]
then
  tail -f $APP_HOME/$1.log
else
  tail -f $APP_HOME/app.log
fi

以上是整个打包操作的相关的配置。对打包的tar解压执行sh脚本时可能会出现脚本无法执行需要使用dos2uninx进行转换。

如果未安装dos2unix会出现

[root@localhost dhing]# dos2unix 
bash: dos2unix: 未找到命令… 

解决方案:

[root@localhost user]# yum install dos2unix 

如此就是项目自定义打包的相关配置~~~~


猜你喜欢

转载自blog.csdn.net/u011663149/article/details/86133473