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.

Tuesday, September 3, 2013

Custom JIRA REST JAVA CLIENT (JRJC) using Jersey


JIRA Setup:

JIRA is not an acronym; it’s a word in itself and an Atlassian product used for issue tracking. You might have used lots of bug (issue) tracking tool like BugZilla, HP QC, IBM Rational ClearQuest, Team Foundation Server (TFS), etc. from a long list of tracking tools. JIRA is one among them.

If you want to practice on your system before hitting the production server, you can get the evaluation licensed JIRA server from here.

You have to register your email id and you will get 30 days license, to start working with JIRA.

Once downloaded, you can install JIRA on your machine following this.

After installation, you can configure your JIRA server using this.

Once setup is done, you can follow this guide to create projects, users, roles, tasks, bugs etc. things involved in project management.

If you are a normal user of JIRA (not an administrator), this guide could be useful.

I am hoping everything is set and you have JIRA installed, configured, TESTJIRA project created.

pics of my project:



Web Services via REST (Representational State Transfer):

We term any service provided via web as web service; it is not a new technology, since the invention of internet we are doing this only, servicing via web. Google, IRCTC, Yahoo, WebMail are the famous web services we are using, without knowledge (it’s amazing  J J).

Now if we are using web services since 1960s than why this buzz for SOAP and REST Web Services? What is so special in it and why I am writing for JRJC?

Previously in the web world we were accessing a complete web site of each provider and fill the forms and get our work done. To build those web sites each and every provider has to follow a pre-set rules to build a web site (site should follow this design pattern, should use this technology, should be hosted on this container and so on), means a full-fledged solution.

Now suppose, IRCTC wants to provide a search service to users. Previously, either it has to create a search engine by itself or integrate Google search API (obviously provided by Goole- hypothetical assumption), which is going to be either tedious task and also might be costly solution.

“It’s always good to use existing wheels instead of reinventing“ – this is the philosophy of web Services.

You want to use Google search, just use the services provided by Google in the form of REST Service.
REST follows our age old client-server (service provider) model. Server will create the service and client will consume it and to facilitate this process we have our java api named JAX-RS. JAX-RS is only standard, we have to use some implementation API like Jersey, Restlet, CXF etc. here I am using Jersey.

As service provider has to expose its services via set of URIs i.e REST URIs for client to consume it, here in our case JIRA is service provider and JIRA has provided a REST API which contain set of URIs to consume the services provided by it.

The general REST URI architecture for JIRA services is as below

http://hostname/rest/<api-name>/<api-version>/<resource-name>

*  The current version of the JIRA REST API is "2".
The <api-name> part of the URI is the name of the JIRA REST API, which is simply api.
*  JIRA's REST API resources have names like "issue", "user" or "attachment".
 For instance, putting all of the above together, the URI to an issue with the key TJ-1 would look like:

                                      http://lacalhost:9090/rest/api/2/issue/TJ-1
 
For detailed JIRA REST API tutorial refer this.
 

Now let’s start Coding.

Create a basic java project and add required maven dependencies.

Code Structure:

POM.xml

<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>JiraRestExample</groupId>
  <artifactId>JiraRestExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
        <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-client</artifactId>
                <version>1.16</version>
        </dependency>
        <dependency>
               <groupId>net.sf.json-lib</groupId>
               <artifactId>json-lib</artifactId>
               <version>2.4</version>
               <classifier>jdk15</classifier>
        </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
     </build>
</project>
 
Client1.java

/*
*
* Custom JIRA REST JAVA CLIENT (JRJC) using Jersey
* Created by: Sushil K Madwani
* Date: 09/03/2013
* 
*/

package com.apollo.jiraRest;

import javax.naming.AuthenticationException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.Base64;

class Client1 {
 
               /*
                  provide admin / user credentials to access the JIRA.
                */
             static String auth = new String(Base64.encode("smadwani:12071985"));

           /*
                * url for creating an issue using REST API ---- C of CRUD
                * */
              static String url = "http://localhost:9090/rest/api/2/issue";
              
               /*
                * JSON for creating an issue using REST API 
                *
                * */
                          static String data = "{\"fields\"" +
                                                            ":{\"project\"" +
                                                                  ":{\"key\":\"TJ\"}," +
                                                                 "\"summary\":\"REST Test \",\"issuetype\":{\"name\":\"Task\"}}}";
     
               /*
                * url for retrieving an issue using REST API ---- R of CRUD
                *
                * */
              static String url1 = "http://localhost:9090/rest/api/2/issue/TJ-1";
                /*
                * url for updating an issue using REST API ---- U of CRUD
                *
                * */
               static String url2 = "http://localhost:9090/rest/api/2/issue/TJ-1";
              
               /*
                * JSON data to be updated for an issue using RESP API                
                * 
                * */
                 static String data1 = "{\"fields\":{\"assignee\":{\"name\":\"vinodh\"}}}";
            
               /*
                * url for deleting an issue using RESP API ---- D of CRUD
                *
                * */
              static String url3 = "http://localhost:9090/rest/api/2/issue/TJ-1";
             /*
                * HTTP POST method is used to create the issue
                * 
                * */
                private static String invokePostMethod(String auth, String url, String data) throws AuthenticationException, ClientHandlerException {
                   Client client = Client.create();
                   WebResource webResource = client.resource(url);
                   ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").post(ClientResponse.class, data);
                   int statusCode = response.getStatus();
                   if (statusCode == 401) {
                       throw new AuthenticationException("Invalid Username or Password");
                   }
                   return "issue successfully created";   
               }
                           
               /*
                * HTTP GET method is used to get the issue
                * 
                * */
               private static String invokeGetMethod(String auth, String url) throws AuthenticationException, ClientHandlerException {
                   Client client = Client.create();
                   WebResource webResource = client.resource(url);
                   ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").get(ClientResponse.class);
                   int statusCode = response.getStatus();
                   if (statusCode == 401) {
                       throw new AuthenticationException("Invalid Username or Password");
                   }
                   return response.getEntity(String.class);
               }
            
               /*
                * HTTP PUT method is used to update the issue
                * 
                * */
            private static String invokePutMethod(String auth, String url, String data1) throws AuthenticationException, ClientHandlerException {
                                   Client client = Client.create();
                                   WebResource webResource = client.resource(url);
                                   ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").put(ClientResponse.class, data1);
                                   int statusCode = response.getStatus();
                                   if (statusCode == 401) {
                                       throw new AuthenticationException("Invalid Username or Password");
                                   }
                                   return "success";
                               }             
              /*
                * HTTP DELETE method is used to delete the issue
                * 
                * */
              private static String invokeDeleteMethod(String auth, String url) throws AuthenticationException, ClientHandlerException {
                   Client client = Client.create();
                   WebResource webResource = client.resource(url);
                   ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json").accept("application/json").delete(ClientResponse.class);
                   int statusCode = response.getStatus();
                   if (statusCode == 401) {
                       throw new AuthenticationException("Invalid Username or Password");
                   }
                   return "successfully deleted";
               }             
        
              public static void main(String[] args) throws Exception {
                              /*
                                * 1. Creating an JIRA issue in project with key TJ using REST API
                                * 
                                */
                            
                              System.out.println(invokePostMethod(auth, url, data));
                                                   
                                /*
                                * 2. Getting details of an JIRA issue in project with issue no TJ-1 using REST API
                                */
                               
                              String resp=invokeGetMethod(auth, url1);                        
                              
                              JSONObject jo=(JSONObject)JSONSerializer.toJSON(resp);
                             
                               JSONObject sum=jo.getJSONObject("fields");
                             
                               System.out.println(sum.getString("summary"));
                                                           
                               /*
                                * 3. Updating details of an JIRA issue in project with issue no TJ-1 using REST API
                              */
                          
                               System.out.println(invokePutMethod(auth, url2, data1));
                                                          
                               /*
                                * 4. Deleting an JIRA issue in project with issue no TJ-1 using REST API
                                */                           
                               
                               System.out.println(invokeDeleteMethod(auth, url3));
                   }
}
 
 OUTPUT :

Creating an issue

i.               JIRA status before creating an Issue
ii.             Code to create an issue.
 
iii.           JIRA status after creating issue.
 
 Retrieving an Issue
 
i.               Code to retrieve the issue in JSON format.

Updating an Issue
i.               JIRA Status before updating 

ii.             Code to update the Issue.
iii.       JIRA Status after update
Deleting an Issue
i.               JIRA Status before deleting 
ii.             Code to delete the Issue. 
iii.           JIRA Status after delete.

For detailed JIRA REST API, please go here.