vscode-java
vscode-java copied to clipboard
Is Annotation Processing Supported for Gradle project?
I know there's a wiki page about how to set up for Maven project - https://github.com/redhat-developer/vscode-java/wiki/Annotation-Processing-support-for-Maven-projects.
But how about Gradle project? Is it even possible?
There's no native annotation processor support in Buildship (the Gradle integration for Eclipse project), that we use under the hood. However following the instructions from http://dplatz.de/blog/2018/gradle-apt.html should give you some good pointers (I haven't tried). Then you're welcome to update the wiki for Gradle ;-)
@fbricon I'll try. Thanks!
The information is outdated. It took me a few hours to get this working:
- The plugin mentioned above is now longer maintained and does not work with newer versions of Gradle
- There is a successor, which can be found here: https://plugins.gradle.org/plugin/com.diffplug.eclipse.apt
Here is the configuration, I currently use in my project and have verified in another project:
plugins {
id "com.diffplug.eclipse.apt" version "3.36.1"
}
eclipse {
jdt {
apt {
// You need to run gradle eclipseJdtApt eclipseJdt eclipseFactorypath
// and restart vscode for this to take effect.
genSrcDir = file('src-gen/main/java')
genTestSrcDir = file('src-gen/test/java')
}
}
}
You then have to run gradle eclipseJdtApt eclipseJdt eclipseFactorypath and restart vscode for the changes to take effect. The Gradle task should add the generated source folders to your .classpath file and they should be visible in Java Projects:

After restarting vscode-java, changes made to source files should trigger the annotation processor and update the generated source files.
@fbricon Should I add this to the Gradle Wiki? I believe you have to run this everytime an annotation processor is added, because the generated .factorypath file contains a list of all annotation processors:
<?xml version="1.0" encoding="UTF-8"?>
<factorypath>
<factorypathentry kind="EXTJAR" id="/home/dev/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-configuration-processor/2.6.5/dced3550504fffed49b76972b4c4aed274a623ee/spring-boot-configuration-processor-2.6.5.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/dev/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.18.22/9c08ea24c6eb714e2d6170e8122c069a0ba9aacf/lombok-1.18.22.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="EXTJAR" id="/home/dev/.gradle/caches/modules-2/files-2.1/org.mapstruct/mapstruct-processor/1.4.2.Final/e55bd90d51cddd638c07d5bd89fc7535d4e3d069/mapstruct-processor-1.4.2.Final.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
After adding a new processor, is it still necessary to restart vscode? If yes, would it be possible to watch the file and generate a message for the user, similiar to what is currently displayed, when the user changes settings regarding the language server.
@fvclaus Thank you so much for this. I try it in vscode, it works beautifully! Except that my path is different from yours, and hopefully putting these path under build folder won't cause problem, when i do a clean using gradle.
genSrcDir = file('build/generated/sources/annotationProcessor/java/main')
genTestSrcDir = file('build/generated/sources/annotationProcessor/java/test')
I hope in future, someone would do something about this, without us having to do this workaround.
i have following error:
Missing Gradle project configuration file: .settings/org.eclipse.buildship.core.prefs
i have following error:
Missing Gradle project configuration file: .settings/org.eclipse.buildship.core.prefs
Create the fle .settings/org.eclipse.buildship.core.prefs
and add:
connection.project.dir=
eclipse.preferences.version=1
by the way: such a trick is working well for me in the case of MapStruct. so, without any plugins
ext.generated = "$project.buildDir/generated/sources/annotationProcessor/java/main"
sourceSets.main.java.srcDir generated
Test project - https://github.com/snjeza/autovalued-gradle
@fbricon in chat:
after some tests, I can say it only partially addresses the issue:
- updates to the classpath (eg. bumping the AP version) don’t trigger an update to .factorypath
- manually triggering the eclipse task, the new version is added to .factorypath, but the old one is not removed
@fbricon I have fixed the issue. See https://github.com/snjeza/autovalued-gradle/commit/f09e3d5221533f4d38acadfa16c38d54bb17476e
Nope?
https://user-images.githubusercontent.com/148698/201059810-1e7e849a-392d-403f-9265-0c575bb550c8.mp4
@fbricon you have faced https://github.com/redhat-developer/vscode-java/issues/2528 Could you try to set
"java.import.generatesMetadataFilesAtProjectRoot": true,
or test VS Code master.

Just FYI, I'm working on the Gradle AP support as well.
Instead of requiring users declare com.diffplug.eclipse.apt plugin. I'm trying to get the compilation args from Gradle and use AptConfig to setup the AP - which is a more native and out-of-box way.
com.diffplug.eclipse.apt plugin will always generate those metadata files at project root (it hard codes those files' path)