Skip to content

Instantly share code, notes, and snippets.

@tbroyer
Last active October 30, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbroyer/406b5c8b1d5c2b401d64c8e1ad84a3ce to your computer and use it in GitHub Desktop.
Save tbroyer/406b5c8b1d5c2b401d64c8e1ad84a3ce to your computer and use it in GitHub Desktop.
Goodbye `net.ltgt.apt-idea` 👋
// Put this file in ~/.gradle/init.d/
//
// It will automatically configure Java projects (not Groovy) with for use in IntelliJ IDEA,
// by declaring options.annotationProcessorGeneratedSourcesDirectory as generated sources
// (assumes "Delegate IDE build/run actions to gradle" and "Create separate module per source set";
// only works for 'main' and 'test' source sets).
// The options.annotationProcessorGeneratedSourcesDirectory still needs to be configured in builds
// (see https://github.com/gradle/gradle/issues/4956). This script is thus mainly a workaround for
// the IDEA issue https://youtrack.jetbrains.com/issue/IDEA-182577
//
// In Gradle < 4.6, falls back to applying net.ltgt.apt-idea whenever net.ltgt.apt is applied
import org.gradle.util.GradleVersion;
if (Boolean.getBoolean("idea.active")) {
def HAS_ANNOTATION_PROCESSOR_PATH = GradleVersion.current() >= GradleVersion.version("4.6")
allprojects { project ->
project.apply plugin: 'idea'
if (HAS_ANNOTATION_PROCESSOR_PATH) {
project.plugins.withType(JavaPlugin) {
project.afterEvaluate {
project.idea.module {
def mainGeneratedSources = project.tasks["compileJava"].options.annotationProcessorGeneratedSourcesDirectory
if (mainGeneratedSources) {
sourceDirs += mainGeneratedSources
generatedSourceDirs += mainGeneratedSources
}
def testGeneratedSources = project.tasks["compileTestJava"].options.annotationProcessorGeneratedSourcesDirectory
if (testGeneratedSources) {
testSourceDirs += testGeneratedSources
generatedSourceDirs += testGeneratedSources
}
}
}
}
} else {
project.plugins.withId("net.ltgt.apt") {
try {
project.apply plugin: "net.ltgt.apt-idea"
project.plugins.withType(JavaPlugin) {
project.afterEvaluate {
project.idea.module.apt.addAptDependencies = false
}
}
} catch (UnknownPluginException) {}
}
}
}
}
@tbroyer
Copy link
Author

tbroyer commented Oct 29, 2018

To configure options.annotationProcessorGeneratedSourcesDirectory in your build, you can use the net.ltgt.apt plugin, or something like the following (assuming Gradle 4.9+ here):

Groovy DSL
sourceSets.all { sourceSet ->
  tasks.named(sourceSet.compileJavaTaskName).configure {
    options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/source/apt/${sourceSet.name}")
  }
}
Kotlin DSL
sourceSets.all {
  tasks.named<JavaCompile>(compileJavaTaskName).configure {
    options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/source/apt/${this@all.name}")
  }
}

@tbroyer
Copy link
Author

tbroyer commented Oct 30, 2018

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