我想生成junit的测试覆盖率报告。所以我使用jacoco maven插件:
这是我的 pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${java.version}</compilerVersion>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
但是当我运行这个时:
mvn clean verify
我收到错误:
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.8:report (report) @ mymodule1 ---
[INFO] Loading execution data file /home/alexeij/dev/projects/JavaTestMavenBOM/mymodule1/target/jacoco.exec
[INFO] Analyzed bundle 'mymodule1' with 7 classes
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mymodule1 ---
[INFO] Building jar: /home/alexeij/dev/projects/JavaTestMavenBOM/mymodule1/target/mymodule1-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.8:check (jacoco-check) @ mymodule1 ---
[INFO] Loading execution data file /home/alexeij/dev/projects/JavaTestMavenBOM/mymodule1/target/jacoco.exec
[INFO] Analyzed bundle 'mymodule1' with 7 classes
[WARNING] Rule violated for package com.gmail.alexei28.javatestmavenbom.module1.ngnms: lines covered ratio is 0.00, but expected minimum is 0.50
[WARNING] Rule violated for package com.gmail.alexei28.javatestmavenbom.module1: lines covered ratio is 0.00, but expected minimum is 0.50
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for testprojectbom 1.0-SNAPSHOT:
[INFO]
[INFO] testprojectbom ..................................... SUCCESS [ 0.090 s]
[INFO] mymodule1 .......................................... FAILURE [ 1.710 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.961 s
[INFO] Finished at: 2022-06-01T12:24:30+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.8:check (jacoco-check) on project mymodule1: Coverage checks have not been met. See log for details. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
详情:
[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.5:check (jacoco-check) on project mymodule1: Coverage checks have not been met. See log for details. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.5:check (jacoco-check) on project mymodule1: Coverage checks have not been met. See log for details.
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoExecutionException: Coverage checks have not been met. See log for details.
at org.jacoco.maven.CheckMojo.executeCheck (CheckMojo.java:194)
at org.jacoco.maven.CheckMojo.executeMojo (CheckMojo.java:169)
at org.jacoco.maven.AbstractJacocoMojo.execute (AbstractJacocoMojo.java:46)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR]
您的输出是预期的。
报告在文件夹
target/site/jacoco
中生成。请参阅下面的 Maven 构建日志。
[INFO] --- jacoco-maven-plugin:0.8.8:report (report) @ mymodule1 ---
[INFO] Loading execution data file /home/alexeij/dev/projects/JavaTestMavenBOM/mymodule1/target/jacoco.exec
[INFO] Analyzed bundle 'mymodule1' with 7 classes
检查您的
target/site/jacoco
文件夹,它应该类似于以下内容:
target/site/jacoco/
├── index.html
├── io.sampadadubey.java.jacoco
│ ├── index.html
│ ├── index.source.html
│ ├── JacocoDemoApplication.html
│ └── JacocoDemoApplication.java.html
├── jacoco.csv
├── jacoco-resources
│ ├── branchfc.gif
│ ├── branchnc.gif
│ ├── branchpc.gif
│ ├── bundle.gif
│ ├── class.gif
│ ├── down.gif
│ ├── greenbar.gif
│ ├── group.gif
│ ├── method.gif
│ ├── package.gif
│ ├── prettify.css
│ ├── prettify.js
│ ├── redbar.gif
│ ├── report.css
│ ├── report.gif
│ ├── session.gif
│ ├── sort.gif
│ ├── sort.js
│ ├── source.gif
│ └── up.gif
├── jacoco-sessions.html
└── jacoco.xml
您的构建配置如下:
prepare-agent
阶段实现initialize
目标。report
阶段实现test
目标。check
阶段实现verify
目标。您的构建在
verify
阶段失败,因为未能符合配置的check
。 check
目标的误差如下:
.Coverage checks have not been met. See log for details
这里是告诉您失败详细信息的日志。
[INFO] --- jacoco-maven-plugin:0.8.8:check (jacoco-check) @ mymodule1 ---
[INFO] Loading execution data file /home/alexeij/dev/projects/JavaTestMavenBOM/mymodule1/target/jacoco.exec
[INFO] Analyzed bundle 'mymodule1' with 7 classes
[WARNING] Rule violated for package com.gmail.alexei28.javatestmavenbom.module1.ngnms: lines covered ratio is 0.00, but expected minimum is 0.50
[WARNING] Rule violated for package com.gmail.alexei28.javatestmavenbom.module1: lines covered ratio is 0.00, but expected minimum is 0.50
使其更易于阅读。
Rule violated for package com.gmail.alexei28.javatestmavenbom.module1.ngnms: lines
covered ratio is 0.00, but expected minimum is 0.50
Rule violated for package com.gmail.alexei28.javatestmavenbom.module1: lines
covered ratio is 0.00, but expected minimum is 0.50
你的问题不清楚,所以我会回答它,就像你想绕过jacoco一样。 您所要做的就是删除 pom 中的特定代码,如下所示:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
这将简单地跳过检查或将其删除。