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

时间:2012-03-23 22:52:01

标签: maven netbeans eclipselink netbeans-7

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

当我构建项目(并且它成功部署运行)时,它在目录

中构建JPA元模型
${basedir}/target/generated-sources/annotations/

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

这是否会发生在其他人身上,是否有人知道修复方法?

更新:

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

6 个答案:

答案 0 :(得分:6)

如果您转到项目属性/来源,则需要注意:您需要在

下生成源
${basedir}/target/generated-sources/FOOBAR 

其中FOOBAR是插件的名称。

enter image description here

答案 1 :(得分:3)

阅读@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 Static Metamodel Generator文档页面,但以下行除外:

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

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

希望这有帮助,

Ĵ

答案 2 :(得分:2)

有时候Netbeans会让人感到不安。也许清理并重建项目并重新启动Netbeans?

答案 3 :(得分:1)

今天,我对此主题进行了更多实验,因为它对我也很烦人。最后,我意识到这只是与NetBeans如何处理索引类有关的问题。这不是目标目录名称的问题,也不是项目的问题。这只是NetBeans的错误。因此,我也提出了一个问题,希望NetBeans Team能够尽快提出最终解决方案。您可以在这里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发行票证中了解更多内容。

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

答案 4 :(得分: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需要它们的位置。显然,每个插件的“配置”部分都不同...... jaxws需要节点<sourceDestDir>,其他插件可能会使用其他插件。

答案 5 :(得分:0)

对我来说,在将-1添加到 pom.xml <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>中后,它起作用了,例如:

<properties>

但是我没有解释为什么。