是否可以使用gradle'application'插件指定多个主类

Jef*_*son 17 java gradle

我想使用Gradle"application"插件为第二个mainClass创建startScripts.这可能吗?即使应用程序插件没有内置此功能,是否可以利用startScripts任务为不同的mainClass创建第二对脚本?

kyl*_*yre 13

将这样的内容添加到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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后从根或子项目中调用如下:

// 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')
Run Code Online (Sandbox Code Playgroud)

  • 我们有机会创建一个启动脚本来设置程序的命令行参数吗? (2认同)

Ren*_*hke 4

您可以创建多个类型的任务CreateStartScripts,并在每个任务中配置不同的mainClassName. 为了方便起见,您可以循环执行此操作。

  • 那里有现有的代码吗?对于我们这些同时学习 groovy 和 gradle 的人来说,文档并不能很好地解释如何“循环”执行此操作。 (4认同)