Under JDK 21, Maven compilation results in an error
Asked Answered
G

3

6

When I execute mvn install -DskipTests Maven gives me an error related to virtual threads

[ERROR] /E:/mycode/javaproject/rms/rms-parse/src/main/java/com/example/config/AsyncConfig.java:[19,49] 鎵句笉鍒扮鍙
[ERROR]   绗﹀彿:   鏂规硶 newVirtualThreadPerTaskExecutor()
[ERROR]   浣嶇疆: 绫?java.util.concurrent.Executorss
[ERROR] /E:/mycode/javaproject/rms/rms-parse/src/main/java/com/example/config/AsyncConfig.java:[25,50] 鎵句笉鍒扮鍙
[ERROR]   绗﹀彿:   鏂规硶 newVirtualThreadPerTaskExecutor()
[ERROR]   浣嶇疆: 绫?java.util.concurrent.Executorss
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :rms-parse

Here is my main tools version: Maven version: 3.9.5 JDK: 21 Project Stucture:

rms
  - rms-common
  - rms-parse
  - rms-admin
  - <some other sub-module>
  - pom.xml

rms's pom.xml is(I have already omitted the configuration related to dependencies.):

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.1</version>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>rms</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>rms-parse</module>
    <module>rms-mapper</module>
    <module>rms-service</module>
    <module>rms-common</module>
    <module>rms-admin</module>
    <module>rms-model</module>
  </modules>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>21</maven.compiler.source>
      <maven.compiler.target>21</maven.compiler.target>
  </properties>


  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>com.github.shalousun</groupId>
          <artifactId>smart-doc-maven-plugin</artifactId>
          <version>2.7.7</version>
          <configuration>
            <!--指定生成文档的使用的配置文件-->
            <configFile>${basedir}/src/main/resources/smart-doc.json</configFile>
            <!--指定项目名称-->
            <projectName>rms</projectName>
          </configuration>
          <executions>
            <execution>
              <phase>compile</phase>
              <goals>
                <goal>html</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.11.0</version>
          <configuration>
            <source>21</source>
            <target>21</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

The following is the code I'm using virtual threads in my project, and it's not compiling correctly.

@Configuration
@Slf4j
public class AsyncConfig {

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
    }

    @Bean
    public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
        return protocolHandler -> {
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };

    }
}

The following is the settings of maven about jdk setting, This file exists in two locations, one in ~/.m2, and the other in the conf directory of the Maven installation. I've configured both locations with the same settings.

  <profiles>
    <profile>    
      <id>jdk-21</id>    
      <activation>    
        <activeByDefault>true</activeByDefault>    
        <jdk>21</jdk>    
      </activation>    
      <properties>    
        <maven.compiler.source>21</maven.compiler.source>    
        <maven.compiler.target>21</maven.compiler.target>    
        <maven.compiler.compilerVersion>21</maven.compiler.compilerVersion>    
      </properties>    
  </profile>
  </profiles>
</settings>

I expect Maven to compile my project successfully.

Guerrero answered 15/10, 2023 at 3:44 Comment(7)
Can you show the output of mvn help:effective-pom?Chemism
The console is outputting a lot of information. What specific information would you like me to filter?Guerrero
I've recorded this information in a file. You can download and review it ; link is alist.duckflew.cn/d/…Guerrero
Can you try setting <java.version>21</java.version> in your <properties>?Chemism
when I set this property, it works, thx!Guerrero
The profiles you have configured and the source/target configurations please remove them.. only use as suggested java.version (based on Spring Boot)...Hypermetropia
Does this mean I don't need to specify configurations like <maven.compiler.source>21</maven.compiler.source> in my Maven configuration file and only need <java.version>21</java.version> in the project's pom.xml?Guerrero
S
9

This works for me: Add this to your pom file

<properties>
  <java.version>21</java.version>
</properties>
Susie answered 15/10, 2023 at 22:9 Comment(2)
It'work for me, But have you noticed that the console output above is showing garbled characters? Do you know how to fix it?Guerrero
This way it works only when you're using Spring Boot.Elocution
E
2

In my case the only change in pom.xml to denote I want to use Java 21 was adding following properties:

    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>

After refreshing project in IntelliJ (in Maven tab), the IDE correctly recognized language features from Java 21.

Elocution answered 17/3, 2024 at 22:16 Comment(0)
P
0
change JAVA verssion 21 => 19 and add this in pom.xml

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>16</source>
                <target>16</target>
                <annotationProcessorPaths>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>
Pantywaist answered 1/9, 2024 at 20:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.