Gradle 2.6 — How to set the “Main” class to run using the Application plugin

I had been using an older version of Gradle and when I needed to specify my main class to run I added something similar to the lines below to my build.gradle file.

task run(dependsOn:'build') <<{
ant.java(classname: 'com.gesker.MyMainClass', fork: true,
classpath: "${sourceSets.main.runtimeClasspath.asPath}")
}

However, the Application plugin in newer versions of Gradle makes this much cleaner. Just add two lines similar to those below to your build.gradle file:

apply plugin: 'application'
mainClassName = "com.gesker.MyMainClass"

By main class I of course mean the specific class you want to run that contains the ever necessary:


public static void main(String args[]) {
// Your Code Here
}

The above is another great reason to revisit the docs for projects upon which you depend as you upgrade.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s