How to pass parameters to main method using Gradle?

Xelian picture Xelian · Mar 15, 2014 · Viewed 17.6k times · Source

I have to pass two arguments to my main method. My build script is

// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'maven central' for resolving your dependencies.
    mavenCentral()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'com.example:example-core:1.7.6'
}

task main(type: JavaExec, dependsOn: classes) {
    description = 'This task will start the main class of the example project'
    group = 'Example'
    main = 'com.example.core.Example'
    classpath = sourceSets.main.runtimeClasspath

}

If I try:

gradlew main doc.json text.txt

Then an error occured.

org.gradle.execution.TaskSelectionException: Task 'doc.json' not found in root project

How can I pass arguments to my main method command line easily?

Answer

juliocesarfx picture juliocesarfx · May 12, 2015
task run(type: JavaExec) {
    main = "pkg.MainClass"
    classpath = sourceSets.main.runtimeClasspath
    args = ["arg1", "arg2"]
}