Wednesday, June 11, 2014

Auto deploy for Tomcat

Auto deployment is a feature we do need to run our test cases. I needed an way to auto deploy web project to tomcat and test it. To to this programatically there are several ways. Lets find out them.

1) Using Embed tomcat

this is the best option I recommend to any one who tries to do that. I will show you the code snippet for it.

 package org.wso2.tomcat;  
 import org.apache.catalina.Context;  
 import org.apache.catalina.LifecycleException;  
 import org.apache.catalina.core.StandardHost;  
 import org.apache.catalina.startup.Tomcat;  
 /**  
  * Created by waruna on 4/17/14.  
  */  
 public class TravelocityDeployer {  
   public static void main(String[] args) throws Exception {  
     Tomcat tomcat = new Tomcat();  
     tomcat.getService().setContainer(tomcat.getEngine());  
     tomcat.setPort(8080);  
     tomcat.setBaseDir(".");  
     StandardHost stdHost = (StandardHost) tomcat.getHost();  
     stdHost.setAppBase(".");  
     stdHost.setAutoDeploy(true);  
     stdHost.setDeployOnStartup(true);  
     stdHost.setUnpackWARs(true);  
     tomcat.setHost(stdHost);  
     System.setProperty("javax.net.ssl.trustStore", "/home/waruna/Products/wso2is-4.7.0/repository/resources/security/wso2carbon.jks");  
     System.setProperty("javax.net.ssl.trustStorePassword",  
         "wso2carbon");  
     System.setProperty("javax.net.ssl.trustStoreType", "JKS");  
     try {  
       Context ctx = tomcat.addWebapp( tomcat.getHost() ,"/travelocity.com", "/home/waruna/tomcatembed/webapps/travelocity.com.war");  
       tomcat.start();  
       HTTPTestClient httpClient = new HTTPTestClient();  
       httpClient.loginToTravelocityApp();  
       tomcat.stop();  
     } catch (LifecycleException e) {  
       e.printStackTrace();  
     }  
   }  
 }  



Best thing in this approach is you do not need to have tomcat installed in your machine. Here I used following dependencies in my pom.xml.
     <dependency>  
       <groupId>org.apache.tomcat</groupId>  
       <artifactId>tomcat-catalina</artifactId>  
       <version>7.0.28</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.tomcat.embed</groupId>  
       <artifactId>tomcat-embed-jasper</artifactId>  
       <version>7.0.28</version>  
     </dependency>  



2) Using Cargo Maven plugin

Cargo is an maven plug-in that I came across when I wanted to do this. You can find more on here. The issue I found here is that you need a running Tomcat container here which is really not matching my requirement.

3) Using Curl

curl -T "myapp.war" "http://manager:manager@localhost:8080/manager/text/deploy?path=/myapp&update=true"



this is again using running tomcat instance. There may be many other ways. Please do add them in comments.

No comments:

Post a Comment