美文网首页
Java项目配置Github Action构建CI(Maven

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

作者: 齐格Insight | 来源:发表于2024-09-15 10:33 被阅读0次

背景

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

相关文章

  • Maven入门教程

    GitHub仓库:Fundebug/maven-tutorial Maven简介 Maven是Java项目构建工具...

  • Maven简易笔记

    Maven用来管理Java项目,主要负责构建项目:验证、编译、测试、打包、分发等。 一、Maven组成 配置依赖关...

  • Maven 基础

    1. Maven Maven 是一个 Java 项目管理和构建工具 约定优于配置 POM(Project Obje...

  • Maven 相关概念总结

    Maven 相关概念总结 构建原理 Maven 构建项目的规则是约定大于配置,约定指 Maven 提倡项目应遵循标...

  • Maven总结

    1. 认识maven 2. 优势 约定大于配置 简单 测试支持 构建简单 CI 插件丰富

  • 利用HanLP计算中文词语语义相似度

    HanLP官方GitHub地址 HanLP 在java项目中配置HanLP 推荐使用Maven方法 在poem.x...

  • 利用HanLP计算中文词语语义相似度

    HanLP官方GitHub地址 HanLP 在java项目中配置HanLP 推荐使用Maven方法 在poem.x...

  • 概述

    maven(几乎所有java项目都使用) maven功能:项目构建、依赖管理maven安装:https://mav...

  • Maven笔记

    maven简介: 项目构建: 下载和配置 使用 maven整合eclipse: maven目录结构: maven工...

  • 通过GitLab CI/CD发布maven项目到私有仓库

    简介 本文将介绍如何通过GitLab CI/CD自动构建、测试maven项目并将它打包发布到maven私有仓库,并...

网友评论

      本文标题:Java项目配置Github Action构建CI(Maven

      本文链接:https://www.haomeiwen.com/subject/tpfiljtx.html