How to write Github workflow for JDK 17
Asked Answered
H

1

9

This is the current Github for JDK 11 and works fine. I have migrated my project to JDK 17. It works fine locally but breaks in Github workflows. I don't know how can I fix it?

.github\workflows\docker-publish.yml:-

name: Docker

on:
  push:
    branches: [ master ]
    # Publish semver tags as releases.
    tags: [ 'v*.*.*' ]
  pull_request:
    branches: [ master ]

env:
  # Use docker.io for Docker Hub if empty
  REGISTRY: ghcr.io
  # github.repository as <account>/<repo>
  IMAGE_NAME: ${{ github.repository }}
  TAG: 'latest'
    
jobs:
  build:

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      # This is used to complete the identity challenge
      # with sigstore/fulcio when running outside of PRs.

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        
      - name: Cache local Maven repository
        uses: actions/cache@v2
        with:
         path: ~/.m2/repository
         key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
         restore-keys: |
          ${{ runner.os }}-maven-
          
      - name: maven-settings-xml-action
        uses: whelk-io/maven-settings-xml-action@v12
        with:
          repositories: '[{ "id": "github", "name": "Backend repo", "url": "https://maven.pkg.github.com/dcsaorg/Core", "releases": { "enabled": "true" }, "snapshots": { "enabled": "true" } }]'
          servers: '[{ "id": "github", "username": "${{ secrets.USER }}", "password": "${{ secrets.PACKAGES_PAT }}" }]'
        
      - name: Checkout org/TNT
        uses: actions/checkout@v2
        with:
          repository: org/TNT
          ref: master
          token: ${{ secrets.REPO_ACCESS_PAT }}
          path: TNT
          
      - name: Build TNT
        run: cd TNT && mvn install -DskipTests -X 
        
      - name: build Toolkit
        run:  mvn -B package -DskipTests -X
        
      - name: Extract Build tag
        id: buildtag
        run:  echo "TAG=$(mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout)">> $GITHUB_ENV

      # Build and push Docker image
      # https://github.com/marketplace/actions/docker-build-push-action
      - name: Build and push Docker image
        uses: mr-smithers-excellent/docker-build-push@v5
        with:          
          image: consumer
          tags: ${{ env.TAG }}, latest
          registry: ghcr.io            
          githubOrg: org
          username: ${{ github.actor }}
          password: ${{ secrets.USER_PACKAGES_PAT }}

Dockerfile

FROM eclipse-temurin:17-jre-alpine
RUN mkdir -p /ctk
COPY target/ctk_consumer-*.jar /ctk/ctk_consumer.jar
WORKDIR /ctk/
ENTRYPOINT java -jar ctk_consumer.jar

Error:-

Error: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ctk_consumer: Fatal error compiling: error: invalid target release: 17 -> [Help 1]

Updated pom.xml after the suggestion:-

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <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>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Updated full pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.company</groupId>
    <artifactId>ctk_consumer</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>
    <name>ctk_consumer</name>
    <description>ctk consumer</description>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <java.version>17</java.version>
        <dcsa.tnt.version>0.0.1</dcsa.tnt.version>
        <dcsa.artifacttype>-SNAPSHOT</dcsa.artifacttype>
        <lombok.version>1.18.22</lombok.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>5.0.9</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Holoenzyme answered 29/4, 2022 at 8:10 Comment(0)
G
12

You are using Maven compiler plugin version 3.8.1, which was released in May 2019 for Java 17, which was released in September 2021. Make sure that you use the newest Maven Compiler plugin and that you specify the correct Java release in its configuration setting in pom.xml.

<project>
  [...]
  <properties>
    <maven.compiler.release>17</maven.compiler.source>
  </properties>
  [...]
</project>

Or like this:

<project>
    [...]
    <build>
        [...]
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <release>17</source>
                </configuration>
            </plugin>
        </plugins>
        [...]
    </build>
    [...]
</project>

Source: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

You also don't specify the Java version in your workflow, the GitHub actions Maven starter workflow specifies it like this:

name: Java CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'adopt'
      - name: Build with Maven
        run: mvn --batch-mode --update-snapshots package

Otherwise you are using whatever Java version is the default in the GitHub runner you are using and as you are not pinning down the version but instead use ubuntu:latest, this may not be the one you want and even change over time.

Grass answered 29/4, 2022 at 8:36 Comment(10)
shouldn't it be 17 for JDK 17 <configuration> <source>1.7</source> <target>1.7</target> </configuration>Holoenzyme
@MasiBoo: You are right, I used the old example from <maven.apache.org/plugins/maven-compiler-plugin/examples/…> where 1.7 stands for Java 7 and have fixed the answer using the newer release option from <maven.apache.org/plugins/maven-compiler-plugin/examples/…>.Silsby
After this update, I got new error as:- Error: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project dcsa_tnt: Fatal error compiling: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x45c90a05) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x45c90a05 -> [Help 1]Holoenzyme
@MasiBoo: You are still using Maven Compiler Plugin 3.8.1, please update to 3.10.1. Also update your project lombok version so that it works with Java 17.Silsby
pls check updatd pom.xml in updated part of the quesiton. I added another extra part for the known lombok error.Holoenzyme
@MasiBoo: ${lombok.version} you missed the definition of that. You need to go to the place where that is and update it.Silsby
Let us continue this discussion in chat.Holoenzyme
@MasiBoo: I wrote you in chat but prefer it to be here because StackOverflow should offer solutions to other people as well instead of just being tech support for a specific problem, so please amend your post with the necessary information so that others can solve this problem as well.Silsby
I added full pom.xml. Also, I got to know this:- with: java-version: 17 distribution: 'temurin' Coz distribution: 'adopt' is not upgradable anymore. Recommadation is to use 'temurin'Holoenzyme
Yes that's fine, great that you got it to work!Silsby

© 2022 - 2024 — McMap. All rights reserved.