springboot项目打包,使用maven-assembly插件把依赖分开打包

引言

以往我们打包项目都是把整个项目打成一个jar包,在往服务器上更新项目的时候,哪怕只修改了很少一部分代码,也需要把全部的依赖包都重新上传一遍,如果服务器带宽太低,就会很花时间。使用这个插件可以把自己写的代码和第三方依赖分开打包,这样在更新代码的时候,只需要上传自己的代码即可。

以往项目打包后:

 用assembly打包后:

这样,在更新服务器部署时,没有变动的依赖就不需要在重新上传。下面我们开始

 1 配置打包插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                 <!-- 下面配置是为了方便修改配置,不在jar包中包含配置文件。 -->
                    <excludes>
                        <exclude>application*.yml</exclude>
                        <exclude>log4j2.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <!--  下面是指定了插件的配置文件,用来说明哪些内容需要打包 -->
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-my-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>

2 写配置文件

配置文件位置:/src/main/assembly/assembly.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>1.0</id>
<!-- 打包为zip -->
    <formats>
        <format>zip</format>
    </formats>
<!-- 文件集合 -->
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <!-- 包含自己写的启停脚本 -->
            <directory>${project.basedir}/src/main/script</directory>
            <outputDirectory>script</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/target/classes</directory>
              <!-- 包含自己的配置文件 -->
            <includes>
                <include>*.yml</include>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
            <outputDirectory>config</outputDirectory>
        </fileSet>
    </fileSets>
<!-- 依赖集合 -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>runtime</scope>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>system</scope>
        </dependencySet>
    </dependencySets>
</assembly>

3 启动/停止脚本

这部分可以自己写,这里给出demo。需要注意的是:如果使用以下脚本,项目部署的路径中不要包含“java”字符,否则停止脚本无法正常工作。

脚本写好后,放在项目的 src/main/script 目录下

1. 启动脚本

#!/bin/sh
cd `dirname $0`
echo `basename $0` is at `pwd`

export _HOME=..
CONFIG_DIR=$_HOME/config

RESOLVED_CONFIG_DIR=`cd "$CONFIG_DIR"; pwd`
export CLASSPATH=$RESOLVED_CONFIG_DIR

for i in `ls $_HOME/lib/*.jar`; do
        CLASSPATH=$i:$CLASSPATH
done
nohup java -classpath $CLASSPATH -Xms512m -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9081  com.my.App $* 2>&1>/dev/null &
echo "start success

2. 停止脚本

#! /bin/sh
cd `dirname $0`
echo `basename $0` is at `pwd`

data_analyzer_path=`cd ..; pwd`
echo "Using data_analyzer_path: $data_analyzer_path"
pid=`ps -ef|grep java|grep $data_analyzer_path |awk '{print $2}'`
echo "pid=[$pid]" 
while [  -n "$pid" ]; do
    echo "kill $pid"
    kill -9 $pid
    sleep 2s
    pid=`ps -ef|grep java|grep $data_analyzer_path |awk '{print $2}'`
done
echo "shutdown ok"

4 打包

正常使用maven打包命令即可。

打包后在target目录得到zip文件:

解压这个zip,得到 :

其中config里是springboot配置文件和日志配置文件,lib中是所有的依赖包,script是启动停止脚本。

5 部署

如果是第一次部署,我们需要上传整个zip包,然后使用script里面的脚本启停项目。

如果是更新部署,只需要更新lib文件夹中有变化的jar包即可。

例如某次我只更新了项目中common模块的代码:

 只需要把压缩包中的 lib/common-0.0.1.jar 这个jar上传替换到服务器上的lib目录内,再重启项目。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值