24小时在线平台

当前位置: 24小时在线平台 > 部署教程大全 > 文章页

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

时间:2025-05-11 07:54来源: 作者:admin 点击: 19 次
文章浏览阅读7.9k次,点赞10次,收藏48次。本文介绍如何通过Maven配置将项目依赖的jar包与代码分离,以加速部署过程。使用spring-boot-maven-plugin和maven-dependency-plugin插件,可以实现打包时去除第三方依赖,并将依赖复制到指定目录。同时,提供了在

有时候我们的项目依赖的jar包比较多,项目会很大,那我们每次部署更新的时候上传都会很慢,其实我们只是想更新里面的代码而已,而那众多的依赖包并不想重新上传,这时候我们就需要将依赖包和项目代码分离开来了

这是我分离之后target目录

其中lib下都是项目依赖的jar包,原本我的plat-admin.jar有70多M,现在只有1M多了

下面是pom.xml的plugin配置

<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> <include> <groupId>com.text.aaa</groupId> <artifactId>${project.parent.artifactId}-core</artifactId> </include> <include> <groupId>com.text.aaa</groupId> <artifactId>common</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/lib是依赖jar包的输出目录,根据自己喜好配置--> <outputDirectory>target/lib/${project.parent.artifactId}</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>runtime</includeScope> <!-- 不拷贝的依赖 --> <excludeArtifactIds>common,${project.parent.artifactId}-core</excludeArtifactIds> </configuration> </execution> </executions> </plugin> </plugins> </build>

pom.xml中加上上面的配置那么打包出来的项目代码和依赖的jar包就会分离开来了

linux运行项目的jar包就不是之前的:  nohup java -jar xxx.jar &   了

需要指定lib目录: nohup  java -Dloader.path=lib包的路径,resources,lib -Dfile.encoding=utf-8 -jar xxx.jar &

最后再补一个启动这种jar包的shell脚本

#! /bin/bash #jar文件路径(自定义配置) CURRENT_PATH="/usr/local/servers/" #lib依赖路径(自定义配置) LIB_PATH="/usr/local/servers/lib/" #日志路径(自定义配置) LOG_PATH="/usr/local/servers/logs/" #服务名称(自定义配置) SERVER_NAMES=("serverName1" "serverName2") #运行环境(自定义配置) SERVER_ENVIRONMENT="dev" #堆栈内存配置 JAR_MEMORY="-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms512m -Xmx512m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseG1GC" #外部配置文件 #OUT_YML_PATH=${CURRENT_PATH}application-${2}-${SERVER_ENVIRONMENT} OUT_YML_PATH=${CURRENT_PATH}application-${SERVER_ENVIRONMENT} COMMANDS=("start" "stop" "restart" "status") JAR="" PID="" START_TIME="" RUN_TIME="" #判断第一个参数是否正确 if [[ ! " ${COMMANDS[@]} " =~ " ${1} " ]] || [ -z "${1}" ]; then echo -e "指令错误: ./outyml_servers.sh [ ${COMMANDS[@]} ] ${2} \n" >&2 exit 1 fi #判断第二个参数是否正确 if [[ ! " ${SERVER_NAMES[@]} " =~ " ${2} " ]] || [ -z "${2}" ]; then echo -e "服务名称错误: ./servers.sh ${1} [ ${SERVER_NAMES[@]} ] \n" >&2 exit 1 fi #根据不同的服务定义项目路径和日志路径,判断第二个参数是否正确 if [[ "${SERVER_NAMES[@]}" =~ "${2}" ]] && [ ${2} ]; then JAR=${2} #服务名称为grid需要单独判断 if [ "${2}" = "grid" ]; then JAR="grid-web" fi CURRENT_PATH=${CURRENT_PATH} #LOG_PATH=${LOG_PATH}${2}.log JAR=$(find $CURRENT_PATH -maxdepth 1 -name "*${JAR}*.jar") PID=$(ps -ef | grep $JAR | grep -v grep | awk '{ print $2 }') elif [ "status" = "${1}" ] && [ "all" = "${2}" ]; then # 遍历所有服务获取状态 for SERVER_NAME in ${SERVER_NAMES[@]} do ${0} ${1} ${SERVER_NAME} done exit 1 else echo -e "Usage: ./servers.sh ${1} [ ${SERVER_NAMES[@]} ] \n" >&2 exit 1 fi #指定的服务扩大堆栈内存 if [[ "("serverName1")" =~ "${2}" ]]; then JAR_MEMORY="-XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=1024m -Xms1024m -Xmx1024m -Xmn1024m -Xss256k -XX:SurvivorRatio=8 -XX:+UseG1GC" fi #打印进程详细信息 function printStatus() { START_TIME=$(ps -eo pid,lstart,etime | grep -v 'grep' | grep $PID | awk '{ print $2,$3,$4,$5,$6 }') RUN_TIME=$(ps -eo pid,lstart,etime | grep -v 'grep' | grep $PID | awk '{ print $7 }') echo -e " 正在运行中:$JAR \n 进程ID: $PID \n 进程开始时间: $START_TIME \n 已运行: $RUN_TIME" echo -e " 运行环境: $SERVER_ENVIRONMENT \n" } case "${1}" in "start") if [ ! -z "$PID" ]; then echo "$JAR 已经启动,进程号: $PID " else echo -e "启动 $JAR ... \n" cd $CURRENT_PATH nohup java -Dloader.path=$LIB_PATH${2} -Dfile.encodiing=UTF-8 $JAR_MEMORY -jar $JAR -Dconfig=${OUT_YML_PATH} --spring.profiles.active=$SERVER_ENVIRONMENT >$LOG_PATH${2}.log 2>&1 & if [ "$?" = "0" ]; then echo -e "$JAR 启动完成,请查看日志确保成功 \n" else echo -e "$JAR 启动失败 \n" fi fi ;; "stop") if [ -z "$PID" ]; then echo -e "$JAR 没有在运行,无需关闭 \n" else echo -e "关闭 $JAR ..." kill -9 $PID if [ "$?" = "0" ]; then echo -e "服务已关闭 \n" else echo -e "服务关闭失败 \n" fi fi ;; "restart") ${0} stop ${2} ${0} start ${2} ;; "status") if [ ! -z "$PID" ]; then printStatus else echo -e "$JAR 未在运行 \n" fi ;; *) echo -e "Usage: ./springboot.sh {start|stop|restart|status} [ ${SERVER_NAMES[@]} ] \n" >&2 exit 1 esac 2023-08-10补充更新,新增打包时将lib和业务jar包移动到指定的目录,然后执行clean时删除指定的文件 <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> <include> <groupId>com.text.aaa</groupId> <artifactId>${project.parent.artifactId}-core</artifactId> </include> <include> <groupId>com.text.aaa</groupId> <artifactId>common</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/lib是依赖jar包的输出目录,根据自己喜好配置--> <outputDirectory>../../A-deploy/package/lib/${project.parent.artifactId}</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>runtime</includeScope> <!-- 不拷贝的依赖 --> <excludeArtifactIds>common,${project.parent.artifactId}-core</excludeArtifactIds> </configuration> </execution> <!-- 添加额外的 execution 配置来移动 JAR 文件 --> <execution> <id>move-jar</id> <phase>package</phase> <goals> <goal>copy</goal> </goals> <configuration> <!-- 指定要移动的 JAR 文件 --> <artifactItems> <artifactItem> <groupId>${project.groupId}</groupId> <artifactId>${project.artifactId}</artifactId> <version>${project.version}</version> <type>jar</type> <overWrite>true</overWrite> <outputDirectory>../../A-deploy/package/jars/</outputDirectory> <destFileName>${project.artifactId}.jar</destFileName> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <configuration> <filesets> <fileset> <!-- 指定执行clean要操作的目录 --> <directory>../../A-deploy/package/lib/${project.parent.artifactId}</directory> <includes> <!-- 目录下要删除的数据,**/*=删除目录下的文件,不删除目录,**=删除整个目录 --> <include>**/*</include> </includes> <followSymlinks>false</followSymlinks> </fileset> <!-- 删除业务代码jar包 --> <fileset> <directory>../../A-deploy/package/jars/</directory> <includes> <include>${project.artifactId}.jar</include> </includes> <followSymlinks>false</followSymlinks> </fileset> </filesets> </configuration> </plugin> </plugins> </build>

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-06-26 19:06 最后登录:2025-06-26 19:06
栏目列表
推荐内容