Maven : A simple webApp

In the last post we covered basic maven commands. Here we will create a simple web app. Lets get the project structure:

$> mvn archetype:generate -Dfilter=webapp-j2ee14

Notice that I am using a filter here as I dont want to get the whole list of archetype and then search it. The filter use the following format: [groupId:]artifactId. If you use single word without :, only artifactId will be checked. The filtering applied is a case sensitive contains on the artifactId (and groupId if set).

Maven06-webApp

Maven07-webAppProjectStructure

Heres the 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>com.webapp.basic</groupId>
 <artifactId>mywebapp</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>

<name>mywebapp</name>
 <url>http://maven.apache.org</url>

<dependencies>
 <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.1</version>
 <scope>provided</scope>
 </dependency>
 </dependencies>

<build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.0.2</version>
 <configuration>
 <source>1.4</source>
 <target>1.4</target>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

Notice, the packaging is ‘war‘ now. Also there are dependencies of servlet and jsp. Make a note of the scope of these dependencies (provided), we will get to it shortly. There is a ‘plugin‘ node. This is the compiler information. Here it tells maven that which version of java to be used for compilation. Here it says to use 1.4 version. If this node is not there, maven takes the default. For maven version 3 (that we are using), the default java version is 5. For an older version maven 2, the default version is 1.4.

To add on other dependencies, you can search in google for the correct groupid-artifactId-version that need to be added in pom.xml.

Now lets compile this, it will download the dependencies. Again make sure you are at the root of the project where your pom.xml is. After compile do a package, or you can directly call package

$>mvn package

Maven08-webAppProjectStructureAfterPackage

Lets deploy this on tomcat.

Note- When you run Tomcat from Eclipse, it won’t show you the default page at localhost:8080/. Run it outside of Eclipse to see that page. Also, you should have a admin password. By default, Tomcat does not enable admin or manager access. To enable it, you have to edit the ‘%TOMCAT_FOLDER%/conf/tomcat-users.xml' manually. Like add following (tomcat 7) :


<role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>

 <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

Now deploy the war from admin console and test:

Maven09-webAppDeployed

Lets automate this a bit.

The tomcat credential needs to be provided to maven for an automatic deployment. This is done in settings.xml (%mavenpath%\apache-maven-3.1.1\conf) on maven side. Add the following:


<server>
 <id>tomcat</id>
 <username>admin</username>
 <password>admin</password>
 </server>

Note that ‘server’ node should be inside ‘servers’ node in setting.xml. We can directly add username/password in our pom.xml, byt lets stick to this best practice.

Add the following in your pom.xml:


<build>
 <plugins>
 <plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>tomcat-maven-plugin</artifactId>
 <configuration>
 <url>http://localhost:8080/manager/text</url>
 <server>tomcat</server>
 <path>/mywebapp</path>
 </configuration>
</plugin>
 </plugins>
 </build>

Above is for tomcat 7. The url will change for previous version.

In context.xml (in your conf folder in tomcat) , though this is optional and is not related to maven3 deployment to tomcat7 ,sometimes jar locking might happen,so to be on the safer side.

<Context antiJARLocking="true" antiResourceLocking="true">

 

Now issue
$ mvn tomcat:deploy  //If you are deploying for the first time and that doesnt exsist
$mvn tomcat:redeploy //If the path already exsist

Remember to start tomcat before maven deployment. If deployment is success , your application will be available at http://localhost:8080/mywebapp/

Let us see now the scope node in dependencies which we left earlier.

Dependency scope includes dependencies as per the current stage of the build. Various Dependency Scopes are:

  • compile: This scope indicates that dependency is available in classpath of project. It is default scope.
  • provided: This scope indicates that dependency is to be provided by JDK or web-Server/Container at runtime.
  • runtime: This scope indicates that dependency is not required for compilation, but is required during execution.
  • test: This scope indicates that the dependency is only available for the test compilation and execution phases.
  • system: This scope indicates that you have to provide the system path.
  • import: This scope is only used when dependency is of type pom. This scope indicates that the specified POM should be replaced with the dependencies in that POM’s section.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: