Tuesday, 12 June 2012

Integration of Maven and ANT

The idea behind this post is to build through maven so we don't have dirty lib folders in our repository or don't have to worry about class paths either. 

Also some time you want to deploy locally as you are deploying in real production environment. 

Following is a sample build script:

pom.xml:



 4.0.0
 com.zohaib.research
 researchPrj
 war
 1.0-SNAPSHOT
 Maven Webapp
 http://maven.apache.org

 

  
   Maven central
   http://central.maven.org/maven2
  

  
   maven2
   http://repo2.maven.org/maven2
  

  
   maven.springframework
   http://maven.springframework.org/release
  


  
   spring-milestones
   http://repo.spring.io/milestone
   
    true
   
  


 


 
  
  true
  4.0.3.RELEASE
  1.5.2.RELEASE
  3.2.3.RELEASE
  4.3.5.Final
  2.3.2
  5.1.30

 

 


  
   javax.servlet
   jstl
   1.2
  

  
   javax.servlet
   javax.servlet-api
   3.0.1
   provided
  


  
   org.springframework
   spring-webmvc
   ${org.springframework-version}
  


  
   org.springframework.data
   spring-data-jpa
   ${org.springData-version}
  

  
   org.springframework
   spring-tx
   ${org.springframework-version}
  

  
   org.aspectj
   aspectjrt
   1.6.5
  

  
   org.aspectj
   aspectjweaver
   1.8.0.RELEASE
  


  
  
   org.springframework.security
   spring-security-web
   ${spring.security.version}
  

  
   org.springframework.security
   spring-security-config
   ${spring.security.version}
  

  
  
   org.springframework.security
   spring-security-taglibs
   ${spring.security.version}
  


  
   org.hibernate
   hibernate-core
   ${hibernate.version}
  
  
   org.hibernate
   hibernate-entitymanager
   ${hibernate.version}
  


  
   mysql
   mysql-connector-java
   ${mysql.connector.version}
  

  
   opensymphony
   sitemesh
   2.4.2
  

  
   cglib
   cglib-nodep
   2.2
  

  
   commons-collections
   commons-collections
   3.1
  

  
   commons-dbcp
   commons-dbcp
   1.3
  


  
   commons-beanutils
   commons-beanutils
   1.8.3
  

  
   org.apache.httpcomponents
   httpclient
   4.1.1
  

  
   commons-fileupload
   commons-fileupload
   1.2.2
  

  
   commons-io
   commons-io
   1.4
  

  
   commons-lang
   commons-lang
   2.5
  

  
   commons-pool
   commons-pool
   1.5.4
  

  
   org.codehaus.jackson
   jackson-mapper-asl
   1.9.3
  


  
   junit
   junit
   3.8.1
   test
  

 

 
  ROOT
  
   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.3.2
    
     1.6
     1.6
    
   
   
    org.apache.maven.plugins
    maven-war-plugin
    2.1.1
    
     false
    
   

   
    org.codehaus.mojo
    aspectj-maven-plugin
    1.5
    
    
     1.6
     1.6
     1.6
    
    
     
      
       compile
       test-compile
      
     
    
   

   
    org.apache.maven.plugins
    maven-site-plugin
    3.0
    
     
      
      
       org.codehaus.mojo
       cobertura-maven-plugin
       2.5.1
      
     
    
   
  
 



build.properties:


deployment.dir=C:/SOFTWARE_INSTALL/TESTING_TOMCAT/apache-tomcat-7.0.53/webapps
tomcat.home=C:/SOFTWARE_INSTALL/TESTING_TOMCAT/apache-tomcat-7.0.53
build.dir=../target
war.file.name=ROOT.war
maven.home=C:/MAVEN/apache-maven-3.0.4
catalina.fileName=catalina.bat
maven.fileName=mvn.bat

build.xml:



 
  ANT SCRIPT
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


 
 

 


 
  
   
  
 

 
  
   
  
 

 
  
   
  
 

 
  
   
  
 



 

 
  
  
 

 

  
  

  
  

   
   

  
  
   
  

 

 
  
  

  

  
   
  
  
  

  
   
   
   
  

  
   
  

  
   
   
   
  


 

 


 

 


 


 
  
  
  
  
 


 
  
   
  

  
  

   
   
  

 

 
  
  
   
   
  
 

 
  
  
  
 

 

 
  
  
    
   
   
   
      
   
   
  
 

 
  
  
   
   
  
 

 
  
   
   
  
 



 
  
  
  
       
  
 



 
  
  
  
  
  
  
   
    
   
  
 

 
  
  
  
  
  
  
 

 
  
  
  
  
  
  
  
  
 

 
  
  
 


 
  
  
  
 

 
  
  
  
  
  
  
  
  

 


Tuesday, 24 January 2012

Implementing virtual host in tomcat

Implementing virtual host in tomcat
Implementing Virtual Host in Tomcat

Say you have Java web application and you want to have “myApp-local.zohaib.umair.com” as a URL to access in tomcat. If you open up conf/server.xml you will notice that tomcat comes with default Host called “localhost” with the appBase “webapps” where you install web applications for default localhost host.

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
And you will also find that URL for the default application becomes:
And for the other sub-context deployed on this host becomes:

1) conf/server.xml configuration:


<Host appBase="vhosts/myApp-local.zohaib.umair.com"
name="myApp-local.zohaib.umair.com" unpackWARs="true" autoDeploy="true">

<!—If you want to log -->
      <Logger className="org.apache.catalina.logger.FileLogger"
            prefix="zohaib_log" suffix=".txt" timestamp="true" verbosity="4" />

      <Alias>myApp-local.zohaib.umair.com</Alias>
      <Alias>www.anotherAlias.net.au</Alias>
      <Valve className="org.apache.catalina.valves.AccessLogValve"
      directory="logs" prefix="myApp-local.zohaib.umair.com_access_log."
      suffix=".txt" pattern="common" resolveHosts="false" />

</Host>

Under ${tomcat-home} folder you need to create a folder vhosts/myApp-local.zohaib.umair.com as specified in above server.xml configuration. Here you actually place your ROOT.war.
Note ROOT.war is actually tomcat's convention. And conceptually you are actually creating a host and tomcat needs to know which is a default root war for that host. Therefore you must follow that convention.

In above configuration you are telling tomcat:That your defualt (ROOT.war) can be accessible via myApp-local.zohaib.umair.com or www.anotherAlias.net.au. Your default war (i.e ROOT.war) is located under ${tomcat}/vhosts/myApp-local.zohaib.umair.com. It could be war or exploded folder. And You want to log messages in zohaib_log.txt file with timestamp.


2) ${tomcat-home}/conf/Catalina/myApp-local.zohaib.umair.com/ROOT.xml


Under ${tomcat-home}/conf/Catalina you have to create folder myApp-local.zohaib.umair.com and in that folder you need to place ROOT.xml. Your ROOT.xml should be similar as follows. Main thing is you need to define CONTEXT:


<?xml version='1.0' encoding='utf-8'?>

<Context allowLinking="true" displayName="MyApp" docBase="ROOT.war"
     path="" workDir="work/Catalina/myApp-local.zohaib.umair.com/_">

<Resource name="myOracle" auth="Container" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@servername:1522:5MyDatabase"
username="zohaib" password="zohaib" removeAbandoned="true"
removeAbandonedTimeout="60" logAbandoned="true" maxActive="150"
maxIdle="20" maxWait="5000" validationQuery="select 1 from dual"
testOnBorrow="true" testOnReturn="true" testWhileIdle="true"
timeBetweenEvictionRunsMillis="1800000" numTestsPerEvictionRun="3"
minEvictableIdleTimeMillis="1800000" />

</Context>

3) Windows hosts file


If you are on windows and if you are running locally then you need to bind your alia to your IP in
C:\Windows\System32\drivers\etc\hosts
10.3.22.2                                         myApp-local.zohaib.umair.com
10.3.22.2                                         www.anotherAlias.net.au

Assume 10.3.22.2 is your IP and if you have deployed war you should able to access your app from above URLs