When gradle is packaged, the dependency is also merged into the jar package.

In some cases, you need to incorporate the jar package that your project depends on into your own jar package, which we call fat-jar. The method I use requires shadow plug-in:

The following example shows you typing mybatis into my jar package and changing the package name of mybatis from org. apache. ibatis to my. org. apache. ibatis.

This example is not a complete build.gradle file. Except for relocate, all the others are necessary. Relocate: modify the dependent package name when merging packages. If it is not necessary, you can ignore the relocate line.

 

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.4'
}

apply plugin: 'com.github.johnrengelman.shadow'


dependencies {
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.5'
}

shadowJar {
	classifier = ""
	relocate 'org.apache.ibatis', 'my.org.apache.ibatis'
	dependencies {
		include(dependency("org.mybatis:mybatis:3.4.5:withDependencies"))
	}
}

 

Finally, run the shadowJar command in the gradleTasks window of eclipse.

Or execute gradlew shadowJar

 

Leave a Reply

Your email address will not be published. Required fields are marked *