maven项目打包将依赖的jar包与项目代码分离

博客针对Spring Boot项目依赖大量三方jar包,整体打包修改代码后上传耗时的问题,提出将依赖的三方jar包与自己开发的代码分开打包的方案。介绍了具体步骤为修改pom.xml文件的build部分,还给出了启动方式和包含所有包、关闭测试的打包构建等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.背景

很多时候我们都是使用springboot项目,依赖大量的三方jar包,但是我们自己写代码却很少

如果整体打包,我们每次修改代码后都要整体上传,非充耗费时间

因此我们希望将依赖的三方jar包与我们自己开发写的代码分开打包,

这样我们修改了代码,只需要上传自己写的很小的包这样会快很多....

2.具体步骤

修改pom.xml文件的bulid部分即可

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>

            <plugin>
                <!--打包时去除第三方依赖-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>non-exists</groupId>
                            <artifactId>non-exists</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <!--拷贝第三方依赖文件到指定目录-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!--target/libs是依赖jar包的输出目录,根据自己喜好配置-->
                            <outputDirectory>target/libs</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3.打包后的结果

4.启动方式

nohup  java -Dloader.path=./libs -jar XXXXX.jar &

5.启动脚本

#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME=XXXX-api-1.0-SNAPSHOT.jar
#使用说明,用来提示输入参数
usage() {
 echo "Usage: sh 脚本名.sh [start|stop|restart|status]"
 exit 1
}
  
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0     
if [ -z "${pid}" ]; then
return 1
else
 return 0
fi
}

#启动方法
start(){
is_exist
if [ $? -eq "0" ]; then
 echo "${APP_NAME} is already running. pid=${pid} ."
else
 nohup java -Dloader.path=./libs -jar $APP_NAME -config.spring=./config/application.yml >/dev/null 2>&1 &
 echo "nohup java -Dloader.path=./lib -jar $APP_NAME -config.spring=./config/application.yml >/dev/null 2>&1 &"
 echo "${APP_NAME} start success"
fi
}

#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
 kill $pid
else
 echo "${APP_NAME} is not running"
fi  
}

#输出运行状态
status(){
is_exist
if [ $? -eq "0" ]; then
 echo "${APP_NAME} is running. Pid is ${pid}"
else
 echo "${APP_NAME} is NOT running."
fi
}

#重启
restart(){
stop
echo "restart ....."
sleep 5
start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
 start
 ;;
"stop")
 stop
 ;;
"status")
 status
 ;;
"restart")
 restart
 ;;
*)
 usage
 ;;
esac

7.其他

1.包含所有包,关闭测试的打包构建

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <mainClass>com.wayn.MobileApplication</mainClass>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skipTests>true</skipTests>    <!--默认关掉单元测试 -->
                </configuration>
            </plugin>
        </plugins>
    </build>

6.完美

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值