在Maven中配置hibernate-jpamodelgen

问题描述 投票:0回答:6
java hibernate maven
6个回答
7
投票

我通常只是将 hibernate-jpamodelgen 添加到编译器插件的注释处理器路径中。这会阻止处理器打包到部署中。

 <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>

3
投票

 中删除 
<scope>provided</scope>

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

添加插件

    <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>

然后重建

mvn clean package -DskipTests

2
投票

这对我有用

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;
}    
}

1
投票

如果是 Java 12,请在 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>

在此之后,为 jpamodelgen 添加以下内容。

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

1
投票

将 hibernate-jpamodelgen 添加到我的依赖项并没有为我完成这项工作,而是创建了依赖项冲突,这导致 maven-enforcer-plugin 的 dependencyConvergence 抱怨,除非我将其与范围“受保护”结合起来。此外,在我看来,声明运行时依赖项似乎不太合理,除非我有一个。

事实证明这对我来说没有必要:我使用了 Cesar 的方法,但与 Cesar 的情况不同,我不需要添加 hibernate-jpamodelgen 或我的依赖项的其他任何内容。

我已经在maven-compiler-plugin中添加了一个处理器,即mapstruct-processor。 就像塞萨尔建议的那样,我将 hibernate-jpamodelgen 添加到该配置中。就我而言,结果是:

<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>

0
投票

使用“maven-compiler-plugin”的正确解决方案。

如果您使用高于 3.1 的版本,请使用标签 compilerArgs 如果您使用版本

低于

3.1,请使用标签compilerArguments 不要忘记删除不适合您的版本的相应标签。

为什么还使用“

build-helper-maven-plugin

”,请检查 Martin Baumgartner 在此线程中的答案

通过将生成的源放入目标中,您将在 IDE 中遇到问题,因为找不到相关代码。因此,您可以添加 build-helper-maven-plugin 来动态添加目标目录中的文件夹

<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>
© www.soinside.com 2019 - 2024. All rights reserved.