Gradle: How to use one-jar output as input for launch4j

2.6k views Asked by At

I'd like to create a exe file, without having to put all required libraries beside the exe. Formerly with ant I created a self-contained jar file with one-jar and then wrapped this into a exe file with launch4j.

Gradle has plugins for both and standalone both work very well with almost no configuration.

But how can I manage to use the created one-jar as the input for launch4j?

This is my current buildfile:

apply plugin: 'java'
apply plugin: 'launch4j'
apply plugin: 'gradle-one-jar'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'edu.sc.seis.gradle:launch4j:1.0.6'
        classpath 'com.github.rholder:gradle-one-jar:1.0.4'
    }
}

launch4j {
    mainClassName = "de.my.umkopierer.Umkopierer"
    launch4jCmd = "C:/Program Files (x86)/Launch4j/launch4j"
    jar = "lib/Umkopierer-1.0.jar"
    headerType = "console"
    dontWrapJar = false
}

sourceCompatibility = 1.7
version = '1.0'

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

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    compile 'com.google.guava:guava:18.0'    
    compile 'com.fasterxml.jackson.core:jackson-core:2.4.4'
    compile 'com.fasterxml.jackson.datatype:jackson-datatype-jdk7:2.4.4'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'


    testCompile group: 'junit', name: 'junit', version: '4.+'
}

task oneJar(type: OneJar) {
    mainClass = "de.stiffi.umkopierer.Umkopierer"
}
1

There are 1 answers

0
Mart1n8891 On

I solved this by making the launch4j task 'createExe' dependent on the onejar/fatjar (or any other fat jar creation method). E.g.:

tasks.createExe.dependsOn('oneJar')     


task launch4j(overwrite: true, dependsOn: ['createExe']){
}

Also I think that your gradle build file should contain a main class attribute like

manifest {
    attributes 'Main-Class':'com.example.MyMainClass'
}

(at least that's the case if you are using the fatjar gradle plugin).