如何将构建的胖JAR作为gradle任务执行?

时间:2017-07-19 03:04:51

标签: java gradle jar build classpath

如何利用gradle 应用程序插件从gradle执行fat JAR:

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'

}

条件是我希望胖JAR实际构建并希望执行实际 JAR文件本身。在从CLI运行fatJAR的context中。

参考:

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?

1 个答案:

答案 0 :(得分:1)

您需要的是添加JavaExec类型的任务,该任务将配置类路径以包含使用fatJar和主类集构建的jar。这是我准备的build.gradle,它显示了它应该是什么样子:

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'
}

here你可以找到可运行的演示。