我有一个使用 JPA 2.0 元模型的 Maven 项目。我将 M2e 配置为在其构建生命周期中使用 maven-processor-plugin 和 build-helper-maven-plugin。结果是:在target/generated-sources/annotations中生成了JPA 2.0 Metamodel类,并且全部编译成功(我查看了生成的.war文件,生成的类都在里面)。但是,Eclipse还是报了编译错误。
我的环境:JDK 1.6 更新 43 x64,Eclipse Juno SR2,M2e 1.4.0。
我的配置如下:
对于 build-helper-maven-plugin (in <build><plugins>
):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</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>
</sources>
</configuration>
</execution>
</executions>
</plugin>
对于 maven-processor-plugin (in <build><plugins>
):
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations/</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.2.0.Final</version>
</dependency>
</dependencies>
</plugin>
对于 maven-compiler-plugin (in <build><plugins>
):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>
-proc:none
</compilerArgument
</configuration>
</plugin>
对于 m2e (in <build>
):
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<versionRange>1.8</versionRange>
<goals>
<goal>add-source</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute/>
</action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<versionRange>2.2.4</versionRange>
<goals>
<goal>process</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
实体类:
@Entity
public class Widget implements Serializable {
// ...
}
元模型类:
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Widget.class)
public abstract class Widget_ {
// ...
}
使用生成的元模型类的类:
public class WidgetService {
public void processWidgets() {
Class<Widget_> clazz = Widget_.class; // Compilation error here - Widget_ cannot be resolved to a variable
}
}
我已经右键单击 > Maven > 更新项目... > 从 pom.xml 更新项目配置,但 .classpath 文件仍然不包含生成源文件夹中的任何条目。
任何人都可以给我一个提示吗?谢谢你。