Fat / shadow / uber jar with groovy scripts included

Hi all

I’ve tried to make a fat jar with the groovy scripts (in source code) from the dir src/main/scripts into the jar (at the root of the jar).

But I’ve tried many configurations, no one seems to work :disappointed:

I want to include all the groovy and sql,mongo,etc. dependencies into this fat jar to execute the groovy scripts with one java call,
because in my server groovy is not installed.
Like this
:fast_forward: java -cp helmeplease-all-1.0.jar groovy.ui.GroovyMain jar:file:helmeplease-all-1.0.jar'!'/thankyou.groovy

:gradlephant: My current build.gradle is here

group 'helmeplease'
version '1.0'

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'

sourceCompatibility = 1.7
targetCompatibility = 1.7


repositories {
    mavenCentral()
    mavenLocal()
    // ...
}


dependencies {
    compile group:'mysql', name:'mysql-connector-java', version:'5.1.46'
    compile 'commons-dbcp:commons-dbcp:1.4'
    compile group:'org.mongodb', name:'mongo-java-driver', version:'2.11.3'
    compile 'org.codehaus.groovy:groovy-all:2.1.9'
    compile group:'com.gmongo', name:'gmongo', version:'1.3'
}



jar {
    manifest {
        attributes 'Implementation-Title': 'lopd_importer', 'Implementation-Version': version
    }
}


task fatJar(type: Jar) {
    dependsOn install
    manifest {
        attributes 'Manifest-Version': '1.0', 'Implementation-Title': 'gd-web-lexnet-firma-client', 'Implementation-Version': version
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }  }
    with jar
}

I’ve tried with

sourceSets.main.resources.srcDirs += [ "src/main/scripts" ]

Also with

task fatJar(type: Jar) {
    // ...
    from('src/main/scripts) {
        include '**/*.groovy'
    }
    //...
}

And many more variations, but it seems I can’t hit the nail :confounded:

Anyone knows how to make it ?
Thanks :slightly_smiling_face:

Hi again!

I think I was without the needed coffe when I tried this :sweat:

Because I tried again, and now it works
With the code
sourceSets.main.resources.srcDirs += [ "src/main/scripts" ]
finally worked !! :blush:

Grapes wont work (event including ivy in gradle dependencies), this error is thrown
General error during conversion: No suitable ClassLoader found for grab - java.lang.RuntimeException: No suitable ClassLoader found for grab

But as you can include all dependencies into build.gradle, you’re ok

Final build.gradle version

group 'helmeplease'
version '1.0'

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'

sourceCompatibility = 1.7
targetCompatibility = 1.7


repositories {
    mavenCentral()
    mavenLocal()
    // ...
}


dependencies {
    compile group:'mysql', name:'mysql-connector-java', version:'5.1.46'
    compile 'commons-dbcp:commons-dbcp:1.4'
    compile group:'org.mongodb', name:'mongo-java-driver', version:'2.11.3'
    compile 'org.codehaus.groovy:groovy-all:2.1.9'
    compile group:'com.gmongo', name:'gmongo', version:'1.3'
}

sourceSets.main.resources.srcDirs += [ "src/main/scripts" ]


jar {
    manifest {
        attributes 'Implementation-Title': 'lopd_importer', 'Implementation-Version': version
    }
}


task fatJar(type: Jar) {
    dependsOn install
    manifest {
        attributes 'Manifest-Version': '1.0', 'Implementation-Title': 'gd-web-lexnet-firma-client', 'Implementation-Version': version
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }  }
    with jar
}

You can execute it with

java -cp helmeplease-all-1.0.jar groovy.ui.GroovyMain jar:file:helmeplease-all-1.0.jar'!'/thankyou.groovy

Or if you want your script external, just execute

java -cp helmeplease-all-1.0.jar groovy.ui.GroovyMain ./thankyou.groovy

I don’t know if there’s a more elegant solution, but this works fine

Thanks and sorry for the incovenience