开发环境 / 开发技巧 Springboot瘦身(二) 2021-03-30 [TOC] # 半自动化步骤 ## 1. 更换maven的jar打包插件 > 先前使用的是spring-boot-maven-plugin来打包,这个插件会将项目所有的依赖打入BOOT-INF/lib下,替换为maven-jar-plugin: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>org.jeecg.JeecgApplication</mainClass> </manifest> </archive> </configuration> </plugin> ``` > 解释: a、addClasspath表示需要加入到类构建路径 b、classpathPrefix指定生成的Manifest文件中Class-Path依赖lib前面都加上路径,构建出lib/xx.jar ## 2. 拷贝依赖到jar外面的lib目录 ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-lib</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>target/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>runtime</includeScope> </configuration> </execution> </executions> </plugin> ``` ![](/uploads/1/image/public/202103/20210330175356_ic6y7tpmnq.png) ## 3.在和jar包同级的目录下新建一个config目录,放入application.yml文件 > 解释:打包的jar里面有application.yml文件为什么再放一份? **这是因为boot读取配置有一个优先级,放在jar包外面config目录优先级最高,主要是便于从外部修改配置,而不是改jar包中的application.yml文件。** 优先级如下: 当前目录的config目录下 当前目录 classpath的config目录下 classpath的根目录 ## 4.启动项目 > 加入–debug可以让你可以看到比较详细的启动日志: ```java java -jar xxx-1.0.0.jar --debug ``` 完整配置: ```xml <build> <plugins> <!-- maven 打包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>org.jeecg.JeecgApplication</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 拷贝依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-lib</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>target/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>runtime</includeScope> </configuration> </execution> </executions> </plugin> </plugins> </build> ```