我有一个布局如下的项目:
src/
java
generated
src/java 包含使用 hibernate metamodel annotation processor 生成的 jpa 元模型类的 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"]
}
最佳答案
这是一个简单的设置,可以与 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]
}
就是这样。 Javac 将完成剩下的工作。
关于java - 如何将 java 注释处理器集成到 java 插件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11375953/