How to prevent JDT LS (via m2e) from adding generated-sources APT folders and org.eclipse.jdt.apt prefs to an Eclipse+Maven project in VS Code.
If you open a Maven Java project in Visual Studio Code that also contains Eclipse project metadata (.project, .classpath, .settings/…), you might notice that VS Code’s Java tooling (JDT Language Server) “helpfully” edits your Eclipse files.
In particular, it may keep re-inserting entries like these into your .classpath:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<classpathentry kind="src" path="target/generated-sources/annotations"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> <attribute name="ignore_optional_problems" value="true"/> <attribute name="m2e-apt" value="true"/> </attributes> </classpathentry> <classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> <attribute name="ignore_optional_problems" value="true"/> <attribute name="m2e-apt" value="true"/> <attribute name="test" value="true"/> </attributes> </classpathentry> |
…and it may also create this file:
.settings/org.eclipse.jdt.apt.core.prefs
|
1 2 3 |
eclipse.preferences.version=1 org.eclipse.jdt.apt.aptEnabled=false |
VS Code Java support is powered by Eclipse JDT Language Server (JDT LS). When it detects Eclipse metadata (.project / .classpath), it will often use Eclipse-style project configuration and keep it synchronized.
For Maven projects, JDT LS relies on m2e (the “Maven integration for Eclipse”), and in many setups m2e-apt is present as well. m2e-apt is the component that manages annotation processing (APT) integration and, as part of that, it adds the standard “generated sources” folders into .classpath.
I find that very annoying!
If your project doesn’t use annotation processing and you don’t want these Eclipse files constantly modified, and you remove the entries, Visual Studio Code will re-add them when you open the projects in Visual Studio Code. If you open the projects from Eclipse and you “update” the Maven projects, Eclipse will remove the entries… and so on and so forth!
Here’s how to fix things for good.
The fix: disable m2e-apt at the project level
m2e-apt supports overriding its activation per-project using a Maven property:
|
1 |
<m2e.apt.activation>disabled</m2e.apt.activation> |
Add this to your pom.xml
Put it under the regular Maven <properties> section:
|
1 2 3 4 5 6 |
<project> ... <properties> <m2e.apt.activation>disabled</m2e.apt.activation> </properties> </project> |
That’s it. After this, m2e-apt will stop treating your project as something it should manage, and VS Code/JDT LS will no longer keep reintroducing those APT-related .classpath entries.
Note: The documentation mentions a “settings section” in the POM. There is no
settingselement inpom.xml; Maven “settings” live in~/.m2/settings.xml. In the POM, this is implemented via a property, soproperties(or a profile’sproperties) is the right place.
Refresh VS Code so it stops regenerating the files
After editing the POM, VS Code may still have cached the project configuration. Do this once:
- Open the Command Palette
- Run: Java: Clean Java Language Server Workspace
- Let the Java server restart and re-import the project
Then you can delete the unwanted entries/files one last time:
- Remove the APT-related
classpathentry … m2e-apt …blocks from.classpath - Delete
.settings/org.eclipse.jdt.apt.core.prefsif you don’t want it around
They should not come back.
If you only want to disable it in certain environments, you can place the property in a Maven profile or in the pom file of a single project.
Happy (quiet) classpaths! 😉