简体   繁体   中英

Eclipse (JBoss Developer Studio) not automatically building JPA metamodel classes

I have a Maven project set up that has a parent and two child modules:

parent
    business
    support

All of my JPA entities are in the support module. I've decided I want to use JPA metamodel classes to provide type safety when I use the Criteria API. I made changes to the pom.xml for the support module (see below) and the metamodel classes are being created correctly in support/target/metamodel when I build from the command line using mvn clean install . Builds work and deployable artifacts work when deployed.

The issue is that when I follow the instructions here (which mirrors many other places) on how to set up Eclipse to build the metamodel classes, Eclipse doesn't seem to do anything. It creates the support/target/metamodel directory but there's never anything in it. I've cleaned inside Eclipse, from the command line using mvn clean , done Maven->Update Project multiple times but nothing seems to work.

What am I missing?

Here's what my support project's properties look like.

在此处输入图像描述

The relavant section of my support module's pom.xml is:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <!-- source output directory -->
                        <outputDirectory>target/metamodel</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/metamodel</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Version information:

Red Hat JBoss Developer Studio Version: 10.2.0.GA Build id: GA-v20161125-1418-B55 Build date: 20161125-1418

Java 1.8.0_112

Maven (command line): 3.3.9

Maven (Eclipse - m2e): 1.7.120161104-1805 embedded version 3.3.9

This is the setup that has worked for me.

I did not use the org.bsc.maven:maven-processor-plugin, rather set up the maven-compiler-plugin for annotation processing. The issues mentioned in the instructions , ie MCOMPILER-62 and MCOMPILER-66 , are now closed, so I see no reason why to bother with the org.bsc.maven:maven-processor-plugin.

Another notable difference is the "Factory Path", in the Eclipse configuration.

pom.xml

A minimal setup to get started. Note the maven-compiler-plugin configuration.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>scratch</groupId>
    <artifactId>jpa</artifactId>
    <version>0.1.0-SNAPSHOT</version>

    <properties>
        <version.plugin.maven.compiler>3.5</version.plugin.maven.compiler>
        <version.hibernate>5.2.5.Final</version.hibernate>
    </properties>

    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.plugin.maven.compiler}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                    <encoding>UTF-8</encoding>
                    <annotationProcessors>
                        <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
                    </annotationProcessors>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-jpamodelgen</artifactId>
                            <version>${version.hibernate}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${version.hibernate}</version>
        </dependency>
    </dependencies>
</project>

You may want to run it once by hand or download hibernate-jpamodelgen by hand (eg mvn dependency:get -Dartifact=org.hibernate:hibernate-jpamodelgen:5.2.5.Final ), so that the JAR is available for the Eclipse configuration.

Eclipse configuration

  1. Go to project properties (project context menu → Properties or Alt+Enter)
  2. Select "Java Compiler" → "Annotation Processing"
  3. Check the following:
    • "Enable project specific settings"
    • "Enable annotation processing"
    • "Enable processing in editor"
  4. Generated source directory: target/generated-sources/annotations/ (this is where Maven puts them by default)
  5. Select "Factory Path"
  6. Add the following external jars, from your Maven repository ( hibernate-jpamodelgen version will probably vary):
    • org/hibernate/hibernate-jpamodelgen/5.2.5.Final/hibernate-jpamodelgen-5.2.5.Final.jar
    • javax/persistence/persistence-api/1.0.2/persistence-api-1.0.2.jar
  7. Add the generated sources folder (as configured in step 4) to the Java source folders (Java build path → Source tab → Add Folder...)
  8. A clean-build of the project may be required afterwards

This will work for eclipse 2019-06 or later versions:

According to Jboss documentation, it is not required to explicitly set the processor plugin, since the metamodel classes are created automatically on Maven build in \\target\\generated-sources\\annotations project's folder (since JDK 1.6).

Thus, you only need to make sure the Annotation Processing refers to this target folder, as in here:

在此处输入图片说明

in case you are going to use a recent eclipse with Java 11, you will meet the issue that the javamodel is not generated even specifying the hibernate-jpamodelgen in annotation processing of eclipse. If you check the error tabs you will see this: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

this is due the fact that it has been removed in Java 11, so to fix it you should add also the jar jaxb-api on the factory path with hibernate-jpamodelgen.

I leave this here for anyone that could meet the same problems I had (and for me when I search again and have forgot about this)

The easiest way to fix this issue is to do a "mvn clean install", which will create the metamodel in the target directory, then use the metamodel generated as the source folder as below

在此处输入图像描述

After you do the above step, your project structure should look like below

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM