How to include dependencies in jar?

Hello everyone,

I know it’s a bit late to update this post, however, I just had very similar case and managed to fix it without any external plugins as well :).

My case was to include external jars into some directory inside a jar being generated - let’s call it “lib” (similar to ear way). The following snippet does the trick:

configurations {
	jarLibs
}

...

dependencies {
...
	jarLibs "my:awesome-dependecy:1.0"
...
}

jar {
	def libBuildDir = mkdir "${buildDir}/resources/main/lib"
	copy {
		from { configurations.jarLibs }
		into { libBuildDir }
	}
}

The trick is about creating directory dedicated to external deps directly into build dir, under main resources path, and copy there all jars from specified configuration. Gradle is kind enough to copy this whole directory into the jar. End of magic.

Hope it will help someone struggling with similar issue.
Have fun! :slight_smile:

1 Like