Create a J2EE Web Application project with Apache Maven

Apache Maven

In this tutorial, we will learn how to create a J2EE Web Application project with Apache Maven, imports it into the Eclipse IDE, and package the Java project into a war file.

Tools used :

  • Maven 3.2.5 (For how to install, click here)
  • Eclipse (Kepler Release)
  • JDK 7

A. Create a Java Project from Maven Template

In a terminal (*uix or Mac) or command prompt (Windows), navigate to the folder you want to create the Java project.

In the command prompt run this command-

mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Where {project-packaging} is the package structure you want for your project and {project-name} is the name of your project.

Example :-

$ mvn archetype:generate -DgroupId=com.jobs -DartifactId=jobs -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:2.0-alpha-3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:2.0-alpha-3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] --- maven-archetype-plugin:2.0-alpha-3:generate (default-cli) @ standalone-pom ---
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] Generating project in Interactive mode
Define value for version:  1.0-SNAPSHOT: : 
Confirm properties configuration:
groupId: com.jobs
artifactId: jobs
version: 1.0-SNAPSHOT
package: com.jobs
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.jobs
[INFO] Parameter: packageName, Value: com.jobs
[INFO] Parameter: package, Value: com.jobs
[INFO] Parameter: artifactId, Value: jobs
[INFO] Parameter: basedir, Value: /opt/dev/workspace
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /opt/dev/workspace/jobs
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.313 s
[INFO] Finished at: 2015-02-26T17:13:06+05:30
[INFO] Final Memory: 12M/246M
[INFO] ------------------------------------------------------------------------

In above case, a new J2EE project jobs, and the entire project directory structure is created automatically.

B. Project Directory Layout

With the above command the following project directory structure is created.

jobs
.
|-- pom.xml
`-- src
    `-- main
        |-- resources
        `-- webapp
            |-- index.jsp
            `-- WEB-INF
                `-- web.xml

Where src/main/webapp contains index.jsp and WEB-INF folder, src/main/webapp/WEB-INF/ contains web.xml and src/main/resources contains images/properties files .

In additional, a standard pom.xml is generated. It describes the entire project information, everything from directory structure, project plugins, project dependencies, how to build this project and etc.

To make this as an Eclipse project, in terminal, navigate to jobs project, type this command :

mvn eclipse:eclipse

And import this project in your Eclipse IDE.

To import the project into Eclipse IDE, select File -> Import… -> General->Existing Projects into Workspace

C. Build Web Application

Open command console, go the project directory and execute the following mvn command.

$ mvn clean package

Maven will start building the project.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building city-jobs Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jobs ---
[INFO] Deleting /opt/dev/workspace/jobs/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jobs ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jobs ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jobs ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /opt/dev/workspace/jobs/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ jobs ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jobs ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ jobs ---
[INFO] Packaging webapp
[INFO] Assembling webapp [city-jobs] in [/opt/dev/workspace/jobs/target/jobs]
[INFO] Processing war project
[INFO] Copying webapp resources [/opt/dev/workspace/jobs/src/main/webapp]
[INFO] Webapp assembled in [25 msecs]
[INFO] Building war: /opt/dev/workspace/city-jobs/target/jobs.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:46 min
[INFO] Finished at: 2015-02-26T17:53:49+05:30
[INFO] Final Memory: 12M/246M
[INFO] ------------------------------------------------------------------------

Now copy the jobs.war created in jobs > target > folder to your tomcat webserver webapp directory and restart the webserver.

Run the web application using URL : http://localhost:8080/jobs/index.jsp