Under JDK 21, Maven compilation results in an error

15.6k Views Asked by At

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.

2

There are 2 best solutions below

2
Daniel Förberg On

This works for me: Add this to your pom file

<properties>
  <java.version>21</java.version>
</properties>
0
Krzysztof Tomaszewski On

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.