Gradle – the gradle equivalent of maven’s exec plugin for running Java apps

gradle

With maven I can create a project, set up my pom with its dependencies, write a class with a main method and then to run it type:

mvn compile exec:java -Dexec.mainClass=thornydev.App

What is the gradle equivalent of doing this?

I can do gradle build, which builds a jar file for me, but if the main class has any dependencies on other jars, just running the jar won't work without setting up the classpath. Can the gradle java plugin run the app and set up the classpath for me?

I'm looking for a command line solution for simple one-off uses, not IDE integration (I know how to do that).

Best Solution

The easiest solution is to use the Application Plugin, which among other things provides a run task. To make the main class configurable from the command line, you'll have to set mainClassName to the value of some system (or project) property, and then pass that property from the command line:

apply plugin: "application"

mainClassName = System.getProperty("mainClass")

Now you can run the application with gradle run -DmainClass=thornydev.App.