Unable to use Intellij with a generated sources folder

Related question How to configure IntelliJ IDEA and/or Maven to automatically add directories with Java source code generated using jaxb2-maven-plugin?

I have a custom plugin that generates sources under target/generated-sources (Note no toolname here). So I get sources like target/generated-sources/com/mycompany...etc.

This format cannot be changed at all, so will I be able to configure Intellij into adding it as a source folder. As of now, I can see that Intellij has added target/generated-sources/com as the source folder.

Please note that I do not have the option of configuring the plugin !

UPDATE 1: I do not agree with the fact that I MUST put my generated sources under a tool name folder. It may be a good convention, but if I have only one generator there is no need for me to put it there? Again, in my pom.xml I have a resources section which clearly indicates that target/generated-sources should be treated as a source folder. This works perfectly fine in Eclipse so I have no idea why Intellij would not respect my settings.

TL;DR -> When I put target/generated-sources in the resource section of pom.xml why is Intellij overzealous to add target/generated-sources/com to the classpath?

166083 次浏览

不管是谁写的这个插件,都搞砸了,这不是解决问题的方法!

任何变通方法都是一个巨大的缺陷,使插件开发人员意识到他的 bug。

抱歉,这是唯一的办法。


好的,这里有一个窍门,在你的插件执行之后,使用 antrun 插件将目录移动到其他地方:

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>process-sources</phase>
<configuration>
<target>
<move todir="${project.build.directory}/generated-sources/toolname/com"
overwrite="true">
<fileset dir="${project.build.directory}/generated-sources/com"/>
</move>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

在这个例子中,toolname应该被唯一标识创建代码的插件的任何东西替换,而 com代表创建的包的根。如果您有多个包根,那么您可能需要多个 <move>任务。

但是如果插件添加文件夹作为源文件夹,那么你就完蛋了。

也许您可以在生成源阶段添加一个步骤来移动文件夹?

您只需更改项目结构,将该文件夹添加为“源”目录即可。

项目结构→模块→ 单击 generated-sources文件夹并使其成为 sources文件夹。

或者:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</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>

解决问题

Go to 工程项目结构 - Modules - 源文件夹 and find the target/generated-sources/antlr4/com/mycompany - click 编辑属性 and set 包的前缀 to com.mycompany.

这就是为什么我们可以在源文件目录上设置 包的前缀的原因。


Different but related problem 给你

使用分级,当你刷新分级设置时,项目设置将被清除。相反,您需要在 build.gradle 中添加以下代码行(或类似的代码行) ,我使用 kotlin 如下:

sourceSets {
main {
java {
srcDir "${buildDir.absolutePath}/generated/source/kapt/main"
}
}
}

我使用的 Maven (SpringBoot 应用程序)解决方案是:

  1. 右键单击项目文件夹
  2. 选择 玛文
  3. 选择 生成源和更新文件夹

然后,Intellij 自动将生成的源导入到项目中。

I had the same issue with Eclipse a couple of months ago when importing my project. Now I had the same with intelliJ. 下面是有人在 IntelliJ 中帮助我解决这个问题的方法:

菜单 = > 视图 = > 工具窗口 = > Maven 项目 在 spring _ user value = > Run Configuration 中,选择 clean install。 这应该做一个干净的安装,在这之后,您应该能够看到类 enter image description here

唯一的工作条件,经过几次尝试,是删除隐藏。创意文件夹,然后从 Intellij 重新导入

通过删除模块设置中的“排除”(右键单击项目,“打开模块设置”)解决了这个问题。 enter image description here

我想更新在 大肖恩早些时候的评论,但因为这是我第一次评论,应用程序不允许我。

Nonetheless, I am using eclipse and after I added the below mention code snippet to my pom.xml as suggested by Dashun and I ran the mvn clean package to generate the avro source files, but I was still getting compilation error in the workspace.

我右键单击 project _ name-> maven-> update project 并更新了该项目,它将 target/generated-source 作为源文件夹添加到我的 eclipse 项目中。

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</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>

我运行 mvn generate-resources,然后将/target/generated-resources 文件夹标记为“ source”(项目结构-> 项目设置-> 模块-> Select/target/generated-resources-> 单击蓝色的“ Source”图标。

对于那些仍在努力修复 IntelliJ 没有接收到的生成源的人来说,

一个原因可能是生成的文件大小太大,无法加载,那么您需要在“自定义 IntelliJ IDEA 属性”(在菜单帮助下)中添加以下代码行

idea.max.intellisense.filesize=7500

I wanted to ask that sometimes even if the pom is correct, Intellij does not see the directory. One way to solve this problem is to close the project, remove the .idea directory and reopen the project. The 'generated-sources' is added to the classpath (the directory is in blue (it was in red) and we see a star ('*') on the left).

Automatically mark directory as a generated source  运行 mvn clean install 时,如果希望将目录标记为生成的源,可以使用此解决方案

很简单:

只需右键单击目录并将其标记为生成的源根。

enter image description here