为什么NetBeans IDE看不到生成的源代码?

21

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

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

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

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

这是否发生在其他人身上? 是否有人知道解决方法?

更新:

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


在Eclipse中,您可以定义多个源文件夹 - 尝试添加它。 - Andrzej Jozwik
3
是的,这是一个错误,Netbeans应该自动刷新。 - eskatos
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Giovanni Toraldo
这在Netbeans 8.1中仍然是一个bug,2016年10月28日,有任何帮助吗? - Omar Ayala
这个错误再次出现在NetBeans 11.3中。但是只有一些元模型类没有被识别。如果我更改那个实体类(添加空格并删除并保存文件并编译它),那么该元模型引用上的红旗就会消失。这在NetBeans中已经非常令人恼火了很多年。这是一种索引问题,但我还没有找到任何解决方案。 - Miklos Krivan
6个回答

6

如果您前往项目属性/源,那么有一个关于此事的注释:您需要在下面生成源代码

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

其中FOOBAR是插件的名称。

在这里输入图片描述


我在Netbeans的文件选项卡中看到了以“Example_”命名的所有生成源和文件,但它们仍未被我的IDE检测到以便导入或在我的Java文件中使用,并且在我的项目选项卡中被检测为已忽略。请帮忙解决。 - Omar Ayala

4

在阅读了@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静态元模型生成器文档

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


2
有时候Netbeans会出现刷新问题。也许可以尝试清理并重新构建项目,然后重启Netbeans?

1
我尝试了这个方法,但结果还是一样。我甚至在我的项目pom目录下尝试了“mvn clean install -Dmaven.test.skip=true”,但仍然没有用。 - Omar Ayala

1
今天我在这个主题上做了更多的实验,因为这对我来说也很烦人。最终我意识到这只是一个与NetBeans处理索引类有关的问题。这不是目标目录名称的问题,也不是项目的问题。这只是NetBeans的错误。所以我也创建了一个问题,希望NetBeans团队能尽快带来最终的解决方案。你可以在这里看到我的工单https://issues.apache.org/jira/browse/NETBEANS-4191 在我的环境中,使用NetBeans 11.3(x64),openJDK 1.8.0_242-b08和apache-maven 3.6.3版本,在Windows 10(1607)下运行。
但在最终解决方案到来之前,以下是我作为解决符号未找到问题的解决方法,我已将配置文件添加到我的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构建助手将生成的源文件夹添加到项目中:
<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中执行“Clean and Build”操作时,它会激活一个任务(使用maven-antrun-plugin轻松实现),该任务仅对所有带有@Entity、@MappedSuperClass或@Embeddable注释的JPA进行简单的触摸,这些是元模型生成的源。我已将此任务附加到安装阶段,但在其他阶段也可以正常工作。看起来这样NetBeans就会为元模型类创建缺失的索引。

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

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


我也是使用Netbeans 11.3(在Ubuntu 18.04上运行Corretto 11.0.7)的。 - rmuller

0
如果您正在使用jaxws,请确保在适当的pom中将<sourceDestDir>节点添加到jaxws插件“artifact”的<configuration>部分。例如:
        <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需要的位置。显然,“配置”部分对于每个插件都是不同的...节点<sourceDestDir>对于jaxws是必需的,其他插件可能会使用其他内容。

0

对我来说,在pom.xml<properties>中添加<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>后,它就可以工作了,例如:

<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>

但我没有解释为什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接