Skip to content

Instantly share code, notes, and snippets.

@mr-archano
Created July 11, 2015 20:14
Show Gist options
  • Save mr-archano/0529a886aaab2bd56ab9 to your computer and use it in GitHub Desktop.
Save mr-archano/0529a886aaab2bd56ab9 to your computer and use it in GitHub Desktop.
APT plugin for plain Java modules
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.plugins.ide.idea.IdeaPlugin
import org.gradle.tooling.BuildException
class JavaAptPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
def aptConfiguration = project.configurations.create 'javapt'
project.afterEvaluate {
if (!project.plugins.hasPlugin('java')) {
throw new BuildException("Project $project.name isn't a java project, can't apply javapt!", null)
}
project.apply(plugin: IdeaPlugin)
configureProject(project, aptConfiguration)
}
}
private void configureProject(Project project, def aptConfiguration) {
File aptOutputDir = project.file('gen/javapt')
project.sourceSets.main.compileClasspath += aptConfiguration
// register generated sources with AndroidStudio
project.idea.module {
scopes.PROVIDED.plus += [aptConfiguration]
sourceDirs += aptOutputDir
generatedSourceDirs += aptOutputDir
}
//tweak compileJava task for annotation processing and clean generated output folder before compiling
project.compileJava {
outputs.file aptOutputDir
options.compilerArgs += ['-s', aptOutputDir.absolutePath, '-processorpath', aptConfiguration.asPath]
doFirst {
aptOutputDir.deleteDir()
aptOutputDir.mkdirs()
}
}
// clean generated code when cleaning
project.clean << {
aptOutputDir.deleteDir()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment