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

发布于 2025-01-25 09:26:08 字数 7319 浏览 5 评论 0 原文

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

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

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

​致命错误编译:错误:无效目标发布:17-&GT; [help 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2025-02-01 09:26:08

您正在使用Maven编译器插件3.8.1版,该版本于2019年5月发布,该版本于2021年9月发布,该版本是Java 17。在 pom.xml 中。

<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版本,指定它:

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

否则,您正在使用所使用的GitHub Runner中的任何Java版本是默认版本,并且由于您不固定版本而不是使用Ubuntu:最新,这可能不是您想要的,甚至是您想要的,甚至是随着时间的变化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文