Bleusocks
(Dylan Curzon)
June 21, 2015, 11:46am
1
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.
carlo_lf
(Carlo Luib-Finetti)
June 21, 2015, 3:59pm
2
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.
Bleusocks
(Dylan Curzon)
June 21, 2015, 5:42pm
3
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.
aacirino
(Ângelo Andrade Cirino)
June 22, 2015, 5:37pm
4
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.
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.
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.
Bleusocks
(Dylan Curzon)
June 24, 2015, 9:50pm
6
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!