Maven Web Application
In this chapter, we will learn how to use the version control system Maven to manage a web-based project, and how to create, build, deploy, and run a web application.
### Creating a Web Application
We can use the `maven-archetype-webapp` plugin to create a simple Java web application.
Open the command console, navigate to the `C:MVN` folder, and execute the following `mvn` command:
C:MVN>mvn archetype:generate -DgroupId=com.companyname.automobile -DartifactId=trucks -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
After execution, Maven will start processing and create the complete directory structure for the Java Web project.
Scanning for projects... Searching repository for plugin with prefix: 'archetype'. ------------------------------------------------------------------- Building Maven Default Project task-segment: [archetype:generate] (aggregator-style) ------------------------------------------------------------------- Preparing archetype:generate No goals needed for project - skipping [archetype:generate {execution: default-cli}] Generating project in Batch mode -------------------------------------------------------------------- Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 -------------------------------------------------------------------- Parameter: groupId, Value: com.companyname.automobile Parameter: packageName, Value: com.companyname.automobile Parameter: package, Value: com.companyname.automobile Parameter: artifactId, Value: trucks Parameter: basedir, Value: C:MVN Parameter: version, Value: 1.0-SNAPSHOT project created from Old (1.x) Archetype in dir: C:MVNtrucks ------------------------------------------------------------------- BUILD SUCCESSFUL ------------------------------------------------------------------- Total time: 16 seconds Finished at: Tue Jul 17 11:00:00 IST 2012 Final Memory: 20M/89M -------------------------------------------------------------------
After execution, we can see the `trucks` project under the `C:/MVN` folder. Let's examine the project's directory structure:
!(#)
The Maven directory structure is standard. The purpose of each directory is as follows:
| Folder Structure | Description |
| --- | --- |
| trucks | Contains the `src` folder and the `pom.xml` file. |
| src/main/webapp | Contains the `index.jsp` file and the `WEB-INF` folder. |
| src/main/webapp/WEB-INF | Contains the `web.xml` file. |
| src/main/resources | Contains image and properties resource files. |
The code for the `pom.xml` file is as follows:
4.0.0com.companyname.automobiletruckswar1.0-SNAPSHOTtrucks Maven Webapphttp://maven.apache.orgjunitjunit3.8.1testtrucks
Next, we open the `C: > MVN > trucks > src > main > webapp >` folder. We can see a pre-created `index.jsp` file with the following code:
YouTip