Tuesday, November 12, 2013

How to deploy project in tomcat7 using maven



How to deploy project in tomcat7 using maven.

It’s always pain to get the project war and deploy it into tomcat webapp directory, and on top of that stop tomcat and run it for each deployment. If you are doing this in eclipse no need to laugh as in production you have to go through this manual process.
Now if MAVEN is taking care about dependency why not this task also.

I have tried to explain it with ATM analogy, if it doesn’t fit please spare me.

There are mainly 3 steps which you have to perform to do this.

Step1 :
Think in this way, if you want to withdraw money from ATM, say SBI what do you require
  •    ATM Card
  •   PIN
Now suppose you got both, you visited SBI ATM and error flashed, invalid PIN.
What could be the reason?

  •    PIN might be wrong
  •   Someone already changed the PIN
  •   If both not than bank DB doesn’t have entry for your ATM.

Same will be the case if you want to deploy project into tomcat using maven. Tomcat has to allow you for deployment that means you should be authorized user.

To get authorized user make below changes into %TOMCAT_HOME%/conf/tomcat-users.xml file.

<tomcat-users>
<role rolename="admin"/>
  <role rolename="dev"/>
  <role rolename="manager"/> 
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <role rolename="manager-jmx"/>
  <role rolename="manager-status"/>
  <user username="sushil" password="sushil"
 roles="admin,dev,manager,manager-gui,manager-script,manager-jmx,manager-status"/>
  <user username="raj" password="raj" roles="dev"/>
  <user username="avishek" password="avishek" roles="dev"/>
</tomcat-users>

Now 'sushil' is authorized to deploy into tomcat.

Step 2:
Maven has to deploy it on behalf of you into tomcat so you have to delegate the control to maven. 

Take this analogy:

Your access has been updated into bank DB. For you to have access, you should get ATM Machine (MAVEN) to withdraw money, but to do that ATM require you to swipe ATM Card and enter PIN means it want card number and PIN and also so the which BANK this ATM card is, for transaction.

Similarly you have to tell MAVEN which user(ATM CARD) with credentials(PIN) and which server (BANK).

So add below lines into %MAVEN_HOME%/conf/settings.xml

<servers>
    <server>
      <id>TomcatServer</id>
      <username>sushil</username>
      <password>sushil</password>
    </server>
 </servers>

Step3:

step1 and step2 done you have ATM and PIN.
What to do next go to ATM and withdraw money if you have in account. Give the details  type of account, amount and do you want receipt etc. that means do the actual things.
In any MAVEN project actual is pom.xml so add the below things into MAVEN.

<plugins>
         <plugin>
                 <groupId>org.apache.tomcat.maven</groupId>
                 <artifactId>tomcat7-maven-plugin</artifactId>
                 <version>2.0</version>
                 <configuration>
                           <url>http://127.0.0.1:8080/manager/text</url>
                           <server>TomcatServer</server>
                           <path>/myProjects</path>
                           <update>true</update> <!—if you want to redeploy-->
                  </configuration>
          </plugin>
------------------------
---------------------------
---------------------------  
<plugins>

If you have done all the above set steps properly, just go to project folder on command prompt and type

mvn tomcat7:deploy.


WOLA… it’s done, you can easily deploy projects using maven.

No comments: