Support for multiple application scripts in application plugin

Its a very common requirement in projects, and there should be an easy way of doing it.

The gradle application plugin does not provide first class support for multiple executable scripts.

Luckily though, because gradle scripts are groovy, you can add to what the application plugin does reasonably easily.

The documentation for the Application plugin show that the startScripts task is of type CreateStartScripts, so try creating yourself a second task of the same type

task moreStartScripts(type: CreateStartScripts) {
    mainClassName = "foo.bar.AnotherMain"
    applicationName = "another"
     outputDir = new File(project.buildDir, 'scripts')
    classpath = jar.outputs.files + project.configurations.runtime
}

then include the output of that task in your distribution

applicationDistribution.into("bin") {
            from(moreStartScripts)
            fileMode = 0755
}

If you have many scripts to create, you could use a loop with task.create(name: taskname, type: CreateStartScripts).

task createAllStartScripts() << {
     // just a placeholder
}
  def scripts = [ 'scriptA': 'com.example.ScriptA',
 'scriptB':'com.example.ScriptB'
]
  scripts.each() { scriptName, className ->
          def t = tasks.create(name: scriptName+'StartScript', type: CreateStartScripts) {
        mainClassName = className
        applicationName = scriptName
        outputDir = new File(project.buildDir, 'scripts')
        classpath = jar.outputs.files + project.configurations.runtime
    }
      applicationDistribution.into("bin") {
            from(t)
            fileMode = 0755
    }
    createAllStartScripts.dependsOn(t)
}

Thank you Perryn & Philip, much appreciated!

Just wondering, is it obvious for everyone that ordering of those blocks is important? :wink:

Application plugin supports ‘applicationDefaultJvmArgs’, any chance this solution could also support overriding JAVA_OPTS?