2013-08-24 33 views
15

我想使用Gradle「application」插件爲第二個mainClass創建startScripts。這可能嗎?即使應用程序插件沒有內置此功能,是否可以利用startScripts任務爲另一個mainClass創建第二對腳本?是否可以使用gradle'application'插件指定多個主類

+0

看到這個答案:http://stackoverflow.com/questions/21241767/multiple-startscript-using-gradle-for-heroku – Phil

回答

4

您可以創建CreateStartScripts類型的多個任務,並在每個任務中配置不同的mainClassName。爲了方便起見,你可以在循環中做到這一點。

+3

是否有任何現有的代碼呢?文檔根本不能很好地解釋如何在「循環」中爲我們這些人同時學習groovy和gradle。 – Core

10

添加像這樣到根的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

我們可以創建一個啓動腳本來設置程序的命令行參數嗎? –

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在應用程序創建時ation插件被應用。

+0

applicationDistribution.from(otherStartScripts){into'bin'} – Joel

相關問題