Question

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?

Était-ce utile?

La solution 2

You should use use -P as listed in the Gradle command line documentation.

For example, the following will work:

gradlew main -Parg1=doc.json --project-prop arg2=text.txt

And you access them in your Gradle script like this:

println "$arg1 $arg2"

Autres conseils

task run(type: JavaExec) {
    main = "pkg.MainClass"
    classpath = sourceSets.main.runtimeClasspath
    args = ["arg1", "arg2"]
}
task run1(type: JavaExec) {
    main = "pkg.mainclass"
    classpath = sourceSets.main.runtimeClasspath
    args = ["$arg1","$arg2",...]
}

//I have named as run1 it can be any task name

While invoking the gradle script:
c:\> gradle run1 -Parg1="test123" -Parg2="sss"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top