Is it possible to specify multiple main classes using gradle 'application' plugin

后端 未结 3 1108
南方客
南方客 2021-01-03 23:42

I would like to use the Gradle \"application\" plugin to create startScripts for a second mainClass. Is this possible? Even if the application plugin doesn\'t have this func

相关标签:
3条回答
  • 2021-01-04 00:24

    I combined parts of both of these answers to arrive at the relatively simple solution:

    task otherStartScripts(type: CreateStartScripts) {
        description "Creates OS specific scripts to call the 'other' entry point"
        classpath = startScripts.classpath
        outputDir = startScripts.outputDir
        mainClassName = 'some.package.app.Other'
        applicationName = 'other'
    }
    
    distZip {
        baseName = archivesBaseName
        classifier = 'app'
        //include our extra start script 
        //this is a bit weird, I'm open to suggestions on how to do this better
        into("${baseName}-${version}-${classifier}/bin") {
            from otherStartScripts
            fileMode = 0755
        }
    }
    

    startScripts is created when the application plugin is applied.

    0 讨论(0)
  • 2021-01-04 00:25

    You can create multiple tasks of type CreateStartScripts and in each task you configure a different mainClassName. for convenience, you can do this in a loop.

    0 讨论(0)
  • 2021-01-04 00:49

    Add something like this to your root build.gradle:

    // Creates scripts for entry points
    // Subproject must apply application plugin to be able to call this method.
    def createScript(project, mainClass, name) {
      project.tasks.create(name: name, type: CreateStartScripts) {
        outputDir       = new File(project.buildDir, 'scripts')
        mainClassName   = mainClass
        applicationName = name
        classpath       = project.tasks[JavaPlugin.JAR_TASK_NAME].outputs.files + project.configurations.runtimeClasspath
      }
      project.tasks[name].dependsOn(project.jar)
    
      project.applicationDistribution.with {
        into("bin") {
          from(project.tasks[name])
          fileMode = 0755
        }
      }
    }
    

    Then call it as follows either from the root or from subprojects:

    // The next two lines disable the tasks for the primary main which by default
    // generates a script with a name matching the project name. 
    // You can leave them enabled but if so you'll need to define mainClassName
    // And you'll be creating your application scripts two different ways which 
    // could lead to confusion
    startScripts.enabled = false
    run.enabled = false
    
    // Call this for each Main class you want to expose with an app script
    createScript(project, 'com.foo.MyDriver', 'driver')
    
    0 讨论(0)
提交回复
热议问题