Gradle Application Plugin - Building multiple start scripts / dists for the same project with different mainClassName

Here's one way to generate multiple start scripts with Gradle's application plugin:

mainClassName = "com.example.PrimaryEntryPoint"

startScripts {
  applicationName = 'primary'
}

// For convenience, define a map of additional start scripts.
// Key is script name and value is Java class.
def extraStartScripts = [
    'secondary' : 'com.example.SecondaryEntryPoint',
    'tertiary'  : 'com.example.TertiaryEntryPoint'
]

// Create a task for each additional entry point script.
extraStartScripts.each { scriptName, driverClass ->
  task(scriptName + "-script",
       group: 'CLI Script Generation',
       type: CreateStartScripts) {
    mainClassName = driverClass
    applicationName = scriptName
    outputDir = startScripts.outputDir
    classpath = startScripts.classpath
  }
}

// Include the additional start scripts in the distribution
applicationDistribution.into("bin") {
  from(tasks.withType(CreateStartScripts))
  fileMode = 0755
}

You can always drop down to the task level and declare/configure/wire the necessary tasks yourself, or declare additional tasks on top of what apply plugin: "application" provides. See the application plugin chapter in the Gradle user guide for which related tasks are available.

Tags:

Gradle