首页 文章

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

提问于
浏览
0

我如何利用gradle application 插件从gradle执行胖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实际构建并希望自己执行 actual 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 回答

  • 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你可以找到可运行的演示 .

相关问题