环境:若依前后端分离版本,原打包时将所有的依赖jar包放至ruoyi-admin.jar 包中,该包130MB,过大。
需求:为了减少打包更新上传的时间,减少至1.1mb
1、将不常更新的模块jar包分离至lib文件夹
2、将常更新的模块如common、system等打包至jar包中。
在ruoyi-admin的pom文件中添加如下:
<!--先去除所有的jar包--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layout>ZIP</layout> <includeSystemScope>true</includeSystemScope> <includes> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> <!--将需要的JAR包保留,如:项目中的 common、framework、system 等更新频繁的模块--> <include> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-common</artifactId> </include> <include> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-framework</artifactId> </include> <include> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-system</artifactId> </include> <include> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-quartz</artifactId> </include> <include> <groupId>com.ruoyi</groupId> <artifactId>ruoyi-generator</artifactId> </include> </includes> </configuration> </plugin> <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>${java.run.main.class}</mainClass> <useUniqueVersions>false</useUniqueVersions> </manifest> </archive> </configuration> </plugin> <!-- 分离lib, 拷贝依赖到lib目录 --> <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> <!-- 依赖包输出目录,将来不打进jar包里 ,将更新不频繁的模块放进lib --> <outputDirectory>${project.build.directory}/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>runtime</includeScope> <!-- 排除如下jar包,将更新频繁的模块不放进lib,放进jar包 --> <excludeArtifactIds> ruoyi-common,ruoyi-framework,ruoyi-system,ruoyi-quartz,ruoyi-generator </excludeArtifactIds> </configuration> </execution> </executions> </plugin>
运行时会遇到一个问题:验证码无法显示
Caused by: java.lang.ClassNotFoundException: com.ruoyi.common.config.kaptcha.KaptchaTextCreator
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at com.google.code.kaptcha.util.ConfigHelper.getClassInstance(ConfigHelper.java:112)
解决方案是重新编译原google 的kaptcha源码,改动一下红框标示之处
便于我使用,我直接将这个类 KaptchaTextCreator 放在了 common中,所以
// properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.google.code.kaptcha.text.impl.KaptchaTextCreator");
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.ruoyi.common.config.kaptcha.KaptchaTextCreator");