简体   繁体   中英

how to execute a built fat JAR as a gradle task?

How would I utilize the gradle application plugin to execute the fat JAR from gradle as so:

thufir@doge:~/NetBeansProjects/selenium$ 
thufir@doge:~/NetBeansProjects/selenium$ gradle clean fatJar;java -jar build/libs/selenium-all.jar 

BUILD SUCCESSFUL in 23s
4 actionable tasks: 4 executed
Jul 18, 2017 7:47:49 PM net.bounceme.dur.web.selenium.Main main
INFO: running..
1500432473386   geckodriver INFO    Listening on 127.0.0.1:30879
1500432475320   geckodriver::marionette INFO    Starting browser /usr/lib/firefox/firefox with args ["-marionette"]
1500432488971   addons.xpi  WARN    Add-on langpack-en-ZA@firefox.mozilla.org is not compatible with application version.
1500432488978   addons.xpi  WARN    Add-on langpack-en-ZA@firefox.mozilla.org is not compatible with application version.
1500432489767   addons.xpi  WARN    Add-on langpack-en-GB@firefox.mozilla.org is not compatible with application version.
1500432489769   addons.xpi  WARN    Add-on langpack-en-GB@firefox.mozilla.org is not compatible with application version.
^Cthufir@doge:~/NetBeansProjects/selenium$ 

build.gradle:

plugins {
    id 'com.gradle.build-scan' version '1.8' 
    id 'java'
    id 'application'
}

mainClassName = 'net.bounceme.dur.web.selenium.Main'

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree = 'yes'
}

repositories {
    jcenter()
}

jar {
    manifest {
        attributes 'Main-Class': 'net.bounceme.dur.web.selenium.Main'
        attributes 'Class-path': 'selenium-java-3.4.0.jar'
    }
}

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': '3.4.0'
        attributes 'Main-Class': 'net.bounceme.dur.web.selenium.Main'
    }
}

dependencies {
     compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.4.0'
     compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '3.4.0'

    compile group: 'org.seleniumhq.selenium', name: 'selenium-firefox-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-ie-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-safari-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-edge-driver', version: '3.4.0'

}

with the proviso that I want the fat JAR to actually build and want to execute the actual JAR file itself . In the context of running a fatJAR from the CLI.

Reference:

https://docs.gradle.org/current/userguide/userguide_single.html

Running an executable jar file built from a gradle based project

Run jar with parameters in gradle

How to execute jar file in gradle?

What you need is to add a task of type JavaExec that will have the classpath configured to contain the jar built with fatJar and main class set. Here's a build.gradle I prepared that shows how it should look like:

plugins {
    id 'java'
}

repositories {
    jcenter()
    mavenCentral()
}

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    manifest {
        attributes 'Main-Class': 'org.Lol'
    }
}

dependencies {
    compile 'com.google.guava:guava:22.0'
}

task runFatJar(type: JavaExec) {
    dependsOn fatJar
    classpath = fatJar.outputs.files
    main = 'org.Lol'
}

and here you can find the runnable demo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM