2013-08-24 15 views
15

Tôi muốn sử dụng plugin "ứng dụng" Gradle để tạo startScripts cho lớp mainClass thứ hai. Điều này có thể không? Ngay cả khi plugin ứng dụng không có chức năng này được tích hợp sẵn, bạn có thể tận dụng tác vụ startScripts để tạo một cặp tập lệnh thứ hai cho một lớp mainClass khác không?Có thể chỉ định nhiều lớp chính bằng cách sử dụng plugin 'ứng dụng' gradle

+0

Xem câu trả lời này: http://stackoverflow.com/questions/21241767/multiple-startscript-using-gradle-for-heroku – Phil

Trả lời

4

Bạn có thể tạo nhiều tác vụ thuộc loại CreateStartScripts và trong mỗi tác vụ bạn định cấu hình khác nhau mainClassName. để thuận tiện, bạn có thể làm điều này trong một vòng lặp.

+3

Có bất kỳ mã hiện có nào không? Tài liệu chỉ đơn giản là không làm một công việc tốt giải thích làm thế nào để làm điều này 'trong một vòng lặp' cho những người trong chúng ta học groovy và gradle cùng một lúc. – Core

10

Thêm một cái gì đó như thế này để build.gradle gốc của bạn:

// 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 
    } 
    } 
} 

Sau đó gọi nó như sau hoặc từ gốc hoặc từ các tiểu dự án:

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

Bất kỳ cơ hội nào chúng ta có thể tạo tập lệnh khởi động để thiết lập các đối số dòng lệnh của chương trình? –

3

tôi kết hợp các bộ phận của cả hai câu trả lời để đến giải pháp tương đối đơn giản:

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 được tạo khi ứng dụng plugin ation được áp dụng.

+0

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