[Maven] How to create a simple java webapp through maven

This post will take you through the sample steps to take to create a simple java webapp through maven. Doing so will allow the developer to easily inject the dependencies of the webapp and concentrate directly on developing the webapp rather than finding the dependencies for the project.

Prerequisites

JDK 7 or higher Download link
Maven 3+ Download link

Create a maven project

Since I have already done a post on Creating a Java webapp through maven I will not redo it again. Please refer [subject] on to creating a java webapp.

For this sample we will create a simple java webapp using the maven. Creating the project can be done as follows (for more information check this link);

mvn archetype:create
 -DgroupId=[prefered group id]
 -DartifactId=[webapp name]
 -DarchetypeArtifactId=maven-archetype-webapp

Here is an example;

mvn archetype:create
 -DgroupId=com.nu1silva.sample
 -DartifactId=sampleapp
 -DarchetypeArtifactId=maven-archetype-webapp

NOTE: Changes can be done to the groupId and artifactId at the user’s preference.

Lets run the and see the output;

[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ————————————————————————
[INFO]
[INFO] — maven-archetype-plugin:2.2:create (default-cli) @ standalone-pom —
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: com.nu1silva.sample
[INFO] —————————————————————————-
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE
[INFO] —————————————————————————-
[INFO] Parameter: groupId, Value: com.nu1silva.sample
[INFO] Parameter: packageName, Value: com.nu1silva.sample
[INFO] Parameter: package, Value: com.nu1silva.sample
[INFO] Parameter: artifactId, Value: sampleapp
[INFO] Parameter: basedir, Value: /home/nuwan/
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/nuwan/sampleapp
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 0.960 s
[INFO] Finished at: 2015-10-04T08:24:41+05:30
[INFO] Final Memory: 14M/620M
[INFO] ————————————————————————

The build was SUCCESS! Viewing the projects structure, It will be as follows;

Project Structure
Project Structure

Now, to make this a standalone server we use maven jetty plugin. Add the following plugin details to pom.xml between the <build></build> tags.

 <plugins>
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>3.1</version>
       <configuration>
          <source>1.6</source>
          <target>1.6</target>
       </configuration>
    </plugin>
    <plugin>
       <groupId>org.mortbay.jetty</groupId>
       <artifactId>maven-jetty-plugin</artifactId>
       <version>6.1.10</version>
       <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <stopKey>foo</stopKey>
          <stopPort>9999</stopPort>
       </configuration>
       <executions>
          <execution>
             <id>start-jetty</id>
             <goals>
                <goal>run</goal>
             </goals>
             <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <daemon>true</daemon>
             </configuration>
          </execution>
          <execution>
             <id>stop-jetty</id>
             <phase>post-integration-test</phase>
             <goals>
                <goal>stop</goal>
             </goals>
          </execution>
       </executions>
    </plugin>
 </plugins>

Lets run the maven project and check the results;

mvn clean jetty:run

The build will initiate and start the jetty server;

...
...
[INFO] --- maven-jetty-plugin:6.1.10:run (default-cli) @ sampleapp ---
[INFO] Configuring Jetty for project: sampleapp Maven Webapp
[INFO] Webapp source directory = /home/nuwan/temp/sampleapp/src/main/webapp
[INFO] web.xml file = /home/nuwan/temp/sampleapp/src/main/webapp/WEB-INF/web.xml
[INFO] Classes = /home/nuwan/temp/sampleapp/target/classes
[INFO] Logging to org.slf4j.impl.SimpleLogger(org.mortbay.log) via org.mortbay.log.Slf4jLog
[INFO] Context path = /sampleapp
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] Webapp directory = /home/nuwan/temp/sampleapp/src/main/webapp
[INFO] Starting jetty 6.1.10 ...
[INFO] jetty-6.1.10
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 10 seconds.

Now goto http://localhost/8080/sampleapp. You can see the application running.

As shown in this tutorial, it is easy to create and run a java webapp through maven. any questions of suggestions please leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.