Peter Penzov
Peter Penzov

Reputation: 1588

Configure hibernate-jpamodelgen in Maven

I want to configure hibernate-jpamodelgen into Maven pom.xml. I tried this:

<?xml version="1.0" encoding="UTF-8"?>
<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>plugin</groupId>
    <artifactId>org.plugin</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>Plugin</name>
    <url>http://maven.apache.org</url>

    <parent>
         ........
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.4.3.Final</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>datalis_plugin</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.6</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArguments>
                        <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                    </compilerArguments>
                </configuration>
            </plugin>               
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>   
</project>

Full POM: https://pastebin.com/VjucMAYL

But I get error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project org.plugin: Compilation failure
[ERROR] Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found

Do you know how I can fix this issue?

I used this quite: https://docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/

Upvotes: 5

Views: 22800

Answers (6)

Matouš Klugar
Matouš Klugar

Reputation: 60

Proper solution with "maven-compiler-plugin".

If you use version higher than 3.1, use tag compilerArgs.

If you use version lower than 3.1, use tag compilerArguments

Do not forget to delete the appropriate tag not appropriate to your version.

Why "build-helper-maven-plugin" is used as well, check answer from Martin Baumgartner in this thread.

By putting your generated sources into target, you would face the problem within your IDE because the related code can't be found. Therefore you can add the build-helper-maven-plugin to dynamically add the folder from the target directory

 <build>
    <directory>${project.basedir}/target</directory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.plugin.compiler.version}</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.hibernate.orm</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs> <!-- Use this for plugin version HIGHER than 3.1, delete the other one -->
                    <arg>-processor</arg>
                    <arg>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</arg>
                </compilerArgs>
                <compilerArguments> <!-- Use this for plugin version LOWER than 3.1, delete the other one -->
                    <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${build-helper-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${project.basedir}/target/generated-sources/annotations</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Upvotes: 0

Oliver Matz
Oliver Matz

Reputation: 11

Adding hibernate-jpamodelgen to my dependencies did not do the job for me, but rather created dependency conflicts, which caused the dependencyConvergence of the maven-enforcer-plugin to complain unless I combined it with scope 'protected'. Besides, it does not seem plausible to me to declare a runtime dependency unless I have one.

It turned out not to be necessary for me: I used Cesar's approach, but unlike in Cesar's case, I did not need to add hibernate-jpamodelgen or anything else to my dependencies.

I already had a processor added to the maven-compiler-plugin, namely mapstruct-processor. Like Cesar suggested, I added the hibernate-jpamodelgen to that configuration. In my case, the result was:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>11</source>
        <target>11</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.2.Final</version>
            </path>
            <!-- The following path declaration is the only thing I added to make hibernate-jpamodelgen run -->
            <path>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>5.3.20.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

Upvotes: 1

Cesar Manuel Cruzata
Cesar Manuel Cruzata

Reputation: 51

This works for me

pom.xml:

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                        <version>2.2.11.RELEASE</version>
                    </path>
                    <path>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                            <version>5.4.22.Final</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
  </plugins>
</build>

<profiles>
  <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
            </dependency>
        </dependencies>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
</profiles>
import com.mycompany.myapp.domain.*; // for static metamodels
import com.mycompany.myapp.domain.User

public class UserQueryService {

private Specification<User> createSpecification(UserCriteria criteria) {
  Specification<User> specification = Specification.where(null);
  if (criteria != null) {
    if (criteria.getId() != null) {
       specification = specification.and(buildSpecification(criteria.getId(), User_.id));
    }
  }
  return specification;
}    
}

Upvotes: 2

Marcus Schulz
Marcus Schulz

Reputation: 123

I usually just add the hibernate-jpamodelgen to the annotationprocessorpath of the compiler plugin. That prevents the processor to be packaged in the deployment.

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <annotationProcessorPaths>
        <path>
          <groupId>org.mapstruct</groupId>
          <artifactId>mapstruct-processor</artifactId>
          <version>${org.mapstruct.version}</version>
        </path>
        <path>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-jpamodelgen</artifactId>
          <version>5.4.3.Final</version>
        </path>
      </annotationProcessorPaths>
    
    </configuration>
  </plugin>

Upvotes: 7

Nikolai  Shevchenko
Nikolai Shevchenko

Reputation: 7521

Remove <scope>provided</scope> from

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>5.4.3.Final</version>
    </dependency>

Add plugin

    <plugins>
        ...
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>
                    <version>5.4.3.Final</version>
                </dependency>
            </dependencies>
        </plugin>
        ...
    </plugins>

And then rebuild

mvn clean package -DskipTests

Upvotes: 3

Sambit
Sambit

Reputation: 8011

In case of Java 12, use the below code snippet inside build in pom.xml.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>12</release>  
    </configuration>
</plugin>

After this, add the below for jpamodelgen.

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>5.4.3.Final</version>
  <optional>true</optional>
</dependency>

Upvotes: 1

Related Questions