環境:若依前後端分離版本,原打包時將所有的依賴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");