开发环境 / 开发技巧 Springboot瘦身 2021-03-30 [TOC] # Springboot瘦身(lib和程序分开打包) ## 1. 首先用mvn clean package正常打出jar包 > 这个jar包可能有几百兆大小,lib占了绝大多数 将jar包解压,将 BOOT-INF 下的 lib 包拿出单独存放 ## 2. 修改pom重新打包 ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layout>ZIP</layout> <includes> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` ![](/uploads/1/image/public/202103/20210330175356_ic6y7tpmnq.png) ## 3.再次用 mvn clean package 打出来的包就不包含lib了 ## 4.运行jar包 假设lib包路径为:C:\111111\lib下,则执行下面命令启动项目。 ```java java -Dloader.path=C:\111111\lib -jar jeecg-boot-module-system-2.3.0.jar ``` ## 5.目录结构 ![](/uploads/1/image/public/202103/20210330175728_gbp3wwtsmi.png) # 问题说明 ## 启动失败,报异常:Failed to configure a DataSource > 异常:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource **具体如下:** *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active). Process finished with exit code 1 **原因分析:** 无法配置DataSource:未指定'url'属性,也无法配置嵌入数据源,而实际的项目中是有相关配置的,只能说明加载配置失败。 **处理方式:** 在项目Jar和lib包同级目录下,创建config包,并将application.yml配置文件复制到config目录,然后启动,或将启动命令写成start.bat,双击启动。 结构如下: ![](/uploads/1/image/public/202103/20210330175728_gbp3wwtsmi.png) start.bat文件内容如下: ```java java -Dloader.path=E:\runwork\lib -jar jeecg-boot-module-system-2.4.3.jar ``` config目录下application.yml文件内容如下: ```yaml spring: application: name: jeecg-system profiles: active: dev ``` **原理分析:** 因为Spring Boot读取配置有一个优先级,放在jar包外面同级config目录的优先级最高(主要是便于从外部修改配置,而不是改jar包中的application.yml文件), 具体优先级如下: jar包外面同级目录的config目录下的配置文件>jar包外面同级目录>classpath的config目录下>classpath的根目录