Spring Boot 2 + Java 9 + Querydsl

时间:2018-04-01 11:16:15

标签: spring-boot querydsl java-9

我已成功将我的应用程序(Spring Boot + Java + Maven)更新为Spring Boot 2.现在我正在尝试更新到Java 9。

根据NicolasFränkel的博客文章(Migrating a Spring Boot application to Java 9 - Compatibility),我完成了更新到Java 9而不使用模块信息文件。但是当我创建这样一个文件时,apt-maven-plugin就无法创建Q类型。

有没有人成功使用Java 9 + Maven + Querydsl?

apt-maven-plugin的My Maven配置如下:

     <plugin>
        <groupId>com.mysema.maven</groupId>
        <artifactId>apt-maven-plugin</artifactId>
        <version>1.1.3</version>
        <executions>
           <execution>
              <goals>
                 <goal>process</goal>
              </goals>
              <configuration>
                 <outputDirectory>target/generated-sources/java</outputDirectory>
                 <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
              </configuration>
           </execution>
        </executions>
     </plugin>

当我运行 mvn generate-sources 时,插件只打印一堆错误消息,不生成任何类并报告成功:

$ mvn generate-sources
...
[INFO] --- apt-maven-plugin:1.1.3:process (default) @ app ---
.../src/main/java/module-info.java:9: error: module not found: java.validation
   requires java.validation;
                ^
.../src/main/java/module-info.java:13: error: module not found: hibernate.jpa
   requires hibernate.jpa;
                     ^
...
21 errors
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.484 s
[INFO] Finished at: 2018-04-01T13:11:25+02:00
[INFO] ------------------------------------------------------------------------

4 个答案:

答案 0 :(得分:1)

用于同时使用QueryDSL + Mapstruct和Lombok:

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources/annotations</outputDirectory>
                <processors>
                    <processor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</processor>
                    <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    <processor>org.mapstruct.ap.MappingProcessor</processor>
                </processors>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:0)

querydsl与hibernate 5.2不兼容,请参阅此处https://github.com/querydsl/querydsl/issues/1917 (与您提到的错误无关,但很高兴知道......)

关于您的错误,您可以尝试使用https://plugins.gradle.org/plugin/net.ltgt.apt?我不得不使用它因为我无法一起使用QueryDSL和Lombok ......

plugins {
  id "net.ltgt.apt" version "0.12"
}


apply plugin: "net.ltgt.apt"


compileJava {
  aptOptions.processors =
      ['com.querydsl.apt.jpa.JPAAnnotationProcessor', 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor']
}

答案 2 :(得分:0)

尝试使用maven-compiler-plugin代替apt-maven-plugin。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>9</source>
        <target>9</target>
        <annotationProcessors>
            <annotationProcessor>com.querydsl.apt.jpa.JPAAnnotationProcessor</annotationProcessor>
        </annotationProcessors>
        <annotationProcessorPaths>
            <path>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-core</artifactId>
                <version>4.1.4</version>
            </path>
            <path>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-apt</artifactId>
                <version>4.1.4</version>
            </path>
            <path>
                <groupId>com.mysema.codegen</groupId>
                <artifactId>codegen</artifactId>
                <version>0.6.8</version>
            </path>
            <path>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.1.0</version>
            </path>
            <path>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </path>
            <path>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

用于同时使用QueryDSL和Lombok:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <annotationProcessors>
            <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
            <annotationProcessor>com.querydsl.apt.jpa.JPAAnnotationProcessor</annotationProcessor>
        </annotationProcessors>
        <annotationProcessorPaths>
            <path>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-core</artifactId>
                <version>4.1.4</version>
            </path>
            <path>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-apt</artifactId>
                <version>4.1.4</version>
            </path>
            <path>
                <groupId>com.mysema.codegen</groupId>
                <artifactId>codegen</artifactId>
                <version>0.6.8</version>
            </path>
            <path>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.1.0</version>
            </path>
            <path>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </path>
            <path>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.22</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

答案 3 :(得分:0)

改进答案或瑜伽。这绝对有效,而且没有冗余

<?xml version="1.0" encoding="UTF-8"?>
<project...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>xxx</groupId>
    <artifactId>yyy</artifactId>
    <version>zzz</version>

    <properties>
        <java.version>11</java.version>
        <mapstruct.version>1.4.1.Final</mapstruct.version>
    </properties>

    <dependencies>
        ...

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            ...
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path> <!-- Lombok made a special thing to better work with mapstruct -->
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>0.1.0</version>
                        </path>
                        <path>
                            <groupId>com.querydsl</groupId>
                            <artifactId>querydsl-apt</artifactId>
                            <classifier>jpa</classifier>
                            <version>${querydsl.version}</version>
                        </path>
                        <path>
                            <groupId>javax.annotation</groupId>
                            <artifactId>javax.annotation-api</artifactId>
                            <version>${javax-annotation.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>