Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle application plugin with multiple main classes

Tags:

java

gradle

I'm using the gradle 'application' plugin to start my application. This works well. Now I want to add the option to start a different main class in the same project. Can I change the plugin's configuration to allow that?

apply plugin: 'application'

mainClassName = "net.worcade.my.MainClass"
like image 730
Jorn Avatar asked May 12 '17 11:05

Jorn


People also ask

Can we have multiple build Gradle?

gradle for one project? Yes. You can have multiple build files in one project.

What does Gradle installDist do?

You can run gradle installDist to create an image of the application in build/install/projectName . You can run gradle distZip to create a ZIP containing the distribution, gradle distTar to create an application TAR or gradle assemble to build both.


2 Answers

From http://mrhaki.blogspot.com/2010/09/gradle-goodness-run-java-application.html

apply plugin: 'java'

task(runSimple, dependsOn: 'classes', type: JavaExec) {
   main = 'com.mrhaki.java.Simple'
   classpath = sourceSets.main.runtimeClasspath
   args 'mrhaki'
   systemProperty 'simple.message', 'Hello '
}

Clearly then what you can change:

  • runSimple can be named whatever you want
  • set main as appropriate
  • clear out args and systemProperty if not needed

To run:

gradle runSimple

You can put as many of these as you like into your build.gradle file.

like image 191
Joseph Larson Avatar answered Sep 20 '22 15:09

Joseph Larson


You can directly configure the Application Plugin with properties:

application {
    mainClassName = project.findProperty("chooseMain").toString()
}

And after in command line you can pass the name of the main class:

./gradlew run -PchooseMain=net.worcade.my.MainClass
like image 25
GuanacoBE Avatar answered Sep 17 '22 15:09

GuanacoBE