如何在运行“maven clean install”时仅运行一次单元测试和声纳?
我的 Jenkins 工作有以下配置: 首先清理并构建 Maven 项目,然后运行单元测试和静态分析:clean install sonar:sonar
问题是 install
和 sonar:sonar
各自运行单元测试,这实际上使构建时间增加了一倍。
我通过将 clean install sonar:sonar
更改为 clean install -DskipTests
并使用 Jenkins 声纳插件运行 Sonar 来修复此问题。现在单元测试只运行一次,声纳显示结果,但詹金斯不再知道测试了。
我的猜测是,Jenkins 仅在构建后查看 Surefire-reports 文件夹,而不是在 Sonar(这是构建后操作)之后查看。
I had the following configuration for my Jenkins job:
First clean and build the maven project, then run the unit tests and static analysis: clean install sonar:sonar
The problem was that install
and sonar:sonar
each ran the unit tests which effectively doubled the build time.
I fixed this by changing clean install sonar:sonar
to clean install -DskipTests
and running Sonar using the Jenkins sonar plugin. Now the unit tests ran only once and sonar showed the results, but Jenkins didn't know about the tests anymore.
My guess is that Jenkins is only looking at the surefire-reports folder after building, not after Sonar (which is a post-build action).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以先运行
clean install -DskipTests
,然后添加第二个 Maven 构建步骤sonar:sonar
。测试(以及完整的声纳分析)将在构建阶段运行,之后詹金斯可以收集可靠的结果。
You could just run
clean install -DskipTests
first and then add a second maven build stepsonar:sonar
.The tests (and complete sonar analysis) will be run during the build phase, and the surefire results can be collected by jenkins afterwards.
正如您所说,Sonar 是编译后步骤。 Sonar 要求构建完成并通过所有测试才能运行,否则它所做的事情没有任何意义。
声纳使用仪器运行测试(如果我没记错的话,cobertura),它给出了测试的代码覆盖率。
因此,您需要在 Sonar 运行之前安装(或至少编译和测试),然后 Sonar 出于其自身目的使用仪器重新运行测试。
As you said, Sonar is a post compile step. Sonar requires that the build be complete and pass all of it's tests before it can run, otherwise the things it does don't make any sense.
Sonar runs the tests with instrumenting (cobertura if I remember correctly), which gives the code coverage for the tests.
So, you need to do install (or at least compile & test) before Sonar runs, and then Sonar reruns the tests with instrumenting for it's own purposes.