本文讲诉如何把jar里的lib依赖包独立出来,方便更新服务时,缩小jar的体积,下面以若依的system服务为例,配置中的路径请酌情修改,主要提供大致配置逻辑
第一步:修改项目的pom.xml,调整build的配置
XML
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!--这是默认的打包方式-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- <version>${spring-boot.version}</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>repackage</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- 以下是分离lib的打包方式 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<layout>ZIP</layout>
<!--解决windows命令行窗口中文乱码-->
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
<!--这里是填写需要包含进去的jar,必须项目中的某些模块,会经常变动,那么就应该将其坐标写进来如果没有则nothing ,表示不打包依赖 -->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!--拷贝依赖到jar外面的lib目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--指定的依赖路径-->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!--指定启动类-->
<mainClass>org.dromara.system.RuoYiSystemApplication</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
打包后会得到分离的jar和lib
打开jar里的MANIFEST.MF,可看到指定了依赖包的路径
第二步:构建dockerfile,不同的就是复制lib目录到镜像中
bash
# 贝尔实验室 Spring 官方推荐镜像 JDK下载地址 https://bell-sw.com/pages/downloads/
FROM bellsoft/liberica-openjdk-debian:17.0.11-cds
#FROM bellsoft/liberica-openjdk-debian:21.0.3-cds
#FROM findepi/graalvm:java17-native
LABEL maintainer="Gavino"
RUN mkdir -p /gavino/system/logs \
/gavino/system/temp \
/gavino/skywalking/agent
WORKDIR /gavino/system
ENV SERVER_PORT=9201 LANG=C.UTF-8 LC_ALL=C.UTF-8 JAVA_OPTS=""
EXPOSE ${SERVER_PORT}
#复制依赖 jar
COPY ${OUT_DIRECTORY}/lib ./lib
ADD ruoyi-system.jar ./app.jar
ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -Dserver.port=${SERVER_PORT} \
#-Dskywalking.agent.service_name=ruoyi-system \
#-javaagent:/gavino/skywalking/agent/skywalking-agent.jar \
-XX:+HeapDumpOnOutOfMemoryError -XX:+UseZGC ${JAVA_OPTS} \
-jar app.jar
项目运行时,会根据MANIFEST.MF中配置的路径加载依赖包,所以只需要复制lib到相应目录
第三步:编写docker-compose,注意dockerfile的路径以及网络配置,以下是我的配置,酌情修改
bash
version: '3'
services:
ruoyi-system:
container_name:ruoyi-system
build:
context: /cloud-pck/ruoyi-cloud/system
dockerfile: Dockerfile
environment:
# 时区上海
TZ: Asia/Shanghai
volumes:
# 配置文件
- /docker/ruoyi-system/logs/:/gavino/system/logs
# skywalking 探针
- /docker/skywalking/agent/:/gavino/skywalking/agent
privileged: true
networks:
- ruoyi-cloud-net
networks:
ruoyi-cloud-net:
external: true