Skip to content

Instantly share code, notes, and snippets.

@sobvan
Forked from nobuoka/JavaAptPlugin.groovy
Last active August 20, 2019 13:31
Show Gist options
  • Save sobvan/6ae8440a5f8ba818e29bedfd68bbce2d to your computer and use it in GitHub Desktop.
Save sobvan/6ae8440a5f8ba818e29bedfd68bbce2d to your computer and use it in GitHub Desktop.
Java Annotation Processing Task for Gradle forked from https://gist.github.com/nobuoka/7614981, fixed for Gradle 4.2 compatilbity, missing imports in JavaAptPlugin.groovy added, option to skip metadataGeneration for a subproject in a multi-project build added.
// java-apt プラグインを使うサンプル
apply plugin: 'java'
apply plugin: 'java-apt'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath gradleApi()
}
}
ext {
generateMeta = true
}
dependencies {
compile 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.7.0'
compile("org.eclipse.persistence:javax.persistence:2.1.1") {
force = true
}
apt 'org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor:2.7.0'
}
procJava {
def persistencexml = new File(project.projectDir, 'src/main/resources/META-INF/persistence.xml').absolutePath
source = project.compileJava.source
processor = 'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor'
apOptions = 'eclipselink.persistencexml=' + persistencexml
}
# buildSrc/src/main/resources/META-INF/gradle-plugins/java-apt.properties
implementation-class=JavaAptPlugin
// buildSrc/src/main/groovy/JavaAptPlugin.groovy
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.file.CompositeFileTree
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.compile.JavaCompile
class JavaAPT extends DefaultTask {
private File _destinationDir
def getDestinationDir(dir) { _destinationDir }
void setDestinationDir(dir) {
_destinationDir = dir
outputs.dir _destinationDir
}
private CompositeFileTree _source
def getSource() { _source }
void setSource(CompositeFileTree src) {
_source = src
inputs.files src
}
String processor
String apOptions
@TaskAction
void process() {
if (project.properties.get('generateMeta') == true) {
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 defaultAptDestination = "build/generated-sources/metamodel"
it.dependsOn project.configurations.compile
project.tasks.compileJava.dependsOn it
project.sourceSets.main.java.srcDirs << defaultAptDestination
destinationDir = new File(project.projectDir, defaultAptDestination)
}
project.configurations {
apt {
description "for annotation processors"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment