如何为 JDK 17 编写 Github 工作流程

How to write Github workflow for JDK 17

这是 JDK 11 的当前 Github,并且工作正常。我已将我的项目迁移到 JDK 17。它在本地运行良好,但在 Github 工作流程中中断。我不知道该如何解决?

.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 }}

Docker 文件

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

错误:-

错误:无法执行目标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 -> [帮助 1]

根据建议更新pom.xml:-

<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>

更新完整 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>

您使用的 Maven 编译器插件版本 3.8.1 于 2019 年 5 月针对 Java 17 发布,该版本于 2021 年 9 月发布。请确保您使用最新的 Maven 编译器插件并且您在 pom.xml.

的配置设置中指定正确的 Java 版本
<project>
  [...]
  <properties>
    <maven.compiler.release>17</maven.compiler.source>
  </properties>
  [...]
</project>

或者像这样:

<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>

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

您也没有在工作流程中指定 Java 版本,GitHub actions Maven starter workflow 是这样指定的:

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

否则,您使用的 Java 版本是您正在使用的 GitHub 运行程序中的默认版本,并且由于您没有固定版本而是使用 ubuntu:latest,这可能不是你想要的,甚至会随着时间的推移而改变。