【问题标题】:Why doesn't NetBeans IDE see the generated sources?为什么 NetBeans IDE 看不到生成的源?
【发布时间】:2012-04-08 12:02:51
【问题描述】:

我有一个 Maven 构建的网络应用程序,它在后端使用 JPA 2.0。 JPA 提供程序是 EclipseLink 2.3.2。

当我构建项目(并且部署成功)时,它会在目录中构建 JPA 元模型

${basedir}/target/generated-sources/annotations/

然而 IDE 看不到那里定义的类。到处都是带有感叹号的小红点。但是我可以在 Projects 窗口中导航到这些文件并打开生成的源文件。

这种情况发生在其他人身上吗?有人知道解决方法吗?

更新:

作为一种变通方法,我发现我可以退出 NetBeans,删除 NetBeans 缓存目录,然后重新启动。这会强制 NetBeans 重新构建缓存,然后这些类再次变得可见。我应该向 NetBeans 错误跟踪器提交错误吗?我无法想出一个测试用例来实现它,但它经常发生。

【问题讨论】:

  • 在 Eclipse 中,您可以定义多个源文件夹 - 尝试添加它。
  • 是的,这是一个错误,Netbeans 应该会自动刷新。
  • 这仍然是 2015 年使用 Netbeans 8.0.2 的 bug。想知道为什么每个人都使用 Eclipse 或 Intellij。
  • 这仍然是 Netbeans 8.1 中的一个错误,2016 年 10 月 28 日,有什么帮助吗?
  • 此错误在 NetBeans 11.3 中再次出现。但只有一些元模型类无法识别。如果我更改该实体类(添加空间并删除并保存文件并编译它,则红色标志会在该metemodel引用上消失。多年来,这在NetBeans中非常烦人。一种索引问题,但我还没有找到任何解决方案.

标签: maven netbeans eclipselink netbeans-7


【解决方案1】:

如果你去项目属性/源有一个说明:你需要在下生成源

${basedir}/target/generated-sources/FOOBAR 

FOOBAR 是您的插件的名称。

【讨论】:

  • 我看到我生成的源和我的所有文件都使用“Example_”命名法,但只在 Netbeans 的文件选项卡中,但我的 ide 仍然没有检测到它们要导入或在我的java 文件,并在我的项目选项卡中被检测为被忽略。请帮忙。
【解决方案2】:

阅读@jeqo 的答案后,我通过手动重命名测试了是否:

 "${project.build.directory}/generated-sources/annotations" to ".../generated-sources/hibernate-jpamodelgen" 

会对 Nebeans 产生影响(我在 ubuntu 16.04 上使用 v8.2)。

一切都像魅力一样。

然后我修改pom文件如下:

1) 移除了“org.hibernate: hibernate.jpamodelgen”依赖。

2) 配置maven-compiler-plugin如下:

   <plugin>
    <groupId>>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
      <compilerArgument>-proc:none</compilerArgument>        
    </configuration>
  </plugin>
  • 这两个步骤是为了确保 hibernate-jpamodelgen 确实 仅通过将其添加到项目依赖项中就不会在自动驾驶仪上运行 列表。请参考JPA Static MetaModel Generator doc.

3) 添加了以下带有配置的插件

  <plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
      <execution>
        <id>process</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-sources</phase>
        <configuration>
          <processors>
            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
          </processors>
          <defaultOutputDirectory>${project.build.directory}/generated-sources/hibernate-jpamodelgen/</defaultOutputDirectory>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>5.2.9.Final</version>
      </dependency>
    </dependencies>
  </plugin>

此配置直接来自 Hibernate JPA 静态元模型生成器文档页面,但以下行除外:

<defaultOutputDirectory>${project.build.directory}/generated-sources/hibernate-jpamodelgen/</defaultOutputDirectory>

这一行只是在以 maven 插件名称命名的目录中生成元模型。从这一点开始,我让所有 Netbeans 引用在设计时都能正常工作,就好像生成的类在 src 目录子树中一样。

希望这会有所帮助,

J

【讨论】:

    【解决方案3】:

    Netbeans 有时会出现刷新问题。也许清理并重建项目并重新启动 Netbeans?

    【讨论】:

    • 我试过了,还是一样,我什至在我的 proyect 的 pom 目录中尝试了“mvn clean install -Dmaven.test.skip=true”,但什么也没有。
    【解决方案4】:

    今天我在这个主题上做了更多的实验,因为它对我来说也很烦人。最后我意识到这只是一个与 NetBeans 如何处理索引类有关的问题。这不是目标目录名的问题,也不是项目的问题。这只是 NetBeans 的错误。所以我也提出了一个问题,希望 NetBeans 团队能尽快提供最终解决方案。你可以在这里看到我的票https://issues.apache.org/jira/browse/NETBEANS-4191

    在我的环境中,在 Windows 10 (1607) 下使用带有 openJDK 1.8.0_242-b08 和 apache-maven 3.6.3 版本的 NetBeans 11.3 (x64)。

    但在最终解决方案出现之前,我所做的就是解决符号未找到问题的变通方法。

    我在我的 pom 文件中添加了一个配置文件部分:

    <profile>
    <id>nb-modelgen-fix</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>modelgen-touch-files</id>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <touch>
                                    <fileset id="model.elements" dir="src/main/java" includes="**/*.java">
                                        <containsregexp expression="(@Entity|@MappedSuperclass|@Embeddable)" casesensitive="yes" />
                                    </fileset>
                                </touch>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    </profile>
    

    我正在使用以下简单的解决方案在我的项目中生成元模型类:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <annotationProcessors>
                <annotationProcessor>
                    org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
                </annotationProcessor>
            </annotationProcessors>
            <compilerArgs>
                <arg>-Aeclipselink.persistenceunits=MY-PU</arg>
            </compilerArgs>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    

    当然还有一个 maven-build-helper 将生成的源文件夹添加到项目中:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
            <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>${project.build.directory}/generated-sources/annotations</source>
                        <source>${project.build.directory}/generated-sources/wsimport</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    我还在 pom.xml 所在的位置创建了一个名为 nbactions.xml 的文件,其中包含以下内容(仅在 NetBeans IDE 中激活此配置文件)

    <?xml version="1.0" encoding="UTF-8"?>
    <actions>
    <action>
        <actionName>rebuild</actionName>
        <packagings>
            <packaging>*</packaging>
        </packagings>
        <goals>
            <goal>clean</goal>
            <goal>install</goal>
        </goals>
        <activatedProfiles>
            <activatedProfile>nb-modelgen-fix</activatedProfile>
        </activatedProfiles>
    </action>
    </actions>
    

    它有什么作用?当您在 NetBeans IDE 中执行“清理和构建”操作时,它会激活一个任务(使用 maven-antrun-plugin 轻松实现),只需简单地触摸所有使用 @Entity、@MappedSuperClass 或 @Embeddable 注释的 JPA 即可元模型世代。我已将此任务附加到安装阶段,但它在其他阶段也同样有效。看起来这样 NetBeans 会唤醒并弥补元模型类的缺失索引。

    您可以在我的 NetBeans 问题单中了解更多信息。

    我希望这可以为其他人节省时间。

    【讨论】:

    • 与 Netbeans 11.3 相同(运行 Corretto 11.0.7 @ Ubuntu 18.04)
    【解决方案5】:

    如果您正在使用 jaxws,请确保将 &lt;sourceDestDir&gt; 节点添加到相应 pom.xml 中的 jaxws 插件“工件”的 &lt;configuration&gt; 部分。例如:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>dojaxws</goal>
                        </goals>
                        <configuration>
                            <sourceDestDir>${project.build.directory}/generated-sources/jaxws</sourceDestDir>
                            ....
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <wsdlDirectory>src/main/resources/com/mystuff/ws</wsdlDirectory>
                    <bindingDirectory>src/jaxws/binding</bindingDirectory>
                    <target>2.0</target>
                </configuration>
            </plugin>
    

    如上所述,正如 netbeans 所指出的,您必须使用附加了“插件”名称的 generate-sources 路径。希望以上内容能够阐明“插件名称”的含义以及应该如何让 jaxws 将生成的源放在 netbeans 需要它们的位置。显然,每个插件的“配置”部分会有所不同...节点&lt;sourceDestDir&gt; 是 jaxws 所必需的,其他插件可能会使用其他东西。

    【讨论】:

      【解决方案6】:

      对我来说,在我将 &lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt; 添加到 pom.xml&lt;properties&gt; 之后它就起作用了,例如:

      <properties>
          <maven.compiler.source>1.8</maven.compiler.source>
          <maven.compiler.target>1.8</maven.compiler.target>
          <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <jakartaee>8.0</jakartaee>
      </properties>
      

      但我没有解释为什么。

      【讨论】:

        猜你喜欢
        • 2020-11-13
        • 2016-01-14
        • 1970-01-01
        • 1970-01-01
        • 2010-11-08
        • 1970-01-01
        • 2017-12-21
        • 2010-10-19
        • 2021-01-10
        相关资源
        最近更新 更多