I have a Maven project that uses JPA 2.0 Metamodel. I configured M2e to use maven-processor-plugin and build-helper-maven-plugin during its build lifecycle. The results were that: JPA 2.0 Metamodel classes were generated in target/generated-sources/annotations, and all of them were compiled successfully (I checked the generated .war file, the generated classes were all there). However, Eclipse still reported compilation errors.
My environment: JDK 1.6 update 43 x64, Eclipse Juno SR2, M2e 1.4.0.
My configuration is as follows:
For 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>
For 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>
For 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>
For 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 class:
@Entity
public class Widget implements Serializable {
// ...
}
Metamodel class:
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Widget.class)
public abstract class Widget_ {
// ...
}
Class that uses the generated metamodel class:
public class WidgetService {
public void processWidgets() {
Class<Widget_> clazz = Widget_.class; // Compilation error here - Widget_ cannot be resolved to a variable
}
}
I already right clicked > Maven > Update Project... > Update Project Configuration from pom.xml, but the .classpath file still didn't contain any entry from generated-sources folder.
Could any one give me a hint? Thank you.