问题背后的故事是,我有一个 Spring-Graphql 项目,其中 GraphQL 实体由 this 插件生成,生成的代码位于
${project.dir}/target/generated-sources
下。 IntelliJ 默认情况下会看到该目录,但 NeoVim 和 Eclipse 不会,因此它们无法解析那里的类,并且屏幕上的整个代码都是红色的。
我在互联网上读到我需要
build-helper-maven-plugin
并配置如下。
当我用 NeoVim 打开 java 文件(它使用 Eclipse 的 lang 服务器进行智能感知和其他魔法)时,情况仍然相同。当我检查项目根中的语言服务器生成的
.classpath
时,我发现 target/generated-sources
目录未在那里列出。这意味着我要么配置不好插件(它按预期编译和运行测试),要么 Eclipse 和 Maven 配置之间存在分歧,如我上面所述。
NeoVim 需要什么配置(将是 jdtls 设置,最终是 Eclipse lang 服务器配置)才能查看生成的源目录?
这里是一个演示该问题的示例项目。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
.classpath
Eclipse 语言服务器生成的文件
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="target/generated-sources">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" 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>
<classpathentry kind="output" path="target/classes"/>
</classpath>
解决方案是进一步指定包含的目录,如下所示。 这样语言服务器就可以拾取它并查看其中有什么,并且 NeoVim 工作得很好。
使用 Eclipse 进行实验的最简单方法。如果 mvn 设置在那里工作,很可能会与 NeoVim 一起工作,因此它们使用相同的语言服务器。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources/graphql/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>