无法将Intellij与生成的源文件夹一起使用

时间:2011-03-02 16:46:07

标签: java maven-2 intellij-idea

相关问题 How to configure IntelliJ IDEA and/or Maven to automatically add directories with Java source code generated using jaxb2-maven-plugin?

我有一个自定义插件,可以在target/generated-sources下生成源代码(请注意此处没有工具名称)。所以我获得了target/generated-sources/com/mycompany ......等来源。

这种格式根本无法更改,因此我可以将Intellij配置为将其添加为源文件夹。截至目前,我可以看到Intellij已添加target/generated-sources/com作为源文件夹。

请注意,我没有配置插件的选项!

更新1 :我不同意我必须将生成的资源放在工具名称文件夹下。这可能是一个很好的约定,但如果我只有一台发电机,我就没有必要把它放在那里?同样,在我的pom.xml中,我有一个resources部分,清楚地表明应将target/generated-sources视为源文件夹。这在Eclipse中运行得非常好,所以我不知道为什么Intellij不尊重我的设置。

TL; DR - >当我将target/generated-sources放在pom.xml的资源部分时,为什么Intellij过于热心地将target/generated-sources/com添加到类路径中?

12 个答案:

答案 0 :(得分:104)

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

项目结构→模块→单击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>

答案 1 :(得分:25)

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

  1. 右键单击项目文件夹
  2. 选择 Maven
  3. 选择生成来源和更新文件夹
  4. 然后,Intellij自动将生成的源导入项目。

答案 2 :(得分:16)

使用gradle时,只要刷新平移设置,项目设置就会被清除。相反,你需要在build.gradle中添加以下行(或类似的),我正在使用kotlin:

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

答案 3 :(得分:13)

修复

转到项目结构 - 模块 - 源文件夹并找到target/generated-sources/antlr4/com/mycompany - 点击编辑属性并将包前缀设置为com.mycompany

这正是我们可以在源目录上设置包前缀的原因。

不同但相关的问题here

答案 4 :(得分:3)

谁写了那个插件搞砸了很多时间。这不是办法!

任何解决方法都是一个巨大的黑客攻击,让插件开发人员意识到他的错误。

抱歉,这是唯一要做的事。


好的,这是一个黑客,直接在你的插件执行后,使用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>任务。

但是如果插件将文件夹添加为源文件夹,那么你就搞砸了。

答案 5 :(得分:1)

也许你可以在移动文件夹的generate-sources阶段添加一个步骤?

答案 6 :(得分:1)

I had the same issue with Eclipse a couple of months ago when importing my project. Now I had the same with intelliJ. Here is how someone helped me to solve this in IntelliJ:

Menu => View => Tools windows => Maven Project In the spring_user value => Run Configuration, choose clean install. This should do a clean install and after this you should be able to see the classes enter image description here

答案 7 :(得分:1)

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

答案 8 :(得分:0)

经过几次尝试,唯一的工作条件是从根项目文件夹中删除隐藏的.idea文件夹,然后从Intellij重新导入它。

答案 9 :(得分:0)

我想更新DaShaun先前发表的评论,但由于这是我第一次发表评论,因此应用程序不允许我这么做。

不过,我正在使用eclipse,并按照Dashun的建议将以下提到的代码段添加到pom.xml中后,我运行了mvn clean程序包来生成avro源文件,但是我仍然遇到了编译错误工作区。

我右键单击project_name-> maven->更新项目并更新了该项目,这将目标/生成源作为源文件夹添加到我的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>

答案 10 :(得分:0)

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

答案 11 :(得分:0)

对于仍然难以修复 IntelliJ 无法获取的生成源代码的人,

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

idea.max.intellisense.filesize=7500