21

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"
Jorn
  • 20,612
  • 18
  • 79
  • 126
  • What is the condition that you want to base this conditional config on? – Manish Kumar Sharma May 12 '17 at 12:14
  • I'm open to suggestions. A command line parameter, a `-D` argument... – Jorn May 12 '17 at 12:19
  • But there must be some condition in which you want this to be main class..what is that? – Manish Kumar Sharma May 12 '17 at 12:20
  • In my IDE, I'd just make two run targets, one for each main class. So the first one could call `gradle run first` and the second one could call `gradle run second`. – Jorn May 12 '17 at 12:23
  • If that's all you need, you can either run the class directly from the IDE, without going through gradle, or add two tasks of type JavaExec to your build file. https://docs.gradle.org/3.5/dsl/org.gradle.api.tasks.JavaExec.html – JB Nizet May 12 '17 at 12:40
  • Have you worked it out? What's your scenario? – Opal May 15 '17 at 05:17

4 Answers4

24

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.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • 1
    If you want to pass arguments from the command line you can do this with: `gradle runSimple --args 'arg1 arg2'` (You'll need to remove the `args` bit from the `task()` obviously) – Kevin Sadler Mar 27 '19 at 21:04
8

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
GuanacoBE
  • 442
  • 1
  • 7
  • 12
4

Here's how you can generate multiple start scripts if you need to package your apps

application {
    applicationName = "myapp"
    mainClassName = "my.Main1"
}
tasks.named<CreateStartScripts>("startScripts") {
    applicationName = "myapp-main1"
}
val main2StartScripts by tasks.register("main2StartScripts", CreateStartScripts::class) {
    applicationName = "myapp-main2"
    outputDir = file("build/scripts") // By putting these scripts here, they will be picked up automatically by the installDist task
    mainClassName = "my.Main2"
    classpath = project.tasks.getAt(JavaPlugin.JAR_TASK_NAME).outputs.files.plus(project.configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)) // I took this from ApplicationPlugin.java:129
}
tasks.named("installDist") {
    dependsOn(main2StartScripts)
}
apflieger
  • 912
  • 10
  • 18
  • I like this approach. What about re-using the classpath value that the application plugin has set for the standard `startScripts` task? I could do so by using `classpath = tasks.startScripts.get().classpath` and the same with `outdir`. And it is probably also possible to let `startScripts` depend on the custom task, right? Inspired by [this answer](https://stackoverflow.com/a/59724219/1804173). – bluenote10 Dec 15 '21 at 12:56
1

Use javaExec task to handle it :

task run(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath

    if (project.hasProperty('chooseMain')){
        if (chooseMain == 'Main1'){
            main = 'application.Main1'
        } else if (chooseMain == 'second'){
            main = 'application.Main2'
        }
    } else {
        println 'please pass the main name'
    }
}

And from the command line pass your option in that way :

gradle run -PchooseMain=first
koppor
  • 19,079
  • 15
  • 119
  • 161
Daniel Taub
  • 5,133
  • 7
  • 42
  • 72