Можно ли указать несколько основных классов с помощью плагина gradle 'application' - программирование
Подтвердить что ты не робот

Можно ли указать несколько основных классов с помощью плагина gradle 'application'

Я хотел бы использовать плагин Gradle "application" для создания startScripts для второго mainClass. Это возможно? Даже если в плагине приложения не встроена эта функция, можно ли использовать задачу startScripts для создания второй пары скриптов для другого mainClass?

4b9b3361

Ответ 1

Добавьте что-то вроде этого в ваш 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.runtime
  }
  project.tasks[name].dependsOn(project.jar)

  project.applicationDistribution.with {
    into("bin") {
      from(project.tasks[name])
      fileMode = 0755
    }
  }
}

Затем вызовите его как из корня, так и из подпроектов:

// 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')

Ответ 2

Вы можете создать несколько задач типа CreateStartScripts, и в каждой задаче вы настраиваете другой mainClassName. для удобства вы можете сделать это в цикле.

Ответ 3

Я объединил части обоих этих ответов, чтобы прийти к относительно простому решению:

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 создается при применении плагина приложения.