Which maven phase will be always executed after te

2019-07-18 11:56发布

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.

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).

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

1条回答
欢心
2楼-- · 2019-07-18 12:24

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.

From the docs:

  • 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

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: 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.

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


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.)


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.

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.

A configuration would look like:

<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>
查看更多
登录 后发表回答