3

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.

awiles
  • 31
  • 1
  • 2

1 Answers1

4

Don't know if you solved your problem, but just in case:

I faced the same situation after re-installing Eclipse IDE and figured out that "Annotation Processing" was disabled for the project.

Right click on the project -> Properties -> Java Compiler -> Annotation Processing

Check enable and specify a generated source directory.

This did the trick for me. No more complaints from Eclipse and autoComplete on foo_.whatever worked again.

Gray
  • 7,050
  • 2
  • 29
  • 52
MattOverF.
  • 69
  • 7
  • I'm facing very similar issue in Eclipse. In my case, module1 depends on another module (module2) that has hibernate entity classes. In module1, the auto generated entity classes (with an underscore at the end) are referenced, however, eclipse gives compilation error. However, the maven build from command prompt works fine. Any suggestions please? Thanks. – Prasanna Hegde Jul 10 '21 at 12:57