YouTip LogoYouTip

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:

Hello World!

* * * ## Building the Web Application Open the command console, navigate to the `C:MVNtrucks` directory, and execute the following `mvn` command: C:MVNtrucks>mvn clean package Maven will start building the project: Scanning for projects... ------------------------------------------------------------------- Building trucks Maven Webapp task-segment: [clean, package] ------------------------------------------------------------------- [clean:clean {execution: default-clean}] [resources:resources {execution: default-resources}] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent! Copying 0 resource [compiler:compile {execution: default-compile}] No sources to compile [resources:testResources {execution: default-testResources}] Using platform encoding (Cp1252 actually) to copy filtered resources,i.e. build is platform dependent! skip non existing resourceDirectory C:MVNtruckssrctestresources [compiler:testCompile {execution: default-testCompile}] No sources to compile [surefire:test {execution: default-test}] No tests to run. [war:war {execution: default-war}] Packaging webapp Assembling webapp in [C:MVNtruckstargettrucks] Processing war project Copying webapp resources[C:MVNtruckssrcmainwebapp] Webapp assembled in Building war: C:MVNtruckstargettrucks.war ------------------------------------------------------------------- BUILD SUCCESSFUL ------------------------------------------------------------------- Total time: 3 seconds Finished at: Tue Jul 17 11:22:45 IST 2012 Final Memory: 11M/85M ------------------------------------------------------------------- ### Deploying the Web Application Open the `C: < MVN < trucks < target <` folder, locate the `trucks.war` file, and copy it to your web server's web application directory. Then, restart the web server. ### Testing the Web Application Access the following URL to run the web application: http://:/trucks/index.jsp Verify the result: !(#)
← Bootstrap4 BreadcrumbMaven Snapshots β†’