如何在不重建项目的情况下在Gitlab CI中进行Maven测试?

发布于 2025-02-12 05:05:49 字数 418 浏览 0 评论 0原文

我创建了一个Maven Spring Boot项目。在构建工作中,我只想在没有测试的情况下构建项目。在测试阶段,我想运行单元测试并使用构建阶段的构建,因此在测试阶段没有新的构建。问题是MVN在测试阶段再次运行构建。

image: maven:3.8-jdk-11

stages:          
  - build
  - test

build-job:       
  stage: build
  script:
    - mvn compile
  artifacts:
    paths:
      - target

test-job:
  stage: test
  script:
    - mvn test


如果它再次在测试阶段运行构建,则无需构建阶段。但是,我希望两者都可以分开阶段,以更好地分开并查看出现问题的地方。

I created a Maven Spring boot project. In the build job I want to only build the project without tests. In the test stage I want to run unit tests and use the build from the build stage, so no new build in test stage. The problem is that mvn runs a build again in the test stage.

image: maven:3.8-jdk-11

stages:          
  - build
  - test

build-job:       
  stage: build
  script:
    - mvn compile
  artifacts:
    paths:
      - target

test-job:
  stage: test
  script:
    - mvn test


If it runs a build in the test stage again, then the build stage wouldnt be necessary. But I want separate stages for both to better separate and see where something goes wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2025-02-19 05:05:49

起初,我认为将构建过程分为不同阶段的想法看起来很有吸引力,但实际上没有任何实际好处,尤其是在您试图将包装分组为编译,测试以及可能的包装阶段时。考虑以下内容:

  • 如果拉动请求管道,我们实际需要得到的是:
    • 答案是拉动请求的更改是否确实有效,即是或否
    • 来自其他工具的报告(代码覆盖,静态代码分析等)
    • 构建日志以分析故障
  • 是否

答案 上面提到的已经被Maven涵盖了 - 没有实际的理由将MVN软件包分为单独的部分,此外,Maven项目中的所有内容 verify 阶段仅是关于代码-CI -CI不应干预MVN软件包

现在,为什么我们需要CI中的阶段?显然,当我们的CI流程以某种方式取决于环境时,即当我们在不同平台上执行构建或我们想运行外部工具时,我们需要阶段,但是没有外部工具的位置,直到阶段

第二,如果您真的想将mvn软件包分为单独的阶段,则选项如下:

  • 配置Maven插件以跳过执行依靠Maven/System属性,大多数Maven插件具有如此的功能,例如,要跳过编译您可以通过-dmaven.main.skip = true来命令行或设置诸如< skipmain> my.cool.property</skipmain> and code and code -dmy.cool.property = true
  • 作为上一个,但在Maven profiles中合并属性
  • 手动重建Maven Lifecycle阶段,例如,在您的情况下,它可能看起来像:MVN Resources:testResources Compiler:testCompile:testCompile compiler:testCompile nurefire:测试

但是,从我的角度来看,所有这些选项看起来都很差:由于以下考虑因素:

  • 它非常复杂且丑陋
  • ,需要保持同步CI定义和Maven POMS

At first, in my opinion the idea to split build process into different stages might look attractive but actually does not have any practical benefits, especially in your case when you are trying to split packaging into compile, test and, probably, package stages. Think about following:

  • in case of pull request pipeline what we actually need to get is:
    • the answer whether the changes from pull request do actually work, i.e. yes or no
    • reports from other tools (code coverage, static code analysis, etc)
    • build logs to analyse failures
  • in case of release pipeline we also expect to get artifacts deployed into DML

Everything mentioned above is already covered by maven - there is no practical reason to split mvn package into separate parts, moreover everything in maven project till verify phase is about the code only - CI should not intervene in mvn package.

Now, why do we need stages in CI? Obviously we need stages when our CI process somehow depends on environment, i.e. when we are performing build against different platforms or we would like to run external tools, however there is no place for external tools till verify phase.

At second, if you really want to split mvn package into separate stages the options are following:

  • configure maven plugins to skip execution relying on maven/system properties, most of maven plugins have such capability, e.g. to skip compile you may pass -Dmaven.main.skip=true to command line or setup something like <skipMain>my.cool.property</skipMain> and pass -Dmy.cool.property=true
  • as previous one, but consolidate properties in maven profiles
  • reconstruct maven lifecycle phase manually, for example in you case it might look something like: mvn resources:testResources compiler:testCompile surefire:test

However, all of those options look very poor from my perspective due to following considerations:

  • it is very complex and ugly
  • it requires to keep in sync CI definition and maven poms
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文