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

Groovy 从单个Gradle构建调用打包和运行可执行JAR?,groovy,gradle,gradle-plugin,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],

下面是我的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,将命令行参数传递给它的主方法
  • 理想情况下,我可以通过以下方式运行我的应用程序:

    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 package+像这样运行我的应用程序?

    要执行
    运行
    任务,您需要应用
    应用程序
    插件。您可以将下面的代码片段添加到
    build.gradle

    apply plugin: 'application'
    mainClassName = "org.me.myapp.MyDriver"
    run {
        args "p"
        args "50"
    }
    
    您可以用一些gradle属性名替换
    “p”
    “50”
    ,并从命令行传递这些属性,如


    gradle run-Pkey=p-Pvalue=50
    谢谢@Sundeep Gupta(+1)-我如何访问代码中的那些gradle属性名称,比如
    System.getProperty(“key”)
    ?否。您可以直接访问它们
    运行{args key args value}
    使用
    -P=
    设置的属性将作为项目属性添加。