springboot 1.5.x + gradle4.x 打包问题:jar包不包含依赖lib

springboot 1.5.x + gradle4.x 打包问题:jar包不包含依赖lib

前言

开发环境:IDEA 2018.2.5,gradle 版本:4.10.1-all ,springboot版本:1.5.20.RELEASE
依赖的写法:

dependencies {

	/********************************************************************************
	 *
	 *                                   lib 库依赖
	 *
	 ********************************************************************************/
	implementation fileTree(dir: 'libs', include: ['*.jar'])
	//implementation project(':commoncode')
	implementation "com.gosuncn:commoncode:1.0.0"

由于gradle版本升级为4.10.x,会提示compile 已过时,需要更换为 implemention
问题来了:

在使用gradle打包的时候,发现并没有把相关的依赖库打包进jar包中

解决思路

网上一阵google搜索,发现是springboot 1.5.x版本和gradle搭配打包的时候的问题:
bootRepackage fails to package dependencies from the new gradle Java Library plugin
引用其中的问题描述:

Bug report
When using the new Gradle Java Library plugin and declaring dependencies using the api or implementation configurations, the bootRepackage task doesn’t package the dependencies into the final jar.
If I replace the declared dependencies using the compile configuration, then the bootRepackage task does package them into the jar. However, the gradle documentation says to ignore this configuration:
The compile, testCompile, runtime and testRuntime configurations inherited from the Java plugin are still available but are deprecated. You should avoid using them, as they are only kept for backwards compatibility.

跟我们上面的描述一样,也是把compile更换为implemention,然后打包就出问题了

下面是其他人提出的问题根源及解决方案,我们基本照着这个来修改就行了

The repackaging includes everything in the runtime configuration which is why dependencies in the api and implementation configurations are not being included. 2.0 snapshots are not affected by this problem as the new bootJar task doesn’t deal with configurations directly. Instead, it uses the runtime class path of the main source set.

In 1.5 and earlier, you can configure a custom configuration that will have its dependencies packaged in the jar. Ideally, this would be possible:

意思是springboot 1.5.x的问题,2.x的不受其影响
在build.gradle中进行配置,就可以正常打包啦

configurations {
    custom {
        it.extendsFrom implementation
    }
}

bootRepackage {
    customConfiguration = 'custom'
}

注意:若有依赖是用runtime,记得修改为implemention

总结

对上述问题有如下两种方案:

  1. 升级springboot版本为2.x
  2. build.gradle中进行配置

参考

1.bootRepackage fails to package dependencies from the new gradle Java Library plugin
2. Spring Boot Gradle plugin

猜你喜欢

转载自blog.csdn.net/u010165638/article/details/89518527