如何将java注释处理器集成到java插件中

Cra*_*ing 3 java hibernate gradle

我的项目布局如下:

src/
  java
  generated
Run Code Online (Sandbox Code Playgroud)

src/java包含jpa实体和查询类,它们使用由hibernate元模型注释处理器生成的jpa元模型类.

将注释处理合并到java插件的最佳方法是什么?

我目前定义了以下任务,但它对compileJava有一个任务依赖,它将失败,因为某些代码依赖于注释处理器生成的类.

task processAnnotations(type: Compile) {
    genDir = new File("${projectDir}/src/generated")
    genDir.mkdirs()
    source = ['src/java']
    classpath = sourceSets.test.compileClasspath
    destinationDir = genDir
    options.compilerArgs = ["-proc:only"]
}
Run Code Online (Sandbox Code Playgroud)

Vic*_*rov 6

这是一个简单的设置,可以与netbeans无缝集成.Javac将基本完成所需的所有工作,无需太多干预.其余的都是小型的,它们可以与像Netbeans这样的IDE一起使用.

apply plugin:'java'

dependencies {
    // Compile-time dependencies should contain annotation processors
    compile(group: 'org.hibernate...', name: '...', version: '...')
}

ext {
    generatedSourcesDir = file("${buildDir}/generated-sources/javac/main/java")
}

// This section is the key to IDE integration.
// IDE will look for source files in both in both
//
//  * src/main/java
//  * build/generated-sources/javac/main/java
//
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir generatedSourcesDir
        }
    }
}

// These are the only modifications to build process that are required.
compileJava {
    doFirst {
        // Directory should exists before compilation started.
        generatedSourcesDir.mkdirs()
    }
    options.compilerArgs += ['-s', generatedSourcesDir]
}
Run Code Online (Sandbox Code Playgroud)

就是这样.Javac将完成剩下的工作.