Package dependencies with jar

My aim is to package libraries downloaded by gradle with the jar file created, yet the way I’m currently doing so gives an exception:

lang.ClassNotFoundException: com.mongodb.client.MongoDatabase

I realise that this is because gradle isn’t packaging the libraries with the jar, but don’t know how to fix it. This is my build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.8
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    runtime 'org.mongodb:mongodb-driver:3.0.2'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

I’ve already tried replacing “runtime” with “compile” and “default”, neither worked, the latter giving an exception on build. If anyone has any ideas I’d be massively grateful. Thanks in advance.

you might overwrite task jar and do something like this:

jar {
into (’/’)
from configurations.runtime
}

Or, maybe better, have a look into Gradle user guide, Chapter 43 and 44.

Ok great, we’re closer. Doing the above exports the gradle dependencies needed to the compiled jar, but apparently not libaries included in the project, nor the src. Could you give me an idea of how I could solve this, or where to find out how to? Thanks.

This works for me:

configurations {
	libraries
}

dependencies {
	libraries 'commons-lang:commons-lang:2.+'
	
	configurations.compile.extendsFrom(configurations.libraries)
}

jar {
	into('lib') {
		from configurations.libraries
	}
}

I hope it helps, since the runtime configuration extends from compile.

What do you mean by “library”?

For the sources, you can add ‘from sourcesSets.all.allSource’
Or sourcesSets.main.allSource to get only the main source set.

It’s fine, I solved this. The problem was I didn’t realise that source had to be located in src/main/java for it to be placed in the jar. My bad!