Github Actions for Java 8 Maven/Gradle Projects

SonarQube 9.9 LTS Version

We have Maven/Gradle projects which use Java 8 to built-in using GitHub actions. Now we updated the SonarQube version to 9.9. How we can set up the GitHub Action yaml file to build Java 8 project? I have gone through this document but there is no example for Java 8 project for GitHub Actions.

if I set java-version to 8 it builds but the sonar scan fails because it requires java 11.
Can you please help?

GitHub integration

Hi,

Any one help me with this?

Hi @Colin @ganncamp

I am sorry for tagging,
Can you please help me here?

Hi,

Have you tried building to Java 8 using the -target flag on javac?

 
Ann

You can continue using Java 8 to build your project and use Java 11 or 17 to run the SonarScanner.

Here’s an example based on GitHub integration

name: Build
on:
  push:
    branches:
      - main # the name of your main branch
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
		  
      - name: Set up JDK 8
        uses: actions/setup-java@v1
        with:
          java-version: 8
	
      - name: Build
        run: mvn -B verify
		  
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
	
      - name: Analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
        run: mvn -B org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
1 Like

Hi @ganncamp @felipebz

Yes, I tried with javac.
Github action yaml file I set up Java 8 and Java 11 as shown in the logs.
Also, I tried to set up -Dsonar.java.jdkHome= with both Java paths.

Logs

Run actions/setup-java@v3
Installed distributions
  Trying to resolve the latest version from remote
  Resolved latest version as 11.0.18+10
  Trying to download...
  Downloading Java 11.0.18+10 (Adopt-Hotspot) from https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.18%2B10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.18_10.tar.gz ...
  Extracting Java archive...
  /usr/bin/tar xz --warning=no-unknown-keyword -C /home/ec2-user/actions-runner/_work/_temp/1598058e-694d-4afd-9666-6952f203774f -f /home/ec2-user/actions-runner/_work/_temp/4b82bcf3-77e7-4bff-aa5c-ada33c7568e3
  Java 11.0.18+10 was downloaded
  Setting Java 11.0.18+10 as the default
  Creating toolchains.xml for JDK version 11 from adopt
  Writing to /home/ec2-user/.m2/toolchains.xml
  
  Java configuration:
    Distribution: adopt
    Version: 11.0.18+10
    Path: /home/ec2-user/actions-runner/_work/_tool/Java_Adopt_jdk/11.0.18-10/x64
  
  Trying to resolve the latest version from remote
  Resolved latest version as 8.0.362+9
  Trying to download...
  Downloading Java 8.0.362+9 (Adopt-Hotspot) from https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u362-b09/OpenJDK8U-jdk_x64_linux_hotspot_8u362b09.tar.gz ...
  Extracting Java archive...
  /usr/bin/tar xz --warning=no-unknown-keyword -C /home/ec2-user/actions-runner/_work/_temp/6a020496-bb18-4e53-8a66-554c164cfe11 -f /home/ec2-user/actions-runner/_work/_temp/f6b0752b-edaa-4733-87c0-5636db9cb7c7
  Java 8.0.362+9 was downloaded
  Setting Java 8.0.362+9 as the default
  Creating toolchains.xml for JDK version 8 from adopt
  Overwriting existing file /home/ec2-user/.m2/toolchains.xml
  
  Java configuration:
    Distribution: adopt
    Version: 8.0.362+9
    Path: /home/ec2-user/actions-runner/_work/_tool/Java_Adopt_jdk/8.0.362-9/x64
  
Creating settings.xml with server-id: github
Writing to /home/ec2-user/.m2/settings.xml
maven cache is not found

Still, I am getting this error.

Downloaded from internal-repository: https://nexus.nee.com/repository/maven-public/org/codehaus/plexus/plexus-utils/3.2.1/plexus-utils-3.2.1.jar (262 kB at 935 kB/s)
[INFO] User cache: /home/ec2-user/.sonar/cache
[INFO] SonarQube version: 9.9.0.65466
[INFO] Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 47.114 s
[INFO] Finished at: 2023-03-06T21:18:12Z
[INFO] Final Memory: 55M/7851M
[INFO] ------------------------------------------------------------------------
Error:  Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar (default-cli) on project tb-volume-import-mv90: Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar failed: An API incompatibility was encountered while executing org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar: java.lang.UnsupportedClassVersionError: org/sonar/batch/bootstrapper/EnvironmentInformation 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
Error:  -----------------------------------------------------
Error:  realm =    plugin>org.codehaus.mojo:sonar-maven-plugin:3.9.1.2184
Error:  strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
Error:  urls[0] = file:/home/ec2-user/.m2/repository/org/sonarsource/scanner/maven/sonar-maven-plugin/3.9.1.2184/sonar-maven-plugin-3.9.1.2184.jar
Error:  urls[1] = file:/home/ec2-user/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.4/plexus-sec-dispatcher-1.4.jar
Error:  urls[2] = file:/home/ec2-user/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
Error:  urls[3] = file:/home/ec2-user/.m2/repository/org/codehaus/plexus/plexus-utils/3.2.1/plexus-utils-3.2.1.jar
Error:  urls[4] = file:/home/ec2-user/.m2/repository/org/sonarsource/scanner/api/sonar-scanner-api/2.16.2.588/sonar-scanner-api-2.16.2.588.jar
Error:  urls[5] = file:/home/ec2-user/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
Error:  Number of foreign imports: 1
Error:  import: Entry[import  from realm ClassRealm[maven.api, parent: null]]

Hi @ganncamp

Can you please help?

Hi,

You’re still running analysis with the wrong version of Java.

And the first log snippet shows that:

Did you try Felipe’s example?

 
Ann

Yes, I tried Felipe’s example.
But we are using the maven wrapper to build and analyze the project.
Here is the command which we are using in GitHub actions yaml and the wrapper file.

      - name: Build
        working-directory: src/volumeImporterLambda
        run: |
           ./mvnw -X \
            --settings settings.xml \
            clean \
            install \
            -Dsonar.java.jdkHome=/home/ec2-user/actions-runner/_work/_tool/Java_Zulu_jdk/8.0.362-9/x64 \
            -Dsonar.host.url=${{ secrets.SONARQUBE_HOST2 }} \
            -Dsonar.login=${{ secrets.SONARQUBE_TOKEN2 }}

mvnw.txt (10.0 KB)