简体   繁体   English

在 Maven 中配置 hibernate-jpamodelgen

[英]Configure hibernate-jpamodelgen in Maven

I want to configure hibernate-jpamodelgen into Maven POM file.我想将 hibernate-jpamodelgen 配置到 Maven POM 文件中。 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完整的 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

D 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/我非常用这个: https : //docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/

In case of Java 12, use the below code snippet inside build in pom.xml.如果是 Java 12,请在 pom.xml 中的 build 中使用以下代码片段。

<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.在此之后,为 jpamodelgen 添加以下内容。

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

Remove <scope>provided</scope> from删除<scope>provided</scope>

    <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

This works for me这对我有用

pom.xml: 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;
}    
}

I usually just add the hibernate-jpamodelgen to the annotationprocessorpath of the compiler plugin.我通常只是将 hibernate-jpamodelgen 添加到编译器插件的 annotationprocessorpath 中。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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