今天我在这个主题上做了更多的实验,因为这对我来说也很烦人。最终我意识到这只是一个与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问题票证中了解更多信息。
我希望这可以为其他人节省时间。