【问题标题】:Maven release issue - JUnit tests run when they shouldn'tMaven 发布问题 - JUnit 测试在不应该运行时运行
【发布时间】:2018-10-19 20:35:13
【问题描述】:

我需要能够在 Jenkins 的工件上运行 Maven release:prepare / release:perform 而无需运行其 JUnit 测试。

我的测试 pom.xml 中的 Surefire 配置

  <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <executions>
      <execution>
        <id>default-test</id>
        <phase>test</phase>
        <goals>
          <goal>test</goal>
        </goals>
        <configuration>
          <testFailureIgnore>false</testFailureIgnore>
        </configuration>
      </execution>
    </executions>
    <configuration>
      <testFailureIgnore>false</testFailureIgnore>
    </configuration>
  </plugin>

Apache’s Surefire documentation 建议使用 mvn install -DskipTests 或 mvn install -Dmaven.test.skip=true。当我运行安装时,两者都可以工作:

mvn clean install -Dsurefire.testFailureIgnore=true --show-version --batch-mode -e

但是当我在 release:perform 上尝试它们时,这两个参数都不起作用。

我尝试了以下命令:

mvn -DpreparationGoals=clean release:prepare release:perform -DskipTests -e --batch-mode --show-version -Dusername=**** -Dpassword=**** -Dresume=false

我希望测试被跳过并且 release:perform 成功。相反,测试运行并失败(见下文)导致整个 release:perform 失败。

mvn -DpreparationGoals=clean release:prepare release:perform -Dmaven.test.skip=true -e --batch-mode --show-version -Dusername=**** -Dpassword=**** -Dresume=false

我希望测试被跳过并且 release:perform 成功。相反,测试运行并失败(见下文)导致整个 release:perform 失败。

mvn -DpreparationGoals=clean release:prepare release:perform -Dsurefire.testFailureIgnore=true -e --batch-mode --show-version -Dusername=**** -Dpassword=**** -Dresume=false

这并不理想(无需重新运行 JUnit 测试,因为它们已经运行),但如果忽略 JUnit 结果允许 release:perform 成功,那么我们现在就可以解决了。但是,测试运行并仍然失败,而不是忽略结果,从而导致整个 release:perform 失败。

mvn -DpreparationGoals=clean release:prepare release:perform -fn -e --batch-mode --show-version -Dusername=**** -Dpassword=**** -Dresume=false

这也不理想。它确实允许 release:perform 成功完成,但代价是如果 release:perform 因 JUnit 测试失败以外的其他原因而失败,最终用户不会收到通知。

这是运行 release:prepare release:perform -DskipTests 或 release:prepare release:perform -Dmaven.test.skip=true 的输出:

+ mvn -DpreparationGoals=clean release:prepare release:perform -DskipTests -e --batch-mode --show-version -Dusername=**** -Dpassword=**** -Dresume=false
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T11:29:23-06:00)
…
Java version: 1.7.0_71, vendor: Oracle Corporation
…
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.11.6.el7.x86_64", arch: "amd64", family: "unix"

[INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ TestInvProfileWeb ---
[INFO] [INFO] 
[INFO] [INFO] -------------------------------------------------------
[INFO] [INFO]  T E S T S
[INFO] [INFO] -------------------------------------------------------
[INFO] [INFO] Running com.investmentprofile.jsf.EndUserBeanTest
[INFO] [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.028 s <<< FAILURE! - in com.investmentprofile.jsf.EndUserBeanTest
[INFO] [ERROR] test1(com.investmentprofile.jsf.EndUserBeanTest)  Time elapsed: 0.008 s  <<< FAILURE!
[INFO] junit.framework.AssertionFailedError
[INFO]   at com.investmentprofile.jsf.EndUserBeanTest.test1(Unknown Source)
[INFO] 
[INFO] [INFO] 
[INFO] [INFO] Results:
[INFO] [INFO] 
[INFO] [ERROR] Failures: 
[INFO] [ERROR]   EndUserBeanTest.test1:48
[INFO] [INFO] 
[INFO] [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] [INFO] 
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Reactor Summary:
[INFO] [INFO] 
[INFO] [INFO] TestInvProfileWebParent ............................ SUCCESS [  1.622 s]
[INFO] [INFO] TestInvProfileWebClient ............................ SUCCESS [  8.253 s]
[INFO] [INFO] TestInvProfileWeb .................................. FAILURE [  5.333 s]
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] BUILD FAILURE
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Total time: 15.396 s
[INFO] [INFO] Finished at: 2018-10-19T13:40:42-05:00
[INFO] [INFO] Final Memory: 48M/600M
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project TestInvProfileWeb: There are test failures.
[INFO] [ERROR] 
[INFO] [ERROR] Please refer to /opt/Jenkins/workspace/unk_TestInvProfileWebParent-YI772VMG3PH6E767GSGQFPUMCBV45RSLSRI75337QAZHUOO63NUQ/target/checkout/TestInvProfileWebParent/TestInvProfileWeb/target/surefire-reports for the individual test results.
[INFO] [ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[INFO] [ERROR] -> [Help 1]
[INFO] [ERROR] 
[INFO] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[INFO] [ERROR] Re-run Maven using the -X switch to enable full debug logging.
[INFO] [ERROR] 
[INFO] [ERROR] For more information about the errors and possible solutions, please read the following articles:
[INFO] [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[INFO] [ERROR] 
[INFO] [ERROR] After correcting the problems, you can resume the build with the command
[INFO] [ERROR]   mvn <goals> -rf :TestInvProfileWeb
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] TestInvProfileWebParent ............................ FAILURE [ 20.071 s]
[INFO] TestInvProfileWebClient ............................ SKIPPED
[INFO] TestInvProfileWeb .................................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.508 s
[INFO] Finished at: 2018-10-19T13:40:42-05:00
[INFO] Final Memory: 15M/210M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5:perform (default-cli) on project TestInvProfileWebParent: Maven execution failed, exit code: '1' -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5:perform (default-cli) on project TestInvProfileWebParent: Maven execution failed, exit code: '1'
              at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
              at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
              at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
              at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
              at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
              at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
              at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
              at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
              at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
              at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
              at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
              at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
              at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
              at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
              at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Maven execution failed, exit code: '1'
              at org.apache.maven.plugins.release.PerformReleaseMojo.execute(PerformReleaseMojo.java:181)
              at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
              at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
              ... 19 more
Caused by: org.apache.maven.shared.release.ReleaseExecutionException: Maven execution failed, exit code: '1'
              at org.apache.maven.shared.release.phase.AbstractRunGoalsPhase.execute(AbstractRunGoalsPhase.java:89)
              at org.apache.maven.shared.release.phase.RunPerformGoalsPhase.runLogic(RunPerformGoalsPhase.java:135)
              at org.apache.maven.shared.release.phase.RunPerformGoalsPhase.execute(RunPerformGoalsPhase.java:46)
              at org.apache.maven.shared.release.DefaultReleaseManager.perform(DefaultReleaseManager.java:429)
              at org.apache.maven.shared.release.DefaultReleaseManager.perform(DefaultReleaseManager.java:381)
              at org.apache.maven.plugins.release.PerformReleaseMojo.execute(PerformReleaseMojo.java:177)
              ... 21 more
Caused by: org.apache.maven.shared.release.exec.MavenExecutorException: Maven execution failed, exit code: '1'
              at org.apache.maven.shared.release.exec.InvokerMavenExecutor.executeGoals(InvokerMavenExecutor.java:394)
              at org.apache.maven.shared.release.exec.AbstractMavenExecutor.executeGoals(AbstractMavenExecutor.java:110)
              at org.apache.maven.shared.release.phase.AbstractRunGoalsPhase.execute(AbstractRunGoalsPhase.java:81)
              ... 26 more

如何让 Surefire 在 release:perform 期间不运行测试?

问候, M.Manojkumar

【问题讨论】:

  • 你真的想要一个没有通过测试的发布工件吗?
  • 只需修复您通过的测试...
  • 你找到解决办法了吗?

标签: maven junit jenkins-pipeline maven-surefire-plugin


【解决方案1】:

首先,我同意上面的两个cmets。也就是说,问题在于您的“-DskipTests”需要传递给发布:执行。您可以通过设置 arguments 参数来做到这一点:

 mvn release:perform -Darguments=-DskipTests

【讨论】:

  • 只有在您正确配置 maven-release-plugin 以使用 arguments 时才有效,默认情况下不会这样做。
  • 嘿@khmarbaise - 我已经在 100 个生产发布工作中使用上述方法超过 5 年了......您不必更改插件配置 - 您只需通过arguments 通过命令行配置。期间。
猜你喜欢
  • 2017-04-20
  • 2013-08-23
  • 2021-12-16
  • 2018-06-18
  • 2019-12-17
  • 2020-12-31
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多