背景
对于 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 ,选择第二个。
![](https://img.haomeiwen.com/i297930/1332b4025b7e304b.png)
接下来出现这个界面:
![](https://img.haomeiwen.com/i297930/add5bb01dd4097ab.png)
它会在项目的根目录
下创建一个 .github/workflows/maven.yml
文件,可以直接先提交。
报错解决
提交完成后,第一次 CI后,如果你项目pom.xml不在根目录,可能会报错:
![](https://img.haomeiwen.com/i297930/04c2e7f3859c5939.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]
![](https://img.haomeiwen.com/i297930/4f4948331a5cfaa3.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."
![](https://img.haomeiwen.com/i297930/d879231a9150c540.png)
说明项目的配置里
Dependency graph
开关没打开,找到项目的配置,打开这个开关,如下:
![](https://img.haomeiwen.com/i297930/3559b1bd942ae993.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-directory
为 StarshipBack
成功状态显示
可以在项目的 Readme.md 里增加 CI的状态显示,代码获取点为:
![](https://img.haomeiwen.com/i297930/87a482e2d5374c12.png)
效果:
![](https://img.haomeiwen.com/i297930/c4a74dbf625a7872.png)
网友评论