Alicia
Alicia

Reputation: 1161

How to execute command line tools with projects built with gradle?

Running a Java application built with Gradle requires adding to the classpath a directory for each individual library, as it stores them in individual directories.

Therefore, when you look at the command to launch the application in IntelliJ you can easily see a whole screen filled with classpath dependencies.

This is fine for IntelliJ because it can figure them out automatically, but I want to be able to run my command line tool in the terminal, writing arguments there instead of editing my run configuration each time I want to change anything. How can I do so without pasting a whole screen of machine-specific JAR dependencies from the IDE?

Is it possible to do it in a development environment without creating a giant JAR file bundling all the libraries?

Upvotes: 3

Views: 271

Answers (1)

majk
majk

Reputation: 857

Take a look at JavaExec task. You can create a custom task as such:

task customRun(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = "fully.qualified.name.of.your.main"
    args = ["100", "1000" ...] // the command line options for your tool
    jvmArgs = [ ... ] // additional JVM args you might need
}

Then you can invoke it using gradle customRun. If you want to be able to provide the command line arguments, I would suggest using gradle properties:

gradle -PcustomRunArgs="100 1000" customRun

and modifying the task to grab the arguments:

task ... {
   ...
   if (project.hasProperty('customRunArgs')) { 
        args =  customRunArgs.split(' ')
   }
   ...
}

Upvotes: 4

Related Questions