How to include dependencies in jar?

Hi David,

Thanks for your answer. It’s just that for a rookie like me it is very confusing that the program works fine in the IDE, but once it is compiled into a jar it is missing dependencies.

Fortunately I have solved my issue without the usage of external plugins. Here is the fixed code which took me ages to find:

plugins {
    id "groovy"
    id "java"
    id "idea"
}

//Grade attributes
group 'com.nikecow'
version '0.1'

task wrapper(type: Wrapper) {
    gradleVersion = "3.0";
}

repositories {
    mavenCentral()
}

configurations {
    // configuration that holds jars to include in the jar
    extraLibs
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.7'
    extraLibs group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
    testCompile group: 'junit', name: 'junit', version: '+'
    configurations.compile.extendsFrom(configurations.extraLibs)
}

jar {
    from {
            configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
        }
}

Now what you put behind “extraLibs” will be both compiled and included into your jar as a dependency. Hopefully it helps out some other people.

8 Likes