2012-11-12 28 views

Trả lời

5

Tôi đã tìm ra cách tự làm. Sau đây là một đoạn mà tôi muốn chia sẻ cho những người có thể gặp phải vấn đề tương tự như tôi đã làm:

apply plugin: 'java' 
apply plugin: 'avro-gradle-plugin' 

sourceCompatibility = "1.6" 
targetCompatibility = "1.6" 

buildscript { 
    repositories { 
    maven { 
     // your maven repo information here 
    } 
    } 
    dependencies { 
    classpath 'org.apache.maven:maven-artifact:2.2.1' 
    classpath 'org.apache.avro:avro-compiler:1.7.1' 
    classpath 'org.apache.avro.gradle:avro-gradle-plugin:1.7.1' 
    } 
} 

compileAvro.source = 'src/main/avro' 
compileAvro.destinationDir = file("$buildDir/generated-sources/avro") 

sourceSets { 
    main { 
    java { 
     srcDir compileAvro.destinationDir 
    } 
    } 
} 

dependencies { 
    compileAvro 
} 
0

tôi thấy "com.commercehub.gradle.plugin.avro" gradle plugin để làm việc tốt hơn.

sử dụng folllowing:

// Gradle 2.1 and later 
plugins { 
    id "com.commercehub.gradle.plugin.avro" version "VERSION" 
} 

// Earlier versions of Gradle 
buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath "com.commercehub.gradle.plugin:gradle-avro-plugin:VERSION" 
    } 
} 
apply plugin: "com.commercehub.gradle.plugin.avro" 

thêm chi tiết tại https://github.com/commercehub-oss/gradle-avro-plugin

0

Khi đánh giá một plug-in các câu hỏi sau cần được hỏi:

file
  • đang tạo đưa vào jar nguồn?
  • Plugin có nhanh không? Plugin tốt sử dụng công cụ avro api thay vì giả mạo VM cho mọi tập tin. Đối với số lượng lớn các tập tin tạo VM cho mỗi tập tin có thể mất 10 phút để biên dịch.
  • Bạn có cần tệp avsc trung gian không?
  • Xây dựng có gia tăng (tức là không tạo lại tất cả các tệp trừ khi một trong các nguồn thay đổi) không?
  • Plugin có đủ linh hoạt để cấp quyền truy cập vào tệp lược đồ đã tạo hay không, vì vậy các hành động khác, chẳng hạn như lược đồ đăng ký trong kho lưu trữ lược đồ có thể được thực hiện?

Thật dễ dàng để triển khai mà không cần bất kỳ plugin nào nếu bạn không hài lòng với plugin hoặc cần linh hoạt hơn.

// 
// define source and destination 
// 
def avdlFiles = fileTree('src/Schemas').include('**/*.avdl') 
// Do NOT generate into $buildDir, because IntelliJ will ignore files in 
// this location and will show errors in source code 
def generatedJavaDir = "generated/avro/java" 

sourceSets.main.java.srcDir generatedJavaDir 

// 
// Make avro-tools available to the build script 
// 
buildscript { 
    dependencies { 
     classpath group:'org.apache.avro', name:'avro-tools' ,version: avro_version 
    } 
} 

// 
// Define task's input and output, compile idl to schema and schema to java 
// 
task buildAvroDtos(){ 
    group = "build" 

    inputs.files avdlFiles 
    outputs.dir generatedJavaDir 

    doLast{ 
     avdlFiles.each { avdlFile -> 
      def parser = new org.apache.avro.compiler.idl.Idl(avdlFile) 
      parser.CompilationUnit().getTypes().each { schema -> 
       def compiler = new org.apache.avro.compiler.specific.SpecificCompiler(schema) 
       compiler.compileToDestination(avdlFile, new File(generatedJavaDir)) 
      } 
     } 
    } 
} 

// 
// Publish source jar, including generated files 
// 
task sourceJar(type: Jar, dependsOn: buildAvroDtos) { 
    from sourceSets.main.allSource 
    // Package schemas into source jar 
    into("Schemas") { from avdlFiles } 
} 

// Clean "generated" folder upon "clean" task 
clean { 
    delete('generated') 
} 
Các vấn đề liên quan