Java项目配置Github Action构建CI(Maven

2024-09-15  本文已影响0人  齐格Insight

背景

对于 Java 类的项目,我们可以配置一个 Github 的构建 CI,这样当我们提交代码后会开启自动构建,如果编译失败也能及早发现。

Github Action配置

对于 Java 的构建是通过 setup-java-jdk 这个 Action (参考文档:https://docs.github.com/zh/actions/use-cases-and-examples/building-and-testing/building-and-testing-java-with-maven

在 Github 项目的 Actions 里搜 Java with Maven ,选择第二个。

image.png

接下来出现这个界面:


image.png

它会在项目的根目录下创建一个 .github/workflows/maven.yml 文件,可以直接先提交。

报错解决

提交完成后,第一次 CI后,如果你项目pom.xml不在根目录,可能会报错:

image.png

Update dependency graph 这一步报错了

 # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
    - name: Update dependency graph
      uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

因为我项目的pom.xml文件不在根目录下,所以需要配置下ci执行的目录,见下文中 dependency-submission配置

Failed to execute goal com.github.ferstl:depgraph-maven-plugin:4.0.1:graph (default-cli): Goal requires a project to execute but there is no POM in this directory (/home/runner/work/Starship/Starship). Please verify you invoked Maven from the correct directory. -> [Help 1]
image.png

dependency-submission配置

官方配置文件
maven-dependency-tree-dependency-submission

一般按如下配置:

    - name: Update dependency graph
      uses: advanced-security/maven-dependency-submission-action@v3
      with:
        directory: StarshipBack

注意这里的 directory 参数,如果项目 pom.xml 文件不在项目根目录里,需要配置下。我的 pom.xml 在StarshipBack这个目录下。如果 pom.xml 已经在项目根目录下,就不需要配置这个参数。

报错:

The Dependency graph is disabled for this repository. Please enable it before submitting snapshots."
image.png
说明项目的配置里Dependency graph开关没打开,找到项目的配置,打开这个开关,如下: image.png

配置信息

# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'temurin'
        cache: maven
    - name: Build with Maven
      working-directory: StarshipBack
      run: mvn -B package --file pom.xml

    # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
    - name: Update dependency graph
      uses: advanced-security/maven-dependency-submission-action@v3
      with:
        directory: StarshipBack

注意:我自己在执行 maven 构建时,也配置了ci 的工作目录working-directoryStarshipBack

成功状态显示

可以在项目的 Readme.md 里增加 CI的状态显示,代码获取点为:


image.png

效果:


image.png
上一篇 下一篇

猜你喜欢

热点阅读