Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to main method using Gradle?

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?

like image 376
Xelian Avatar asked Mar 15 '14 12:03

Xelian


People also ask

How do you pass arguments in gradle?

Types of Input Arguments When we want to pass input arguments from the Gradle CLI, we have two choices: setting system properties with the -D flag. setting project properties with the -P flag.

How does dependsOn work gradle?

dependsOn(jar) means that if you run assemble , then the jar task must be executed before. the task transitive dependencies, in which case we're not talking about tasks, but "publications". For example, when you need to compile project A , you need on classpath project B , which implies running some tasks of B .

What is the difference between gradle and gradlew?

The difference lies in the fact that ./gradlew indicates you are using a gradle wrapper. The wrapper is generally part of a project and it facilitates installation of gradle.


2 Answers

task run(type: JavaExec) {
    main = "pkg.MainClass"
    classpath = sourceSets.main.runtimeClasspath
    args = ["arg1", "arg2"]
}
like image 198
juliocesarfx Avatar answered Sep 21 '22 09:09

juliocesarfx


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"
like image 36
Ravikumar Avatar answered Sep 22 '22 09:09

Ravikumar