Skip to content

Instantly share code, notes, and snippets.

@nobuoka
Last active September 28, 2017 09:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nobuoka/7614981 to your computer and use it in GitHub Desktop.
Save nobuoka/7614981 to your computer and use it in GitHub Desktop.
Java Annotation Processing Task for Gradle
// java-apt プラグインを使うサンプル
apply plugin: 'java'
apply plugin: 'java-apt'
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
}
// Eclipse Link を使って JPA の canonical metamodel クラスを生成する Annotation Processing のサンプル
dependencies {
compile 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.5.+'
// Annotation Processor を含むライブラリ
apt 'org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor:2.5.+'
}
procJava {
// ここでは EclipseLink の実装を使って JPA の canonical metamodel クラスを生成する
def persistencexml = new File(project.projectDir, 'src/main/resources/META-INF/persistence.xml').absolutePath
// 処理対象ソースコードの指定
source = project.compileJava.source
// Annotation Processor の指定
processor = 'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor'
// `javac` コマンドの `-A` オプションに渡す値
apOptions = 'eclipselink.persistencexml=' + persistencexml
}
# buildSrc/src/main/resources/META-INF/gradle-plugins/java-apt.properties
implementation-class=JavaAptPlugin
// buildSrc/src/main/groovy/JavaAptPlugin.groovy
class JavaAPT extends DefaultTask {
private File _destinationDir
def getDestinationDir(dir) { _destinationDir }
void setDestinationDir(dir) {
_destinationDir = dir
outputs.dir _destinationDir
}
private Object _source
def getSource() { _source }
void setSource(src) {
_source = src
inputs.source src
}
String processor
String apOptions
@TaskAction
void process() {
def javaCompile = project.task(name + "_", type: JavaCompile);
javaCompile.source _source
javaCompile.destinationDir = _destinationDir
javaCompile.classpath = project.configurations.compile
javaCompile.options.compilerArgs.addAll '-proc:only', '-implicit:none',
'-processorpath', project.configurations.apt.asPath,
'-processor', processor,
'-A' + apOptions
javaCompile.execute()
project.tasks.remove(javaCompile)
}
}
class JavaAptPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('procJava',type: JavaAPT) {
def aptTarget = 'src/main/javaGen'
project.tasks.compileJava.dependsOn it
project.sourceSets.main.java.srcDirs << aptTarget
destinationDir = new File(project.projectDir, aptTarget)
}
project.configurations {
apt {
description "for annotation processors"
}
}
}
}
@sobvan
Copy link

sobvan commented Sep 28, 2017

Thanks for the code @nobuoka.
I have forked it, and fixed it for Gradle 4.2, added an option for easy skipping in multi-project build, and added the missing import to the JavaAptPlugin.groovy file.
Fork is here:
https://gist.github.com/istvanszoboszlai/6ae8440a5f8ba818e29bedfd68bbce2d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment