Gradle Goodness: Enable Compiler Annotation Processing For IntelliJ IDEA
Suppose we have a project where we use Lombok annotations. To use it we must change the compiler configuration for our project and enable annotation processing. We can find this in the Preferences or Settings window under Build, Execution, Deployment | Compiler | Annotation Processors. Here we have a checkbox Enable annotation processing that we must check to use the annotations from IntelliJ IDEA. We can automate this setting with some configuration in our Gradle build file.
In the next example build file we alter the generated IntelliJ IDEA project file using the withXml
hook. We can access the generated XML as a groovy.util.Node
and change the XML.
// File: build.gradle
apply plugin: 'groovy'
apply plugin: 'idea'
repositories {
jcenter()
}
dependencies {
compile('org.projectlombok:lombok:1.16.6')
}
idea {
project {
ipr {
withXml { provider ->
// Get XML as groovy.util.Node to work with.
def projectXml = provider.asNode()
// Find compiler configuration component.
def compilerConfiguration = projectXml.component.find { component ->
component.'@name' == 'CompilerConfiguration'
}
// Replace current annotationProcessing
// that is part of the compiler configuration.
def currentAnnotationProcessing = compilerConfiguration.annotationProcessing
currentAnnotationProcessing.replaceNode {
annotationProcessing {
profile(name: 'Default', default: true, enabled: true) {
processorPath(useClasspath: true)
}
}
}
}
}
}
}
Written with Gradle 2.12 and IntelliJ IDEA 15.