在测试阶段之后将始终执行哪个Maven阶段? [英] Which maven phase will be always executed after test phase?

查看:115
本文介绍了在测试阶段之后将始终执行哪个Maven阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个Maven插件,该插件用于在Maven test阶段之前创建测试数据库(具有随机名称),并在test阶段完成后删除该数据库.

I have implemented a Maven plugin which is used to create test database (with random name) before Maven test phase, and drops that database when the test phase is completed.

该插件需要执行两次,分别在test阶段(用于创建数据库)之前和test阶段之后(用于删除该测试数据库)之后.

The plugin need to be executed two times, before test phase (when is used to create database) and after test phase (when is used to drop that test database).

哪个Maven生命周期阶段将始终在测试阶段之后执行,而test阶段是否成功执行?

Which Maven lifecycle phase will be always executed after test phase, whether test phase is successfully executed or not?

推荐答案

There are no particular phase in the Maven lifecycle that corresponds to pre- and post-test. This is because unit tests are not supposed to require an external environment. It sounds like what you want to do are not unit tests but integration tests instead, because they require an environment to be set up.

文档:

  • test-使用合适的单元测试框架测试编译的源代码.这些测试不应要求将代码打包或部署
  • integration-test-处理该程序包并将其部署到可以运行集成测试的环境中
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • integration-test - process and deploy the package if necessary into an environment where integration tests can be run

其中有一个pre-integration-testintegration-testpost-integration-test用于设置,运行和销毁测试环境.

And there is a pre-integration-test, integration-test and post-integration-test that are used to setup, run and destroy the test environment.

  • pre-integration-test:执行集成测试之前所需的操作.这可能涉及诸如设置所需环境之类的事情.
  • integration-test:处理该程序包并将其部署到可以运行集成测试的环境中.
  • post-integration-test:执行集成测试后执行所需的操作.这可能包括清理环境.
  • pre-integration-test: perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run.
  • post-integration-test: perform actions required after integration tests have been executed. This may including cleaning up the environment.

这样,在integration-test阶段使用 maven-failsafe-plugin .

As such, it would be easier and a lot cleaner to do this in integration-test phase using the maven-failsafe-plugin.

现在,如果您真的想在单元测试中运行它,那么我不会将数据库的创建/删除作为Maven插件编写.在测试环境中配置应用程序后,让您的应用程序创建测试数据库会更好. (例如,如果您使用的是Spring,它有很多功能.)

Now, if you really want to run that as unit tests, I would not write the creation / deletion of the database as a Maven plugin. It would be a lot better to let your application create the test database when it is configured in a test environment. (For example, if you're using Spring, it has a lot of facilities for that.)

而且,如果您真的想使用插件在test阶段,中作为单元测试运行,则必须跳过maven-surefire-plugin的默认执行,然后定义创建数据库的Maven插件的执行,maven-surefire-plugin的新执行以及删除数据库的Maven插件的执行,绑定到test阶段.

And, if you really want to run that as unit tests in the test phase, and using your plugin, you will have to skip the default execution of the maven-surefire-plugin and then define an execution of your Maven plugin creating the database, a new execution of the maven-surefire-plugin and an execution of your Maven plugin dropping the database, bound to the test phase.

之所以可行,是因为Maven按的顺序调用插件.当它们绑定到同一个阶段时,它们就是在POM中定义的.

This works because Maven invokes the plugins in the order as they are defined in the POM when they're bound to the same phase.

配置如下:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <executions>
    <execution>
      <id>default-test</id>
      <configuration>
        <skip>true</skip>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId><!-- group id of your plugin --></groupId>
  <artifactId><!-- artifact id of your plugin --></artifactId>
  <version><!-- version --></version>
  <executions>
    <execution>
      <id>create-db</id>
      <phase>test</phase>
      <goals>
        <goal><!-- your goal --></goal>
      </goals>
      <!-- add configuration -->
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <executions>
    <execution>
      <id>test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId><!-- group id of your plugin --></groupId>
  <artifactId><!-- artifact id of your plugin --></artifactId>
  <version><!-- version --></version>
  <executions>
    <execution>
      <id>drop-db</id>
      <phase>test</phase>
      <goals>
        <goal><!-- your goal --></goal>
      </goals>
      <!-- add configuration -->
    </execution>
  </executions>
</plugin>

这篇关于在测试阶段之后将始终执行哪个Maven阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆