Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is Annotation Processing Supported for Gradle project? #1039

Closed
chialunwu opened this issue Aug 21, 2019 · 12 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#2319
Closed

Is Annotation Processing Supported for Gradle project? #1039

chialunwu opened this issue Aug 21, 2019 · 12 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#2319
Assignees

Comments

@chialunwu
Copy link

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?

@chialunwu chialunwu changed the title How to make Annotation Processing work in Gradle project? Is Annotation Processing Supported for Gradle project? Aug 21, 2019
@fbricon
Copy link
Collaborator

fbricon commented Aug 21, 2019

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 ;-)

@chialunwu
Copy link
Author

@fbricon I'll try. Thanks!

@fvclaus
Copy link
Contributor

fvclaus commented Apr 21, 2022

The information is outdated. It took me a few hours to get this working:

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:

annotation_processor_path

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.

@hannah23280
Copy link

hannah23280 commented Jul 6, 2022

@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.

@VitaliyKulikov
Copy link

i have following error:

Missing Gradle project configuration file: .settings/org.eclipse.buildship.core.prefs

@jsilverdev
Copy link

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

@VitaliyKulikov
Copy link

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

@snjeza
Copy link
Contributor

snjeza commented Nov 9, 2022

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

@snjeza snjeza self-assigned this Nov 9, 2022
@snjeza
Copy link
Contributor

snjeza commented Nov 9, 2022

@fbricon I have fixed the issue. See snjeza/autovalued-gradle@f09e3d5

@fbricon
Copy link
Collaborator

fbricon commented Nov 10, 2022

Nope?

gradle-ap.mp4

@snjeza
Copy link
Contributor

snjeza commented Nov 10, 2022

@fbricon you have faced #2528
Could you try to set

"java.import.generatesMetadataFilesAtProjectRoot": true,

or test VS Code master.

anon

@jdneo
Copy link
Collaborator

jdneo commented Nov 11, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants