首页 文章

从单个Gradle构建调用打包并运行可执行JAR?

提问于
浏览
0

这是我的Groovy应用程序的驱动程序类:

package org.me.myapp

class MyDriver {
    static void main(String[] args) {
        // The p flag was passed in and had a value of 50!
        println String.format("The %s flag was passed in and had a value of %s!", args[0], args[1])
    }
}

我正在尝试增加我的Gradle构建,以便我可以:

  • 让Gradle打包我的可执行JAR;和

  • 运行我的可执行文件JAR,将命令行参数传递给其main方法

理想情况下,我可以通过以下方式运行我的应用:

gradle run -p 50

并查看以下控制台输出:

The p flag was passed in and had a value of 50!

这是我的 build.gradle

apply plugin: 'groovy'
apply plugin: 'eclipse'

sourceCompatibility = '1.7'
targetCompatibility = '1.7'

repositories {
    mavenCentral()
}

dependencies {
    compile (
        'org.codehaus.groovy:groovy-all:2.3.9',
        'com.google.guava:guava:18.0',
        'com.google.inject:guice:3.0'
    )
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

What do I need to do to be able to have Gradle package + run my app like this?

1 回答

  • 2

    要执行 run 任务,您需要应用 application 插件 . 您可以将以下代码段添加到 build.gradle

    apply plugin: 'application'
    mainClassName = "org.me.myapp.MyDriver"
    run {
        args "p"
        args "50"
    }
    

    您可以将 "p""50" 替换为某些gradle属性名称,并从命令行传递这些属性,如

    gradle run -Pkey=p -Pvalue=50

相关问题