Running the exec-maven plugin several times in one phase

I am trying to test a client / server application and use Maven to handle build / testing / deployment. To test the application, I need:

  • run the installer script (to install the server)
  • run the start command (to start the service),
  • run the test (maven-surefire-plugin),
  • stop the service and
  • delete the service.

Steps 1,2,4 and 5 will use the maven-exec plugin. Step 3 will use the maven-surefire plugin.

The problem is that all 5 of these steps will be performed during the testing phase. Maven allows you to create plugins in a specific order. The exec plugin can be run multiple times using multiple entries. The problem is that I need to use a valid plugin in the middle of exec-plugin execution.

Has anyone ever come across this before or knows how to structure the plugin and execution?

+7
source share
2 answers

What you are trying to do is more like an integration test than a unit test. For this use case, the default maven lifecycle has three phases related to integration testing:

  • pre-integration-test : perform the steps necessary to run integration tests. This may include things like setting up your environment.
  • integration-test : process and deploy the package, if necessary, in an environment where integration tests can be run.
  • post-integration-test : perform the steps necessary after completing the integration tests. This may include cleaning the environment.

The surefire plugin typically runs in the test phase, but can be reconfigured to run in a different phase. Then your steps 1 + 2 can be completed to execute in pre-integration-test , and steps 4 + 5 should be done in post-integration-test .

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <!-- Skip execution in test phase and execute in integration-test phase instead --> <configuration> <skip>true</skip> </configuration> <executions> <execution> <id>surefire-it</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <skip>false</skip> </configuration> </execution> </executions> </plugin> 
+4
source

This is how I configured exec and fail-safe plugins. I am using a fail-safe system rather than reconfiguring confidence, as surefire still runs other tests that are in the testing phase. This will perform steps 1 and 2 in the pre-integration phase (several executions listed for the same phase will be performed in the indicated order), run the test in the integration step and then clear steps 3 and 4 in the post-integration step.

(Note: I have echo commands instead of real install and cleanup commands)

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> </goals> </execution> </executions> <configuration> <forkMode>always</forkMode> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <id>step1</id> <goals> <goal>exec</goal> </goals> <phase>pre-integration-test</phase> <configuration> <executable>echo</executable> <arguments> <argument>foo</argument> </arguments> </configuration> </execution> <execution> <id>step2</id> <goals> <goal>exec</goal> </goals> <phase>pre-integration-test</phase> <configuration> <executable>echo</executable> <arguments> <argument>bar</argument> </arguments> </configuration> </execution> <execution> <id>step3</id> <goals> <goal>exec</goal> </goals> <phase>post-integration-test</phase> <configuration> <executable>echo</executable> <arguments> <argument>baz</argument> </arguments> </configuration> </execution> <execution> <id>step4</id> <goals> <goal>exec</goal> </goals> <phase>post-integration-test</phase> <configuration> <executable>echo</executable> <arguments> <argument>woot</argument> </arguments> </configuration> </execution> </executions> </plugin> 
+10
source

All Articles