springboot-jar瘦身
单模块打包瘦身
项目结构
pom.xml
<build>
<plugins>
<!-- 由于没有直接继承spring-boot-starter-parent,此处修添加主类和设置repackage -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<!-- 启动类 -->
<mainClass>yunping.com.SpringBootPlusApplication</mainClass>
<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>
瘦身之前
瘦身之后
调用lib包两种方式
第一种还没有瘦身的时候就把lib目录复制:缺陷后期添加其他jar包,需要手动添加
第二种是用命令生成jar包拿最新可以配置脚本(推荐):mvn dependency:copy-dependencies
Windows下运行maven命令:
C:\Users\Administrator\Desktop\sharding-jdbc\META-INF\maven\com.demo\spring-boot--shardingsphere-demo>mvn dependency:copy-dependencies
运行jar包
-Dloader.path:指定引用jar包的地址
–spring.profiles.active:指定配置文件是哪一个
java -Dloader.path=C:\\Users\\Administrator\\Desktop\\sharding-jdbc\\META-INF\\maven\\com.demo\\spring-boot--shardingsphere-demo\\target\\dependency -jar sharding-jdbc.jar --spring.profiles.active=dev
多模块打包瘦身(只有一个启动类user-system其他都是配置)
项目结构
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<!--<configuration>
<mainClass>com.yunping.ArchivesApplication</mainClass>
</configuration>-->
<configuration>
<!--第一部分-->
<layout>ZIP</layout>
<includes>
<!--包含lib的jar包-->
<include>
<groupId>com.yunping</groupId>
<artifactId>user-bootstrap</artifactId>
</include>
<include>
<groupId>com.yunping</groupId>
<artifactId>user-config</artifactId>
</include>
<include>
<groupId>com.yunping</groupId>
<artifactId>user-framework</artifactId>
</include>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--第二部分-->
<!--拷贝第三方依赖文件到指定目录-->
<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包的输出目录,根据自己喜好配置-->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<!--排除以下artifactId的jar包-->
<excludeArtifactIds>user-bootstrap</excludeArtifactIds>
<excludeArtifactIds>user-config</excludeArtifactIds>
<excludeArtifactIds>user-framework</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
第一部分zip打包依赖三个模块的包
第二部分添加maven-dependency-plugin插件用于将引用的jar包拷贝到指定的路径
运行jar包
-Dloader.path:指定引用jar包的地址
–spring.profiles.active:指定配置文件是哪一个
java -Dloader.path=C:\\Users\\Administrator\\Desktop\\user-system-2.0\\BOOT-INF\\lib -jar user.jar --spring.profiles.active=dev
报错
C:\Users\Administrator>java -Dloader.path=C:\\Users\\Administrator\\Desktop\\user1\\BOOT-INF\\lib -jar C:\Users\Administrator\Desktop\user.jar --spring.profiles.active=dev
_ _ _ _
(_) | | | | | |
___ _ __ _ __ _ _ __ __ _ ______| |__ ___ ___ | |_ ______ _ __ | |_ _ ___
/ __| '_ \| '__| | '_ \ / _` |______| '_ \ / _ \ / _ \| __|______| '_ \| | | | / __|
\__ \ |_) | | | | | | | (_| | | |_) | (_) | (_) | |_ | |_) | | |_| \__ \
|___/ .__/|_| |_|_| |_|\__, | |_.__/ \___/ \___/ \__| | .__/|_|\__,_|___/
| | __/ | | |
|_| |___/ |_|
:: Spring Boot :: (v2.2.5.RELEASE)
:: spring-boot-plus :: (v2.0)
https://springboot.plus
2021-08-17 10:09:11.118 INFO [,,,] [ main] [] yunping.com.ApiApplication [655] : The following profiles are active: dev
2021-08-17 10:09:12.463 WARN [,,,] [ main] [] ConfigServletWebServerApplicationContext [558] : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name '${visit.wechatPath}.FeignClientSpecification' defined in null: Cannot register bean definition [Generic bean: class [org.springframework.cloud.openfeign.FeignClientSpecification]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean '${visit.wechatPath}.FeignClientSpecification': There is already [Generic bean: class [org.springframework.cloud.openfeign.FeignClientSpecification]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
2021-08-17 10:09:12.473 INFO [,,,] [ main] [] ConditionEvaluationReportLoggingListener [136] :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-17 10:09:12.478 ERROR [,,,] [ main] [] o.s.b.d.LoggingFailureAnalysisReporter [40] :
***************************
APPLICATION FAILED TO START
***************************
Description:
//意思是Bean已经存在,不能被注册,我感觉不对我这个是配置在配置文件的,一个配置文件我就指定一个不会出现这个问题的。
The bean '${visit.wechatPath}.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
//意思是要在配置文件配置后来发现的bean会覆盖之前相同名称的bean,看了一下配置文件有配置所有不是这个问题
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
经过详细的排查我怀疑是打包的时候有问题,然后我去引用jar报的地方去看,发现这里多了三个包,于是我把它删掉,在运行却可以了。
但是这样我还是不知道为什么所以我做了试验把这里的包删除,引用的jar不删,发现还是报同样的错误
所以我得出结论这个是不是跟它的引用顺序有关系,先走引用jar包的位置,然后再引用自己本身。
我进到引用jar包的位置,解压user-bootstrap-2.0 看指定的配置文件dev发现没有配置: main:
allow-bean-definition-overriding: true这个东西,我把lib的复制到引用jar包的位置,发现可以了。
所以我推荐使用maven命令来生成jar包,这样配置可以拿最新的。
多模块打包瘦身(每一个模块都有启动类)
后期补充
参考
https://www.cnblogs.com/kingsonfu/p/11431743.html