Showing posts with label Deploy. Show all posts
Showing posts with label Deploy. Show all posts

Monday, 13 August 2012

What is web.xml (Deployment Descriptor) in Java?

In Java, all web applications have a web.xml file, also called deployment descriptor, in the \META-INF directory. The main purpose of this file is to provide information about the web application to deploy in the container.

It can be used to declare and configure servlets belonging to the application via descriptor elements. For example, tt helps setting filters, icons or define default welcome pages too.

Here is a web.xml example:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">

  <display-name>My Application</display-name> 
  <description>My application description</description>

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>somepackage.MyServletClass</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServletMapping</url-pattern>
  </servlet-mapping>

</web-app>
It defines a web application with one servlet called MyServlet. User requests to:

  http://www.mywebserver.com/MyAppPath/MyServletMapping/...

are sent to MyServlet. The MyAppPath application path is typically defined in the context.xml file. Both files serve different purpose and should not be confused with each other.

Friday, 27 July 2012

How to Deploy a .war to Tomcat 7 via Maven?

In order to deploy a .war to Tomcat 7 during a maven build process, the cargo plugin is very useful.

One should add the following plugin in your pom.xml:
  <plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <version>1.1.2</version>
    <configuration>
      <container>
        <containerId>tomcat7x</containerId>
        <type>remote</type>
      </container>
      <configuration>
        <type>runtime</type>
        <properties>
          <cargo.remote.uri>
            http://localhost:8080/manager/text
          </cargo.remote.uri>
          <cargo.remote.username>admin</cargo.remote.username>
          <cargo.remote.password>admin</cargo.remote.password>
        </properties>
      </configuration>
      <deployer>
        <type>remote</type>
        <deployables>
          <deployable>
            <groupId>com.my-group-id</groupId>
            <artifactId>my-artifact</artifactId>
            <type>war</type>
          </deployable>
        </deployables>
      </deployer>
    </configuration>
  </plugin>

The remote URI is configured to deploy the .war on a local Tomcat 7 installation listening to port 8080. The host could be a remote host too (one should use https instead for security).

The profile used to connect to maven must be configured with proper roles in the /conf/tomcat-users.xml file where Tomcat is installed, as following:
  <tomcat-users>
    <user name="admin" password="admin"
       roles="admin-gui,manager-gui,manager-script" />
    ...
  </tomcat-users>

Then you can use the following maven goals to deploy or redeploy your application:
  cargo:deployer-deploy
  cargo:deployer-redeploy
More Maven tips & tricks here.