Jenkins Pipeline Failing Due to SonarQube Compatibility Issue with Java 8 – How to Dynamically Use Java 11 for Sonar Analysis?

I am encountering an issue when running a Jenkins pipeline that involves SonarQube (9.9) analysis. The job is failing on a Jenkins slave machine that runs Java 8. Below is the error message:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar (default-cli) on project BillDeskIntegrationService: 
Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar failed: 
Unable to load the mojo 'sonar' in the plugin 'org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121' due to an API incompatibility: 
org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/sonarsource/scanner/maven/SonarQubeMojo has been compiled by a more recent version of the Java Runtime 
(class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

The error indicates that the Sonar Maven plugin requires Java 11 (class file version 55.0), but the Jenkins slave is running Java 8, which only supports class file versions up to 52.0.

I am hesitant to upgrade the entire Jenkins slave to Java 11 because it may introduce compatibility issues with other Maven builds that are dependent on Java 8.

My SonarQube 9.9 runs in a separate instance with Java 17.

Additionally, I have another Jenkins slave with both Java 8 and Java 11 installed. Is there a way to configure my Jenkins pipeline or Maven job to dynamically select the appropriate Java version based on the tool (e.g., Java 11 for SonarQube analysis and Java 8 for other parts of the build)?

14:13:19  [WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:Maven-surefire-plugin:2.19.1: 
Plugin org.apache.maven.plugins:Maven-surefire-plugin:2.19.1 or one of its dependencies could not be resolved: 
Failure to find org.apache.maven.plugins:Maven-surefire-plugin:jar:2.19.1 in https://artifactory.bfsgodirect.com/repository/BFDL_PROXY/ 
was cached in the local repository, resolution will not be reattempted until the update interval of BFDL_PROXY has elapsed or updates are forced
...
14:13:19  [INFO] --- sonar-maven-plugin:4.0.0.4121:sonar (default-cli) @ BillDeskIntegrationService ---
...
14:13:20  [ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar (default-cli) on project BillDeskIntegrationService: 
Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar failed: 
Unable to load the mojo 'sonar' in the plugin 'org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121' due to an API incompatibility: 
org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/sonarsource/scanner/maven/SonarQubeMojo has been compiled by a more recent version of the Java Runtime 
(class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
14:13:20  [ERROR] -----------------------------------------------------

I would appreciate any advice on how to resolve this issue without disrupting the current setup.

I have both Java 8 and Java 11 present on the same Jenkins slave but still face the same error when running the pipeline.

Is it possible to run two different Java versions in the same pipeline script?
How can I fix the SonarQube 9.9 compatibility with the Java 8 slave?

Your entire Maven build must run using Java 11 or above. You can’t run parts of the Maven build with Java 8 and other parts with Java 11. However, what you can do is do 2 builds: one with Java 8 that doesn’t include SonarQube, and one with Java 11 that runs only SonarQube. To use existing test and coverage reports it’s advised to run this after the regular build, and without clean.

To get this to work you need to have Java 11 configured as tool inside Jenkins. For me the easiest way is to include an installer (I prefer Adoptium), that way the JDK will be installed if needed, separate from the JDK that’s running Jenkins.

Assuming you have a (multibranch) pipeline job, the usual first step is to define global tools:

pipeline {
    ...
    tools {
        jdk 'jdk-8'     // or whatever the identifier is
        maven 'maven-3' // or whatever the identifier is
    }
    ...
}

This makes java and mvn available throughout the pipeline. This is where we now have to deviate from, but only for the part that needs a different version.

In the build for SonarQube you need to define a different tool. If available you can use withMaven:

// The publisherStrategy prevents build artifacts to be added as Jenkins artifacts
withMaven(jdk: 'jdk-11', maven: 'maven-3', publisherStrategy: 'EXPLICIT') {
    ...
}

If that’s not available (due to a missing plugin), you can still do it using tool and withEnv:

withEnv(["JAVA_HOME=${tool('jdk-11')}"]) {
    // $JAVA_HOME is now that of Java 11, so Maven will use Java 11
    // To run a different Maven that's also possible:
    sh "${tool('maven-3')}/bin/mvn --version"
}

The tool function installs the tool if needed, and returns its base path. I’ve used it quite often in configurable pipelines where the Java version wasn’t fixed.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật