Working With a Web Application in Maven.
In This article , we will be seeing how we can create a sample web application in maven and deploy that in tomcat server .So we will start creating a web application in maven by executing the command ,
mvn archetype:generate -DgroupId=com.SampleWeb.WebApplication -DartifactId=WebApplication -Dpackage=com.SampleWeb.WebApplication -Dversion=1.0-SHAPSHOT
We will use archeTpye#19 for creating a web application in maven. Once we execute the command, we will see the POM file like this,
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.SampleWeb.WebApplication</groupId>
<artifactId>WebApplication</artifactId>
<packaging>war</packaging>
<version>1.0-SHAPSHOT</version>
<name>WebApplication Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>WebApplication</finalName>
</build>
</project>
We have web.xml in WEB-INF which contains,
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
Now we will add a simple servlet to the web application, for this we need to create the package as we defined in the command [for creating a web project above] .The structure looks like this after package creation.
So I have created the “SampleServlet” in web directory. We need to update the web.xml also like
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>com.SampleWeb.WebApplication.web.SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
</web-app>
So now we will try to execute the command “mvn compile” to compile all the source files. This command throws the exception, since we don’t have the libraries available
[INFO] ------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------
[INFO] Compilation failure
C:\WebApplication\src\main\
C:\WebApplication\src\main\
C:\WebApplication\src\main\
C:\WebApplication\src\main\
So for this we will make sure that maven downloads the required artifacts [jars] for compiling the source code. We need servlet-api.jar and jsp-api.jar. In order to make sure the jars are available to the maven, we need to make some modifications to the POM file like adding the dependencies. The complete POM looks like
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.SampleWeb.WebApplication</groupId>
<artifactId>WebApplication</artifactId>
<packaging>war</packaging>
<version>1.0-SHAPSHOT</version>
<name>WebApplication Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>WebApplication</finalName>
</build>
</project>
I just added the dependencies for the servlet and jsp api.But how can we find the groupId,artifactID and version e.t.c. for these details go to
We have an artifact Search available, by which we can search the artifact by the name and many other ways. We can search for any type of artifact in this.
Once we made the changes to the POM file, we will once again run “mvn clean install” .once this executes it downloads all the necessary artifacts and compiles the source code, create a target directory and also creates a war file with the name specified in the finalName element in Build.
So now we have created a sample web application which contains a simple servlet.Now we will see how we can deploy the war file in Tomcat and Jetty Server using plug-ins.
Deploying an Application in Tomcat
In order to deploy the application in tomcat, we need to make use of the tomcat plug-in.for using the tomcat plug-in we need to make some changes to the POM file for downloading the necessary plug-in. So add the plug-in element to the build element in POM file like
<build>
<finalName>WebApplication</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:29801/manager/html</url>
<server>netcontrol</server>
<path>/simple</path>
</configuration>
</plugin>
</plugins>
</build>
So now we have added the plug-in, we need to run the install command to download the plug-in. so we execute the “mvn clean install”.This command makes sure to download the necessary tomcat plugin.lets see the configuration part in plug-in element for tomcat.
url : the url of the tomcat .
path is the web-path defined in the url-pattern of the web.xml.
The server element is the important thing, in order to provide the authentication to the tomcat server; we will create a server element in the setting.xml file which is in c: drive. Go for the user and look for .m2 directory which contains the setting.xml file. So once you find the file, add the servers element to the file like
<servers>
<server>
<id>netcontrol</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
We have specified a server name which has the user name and password for authentication. Now execute the “mvn -e clean tomcat:deploy” . so the info in the console may look like ,
mvn -e clean tomcat:deploy
+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'tomcat'.
[INFO] ------------------------------------------------------------------------
[INFO] Building WebApplication Maven Webapp
[INFO] task-segment: [clean, tomcat:deploy]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Preparing tomcat:deploy
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to C:\WebApplication\target\classes
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] [war:war]
[INFO] Packaging webapp
[INFO] Assembling webapp[WebApplication] in [C:\WebApplication\target\WebApplication]
[INFO] Processing war project
[INFO] Webapp assembled in[63 msecs]
[INFO] Building war: C:\WebApplication\target\WebApplication.war
[INFO] [tomcat:deploy]
[INFO] Deploying war to http://localhost:29801/simple
[INFO] OK - Deployed application at context path /simple
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
It deploys the web application at the /simple path and shows in the tomcat.for more information on tomcat plug-in, go to http://mojo.codehaus.org/tomcat-maven-plugin/
Some more articles coming, So Happy Coding…..
No comments :
Post a Comment