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

时间:2015-01-13 10:39:14

标签: groovy gradle gradle-plugin

这是我的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构建,以便我可以:

  1. 让Gradle打包我的可执行JAR;和
  2. 运行我的可执行文件JAR,将命令行参数传递给其主方法
  3. 理想情况下,我可以通过以下方式运行我的应用程序:

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

    我需要做些什么才能让Gradle包+像这样运行我的应用程序?

1 个答案:

答案 0 :(得分:2)

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

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

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

gradle run -Pkey=p -Pvalue=50