Gradle application plugin with multiple main classes

前端 未结 4 1374
南方客
南方客 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:10

    Use javaExec task to handle it :

    task run(type: JavaExec) {
        classpath = sourceSets.main.runtimeClasspath
    
        if (project.hasProperty('first')){
            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
    

提交回复
热议问题