Wednesday, August 20, 2014

Run maven plugin in java code

This is some thing I wanted to do. My initial requirement was to run a maven plugin within a run time of another plugin.  So i found the way of running the maven plugin via java code.

Use Case

I wanted to integrate running soapui projects within integration tests in a WSO2 product run time. What I did was to have a separate pom.xml with the plugin configuration that I wanted to run and call it within a test case. The test case is executed by the maven surefire plugin.


Here is the sample code snipet.

 InvocationRequest request = new DefaultInvocationRequest();  
     request.setPomFile( new File(FrameworkPathUtil.getSystemResourceLocation() + "artifacts" +  
         File.separator + "AS" + File.separator + "soapprojects" +  
         File.separator + "pom" + File.separator + "pom.xml") );  
     request.setGoals(Arrays.asList("clean", "install"));  
     Invoker invoker = new DefaultInvoker();  
     invoker.setMavenHome(new File("/home/waruna/apache-maven-3.0.5"));  
     invoker.execute( request );  

Run soapui project using maven plugin

SoupUI is a very useful tool that you can  use for test web and other services. Users can create soapui project to send requests and do validations on that. We need to find a way to automate running these soapui projects, so that the test case writer can easily integrate many tests created with soapui to the existing maven projects. In the following steps we will describe how to automate running projects created with soapui within a maven project.

Create your soapui project


You have to create your soapui project first. In the example project we have given here, used existing wsdl files provided with echo and version services shipped with WSO2 ESB.  When you create project with soapui, you have to make sure to pick the option of creating test cases with it.


1.jpg

Add Assertions with soapui projects


You need to configure assertions which needs to be validated against the inputs given in the test cases. If assertion occurs you can view it within the maven build. To create assertion follow the document [1].

Configure maven project


Once you have finished creating soapui project and its assertions you have to save it. The output file will be in xml format. To run soapui project file we use the Maven Soapui Extension Plugin [2].  In this plugin you can run multiple soapui projects to run. In order to do that you have to set the goal as test-multi. Given below is the sample configuration for that.


 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
   <modelVersion>4.0.0</modelVersion>  
   <groupId>com.smartbear.samples</groupId>  
   <artifactId>soapui-maven2-plugin</artifactId>  
   <packaging>jar</packaging>  
   <version>1.0-SNAPSHOT</version>  
   <name>Maven 2 SoapUI Sample</name>  
   <url>http://maven.apache.org</url>  
   <pluginRepositories>  
     <pluginRepository>  
       <id>SmartBearPluginRepository</id>  
       <url>http://www.soapui.org/repository/maven2/</url>  
     </pluginRepository>  
   </pluginRepositories>  
   <build>  
     <plugins>  
       <plugin>  
         <groupId>com.github.redfish4ktc.soapui</groupId>  
         <artifactId>maven-soapui-extension-plugin</artifactId>  
         <version>4.6.4.1</version>  
         <executions>  
           <execution>  
             <id>soapUI1</id>  
             <phase>test</phase>  
             <goals>  
               <goal>test-multi</goal>  
             </goals>  
             <configuration>  
               <projectFiles>  
                 <scan>  
                   <baseDirectory>/home/waruna/workspace/soapuitest/src/main/resources/projects</baseDirectory>  
                   <includes>  
                     <include>*.xml</include>  
                   </includes>  
                   <excludes>  
                     <exclude>**/*fail-*-soapui-project.xml</exclude>  
                     <exclude>**/composite-projects/**</exclude>  
                   </excludes>  
                 </scan>  
               </projectFiles>  
               <outputFolder>/home/waruna/workspace/soapuitest/src/main/resources/</outputFolder>  
               <junitReport>true</junitReport>  
               <useOutputFolderPerProject>true</useOutputFolderPerProject>  
             </configuration>  
           </execution>  
         </executions>  
       </plugin>  
     </plugins>  
   </build>  
 </project>  
[2]http://mvnrepository.com/artifact/com.github.redfish4ktc.soapui/maven-soapui-extension-plugin