smeeb
smeeb

Reputation: 29537

Packaging and running executable JAR from single Gradle build invocation?

Here is my Groovy app's driver class:

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])
    }
}

I am trying to augment my Gradle build so that I can:

  1. Have Gradle package up my executable JAR; and
  2. Run my executable JAR, passing in command line args to its main method

Ideally, I would be able to simply run my app by:

gradle run -p 50

And See the following console output:

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

Here is my 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?

Upvotes: 0

Views: 622

Answers (1)

Sundeep Gupta
Sundeep Gupta

Reputation: 1976

To execute run task you need to apply application plugin. You can add below snippet to your build.gradle

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

You can replace "p" and "50" with some gradle property names, and pass those properties from command line like

gradle run -Pkey=p -Pvalue=50

Upvotes: 2

Related Questions