Gradle application plugin with multiple main classes

前端 未结 4 1388
南方客
南方客 2021-02-20 04:55

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 ch

4条回答
  •  眼角桃花
    2021-02-20 05:01

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

    application {
        applicationName = "myapp"
        mainClassName = "my.Main1"
    }
    tasks.named("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)
    }
    

提交回复
热议问题