【发布时间】:2021-04-01 15:43:29
【问题描述】:
我是 CI 和 GitHub 的绝对新手,尤其是两者的结合。 我有一个小组项目,我们使用 maven 进行构建,使用 junit 5 进行测试。我们有几个测试在本地运行得很好,但是我们的 maven.yml 没有配置,repo 不会运行这些测试。这是 .yml 的当前迭代(所有当前注释的行都是我们测试过的):
name: Java CI with Maven
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
#run: mvn -B package --file pom.xml
run: mvn --batch-mode --update-snapshots verify
- run: npm ci
- run: npm run build --if-present
- run: npm test
#- name: Test with Maven
#run: mvn -Dtest="test/*Test" test
#run: mvn '-Dtest=test.*Test' test
这是我们当前的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>Izsakazsivany</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven-unit-test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
使用 Intellij 的 maven 集成中的默认“测试”生命周期,我们所有的测试都在本地运行。 (据我所知,intellij maven 测试生命周期使用以下行进行测试:
test -f pom.xml)。但是,当我们使用 CI 配置在 GitHub 上尝试此操作时,它说在默认测试中未找到任何测试:
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ Izsakazsivany ---
[INFO] No tests to run.
(旁注:izsakazsivany 是我们的项目名称 旁注2:上面两行之间大约有100行下载)
是什么导致本地版本和 GitHub 上的版本之间出现这种差异,为什么在运行相同的 mvn 命令时看不到测试?
编辑:这可能是一个重要的规范,我们的测试在一个名为 test 的包中,并且在用于测试的单独类的特定包中。
示例:git/src/main/test/entity
【问题讨论】:
-
当您从命令行而不是 intellij 在本地运行
mvn test时会发生什么? -
@Eugene 我没有测试过,因为我没有在 Intellij 之外安装 maven,现在试试
-
顺便说一句,覆盖插件版本的首选方法,就像您对
maven-surefire-plugin所做的那样,是使用<pluginManagement>,而不是<plugins>。你的 POM 中也有冲突的东西。这一行<maven.compiler.source>15</maven.compiler.source>和它下面的一行指定了JDK 15,然后你的编译器插件指定了Java 8<source>1.8</source>。您可以通过完全删除编译器插件(无论如何它是隐式绑定的)来清理您的 POM,并在您的属性中使用maven.compiler.source/target中的 Java 8。 -
@devor110 不需要单独安装,可以从命令行调用捆绑的。它存在于
/C/Program Files/JetBrains/IntelliJ IDEA 2020.1.1/plugins/maven/lib/maven3/bin/mvn之类的地方 -
@Eugene 使用
mvn test在本地对其进行了测试,它工作得很好,就像 intellij 一样
标签: java maven unit-testing continuous-integration maven-surefire-plugin