【问题标题】:Is it possible to specify multiple main classes using gradle 'application' plugin是否可以使用 gradle 'application' 插件指定多个主类
【发布时间】:2013-08-24 18:38:04
【问题描述】:

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

【问题讨论】:

标签: java gradle


【解决方案1】:

将类似这样的内容添加到您的根 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
    }
  }
}

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

// 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】:

我将这两个答案的部分结合起来,得出了一个相对简单的解决方案:

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 是在应用应用程序插件时创建的。

【讨论】:

  • applicationDistribution.from(otherStartScripts) {into 'bin'} 怎么样
【解决方案3】:

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

【讨论】:

  • 那里有现成的代码吗?对于我们这些同时学习 groovy 和 gradle 的人来说,文档根本无法很好地解释如何“循环”执行此操作。
猜你喜欢
  • 2020-08-28
  • 2017-06-26
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
相关资源
最近更新 更多