Spring Boot 2 + Java 9 + Querydsl

戏子无情 提交于 2021-01-27 14:34:46

问题


I have successfully updated my application (Spring Boot + Java + Maven) to Spring Boot 2. Now I'm trying to update to Java 9.

According to Nicolas Fränkel's blog post (Migrating a Spring Boot application to Java 9 - Compatibility) I accomplished to update to Java 9 without using a module-info file. But when I create such a file, the apt-maven-plugin gets stucked on creating the Q-types.

Has anyone successfully completed using Java 9 + Maven + Querydsl?

My Maven configuration for the apt-maven-plugin looks as follows:

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

When I run mvn generate-sources, the plugin only prints a bunch of error messages, generates no classes and reports success:

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

回答1:


For using QueryDSL + Mapstruct and Lombok together:

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



回答2:


querydsl is not compatible with hibernate 5.2 yet, see here https://github.com/querydsl/querydsl/issues/1917 (not related to the error you mention but good to know...)

Regarding your error, you could try to use https://plugins.gradle.org/plugin/net.ltgt.apt ? I had to use it because I was not able to use QueryDSL and Lombok together...

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']
}



回答3:


Instead of apt-maven-plugin, try maven-compiler-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>

For using QueryDSL and Lombok together:

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



回答4:


Improvement to the answer or yogi. This definitely works and there're no redundancies

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



来源:https://stackoverflow.com/questions/49597466/spring-boot-2-java-9-querydsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!