Maven Web App
Create a Web application
To create a simple Java Web application, we'll use Maven's prototype-web App plugin. So let's open the command console, go to the C:MVN directory and execute the command mvn.
C:MVN>MVN archetype:generate-dgroupid=com.companyname.automobile-dartifactid=trucks-darchetypeartifactid= Maven-archetype-webapp-dinteractivemode=false
MAVEN will start processing and build a complete web-based Java application project structure.
[INFO] Scanning for projects ... [INFO] Searching repository for plugin with prefix: ' Archetype '. [INFO]-------------------------------------------------------------------[INFO] Building Maven Default project[ INFO] Task-segment: [Archetype:generate] (Aggregator-style) [INFO]------------------------------------------------ -------------------[INFO] Preparing Archetype:generate[info] No goals needed for Project-skipping[info] [archetype: Generate {Execution:default-cli}][info] generating project in Batch Mode[info]-------------------------------------- ------------------------------[INFO] Using following parameters for creating project from the old (1.x) Archetype:maven-arch Etype-webapp:1.0[info]--------------------------------------------------------------------[INFO] Parameter: GroupId, Value:com.companyname.automobile[info] parameter:packagename, Value:com.companyname.automobile[info] Parameter:package, Value:com.companyname.automobile[info] Parameter:artifactid, value:trucks[iNFO] Parameter:basedir, Value:c:mvn[info] parameter:version, Value:1.0-snapshot[info] project created from old (1.x) A Rchetype in Dir:c:mvnrucks[info]-------------------------------------------------------------------[INFO] BUILD Successful[info]-------------------------------------------------------------------[INFO] Total time:16 seconds[ INFO] finished at:tue Jul 11:00:00 IST 2012[info] Final Memory:20m/89m[info]---------------------------------------- ---------------------------
Now go to c:/. MVN directory. You will see the creation of a Java application project named trucks (as specified by Artifactid).
MAVEN uses a standard directory structure. In the example above, we can see the following key concepts
folder Structure |
Description |
Trucks |
Contains src folder and Pom.xml |
Src/main/webapp |
Contains index.jsp and Web-inf folder. |
Src/main/webapp/web-inf |
Contains Web |
Src/main/resources |
It contains images/properties files. |
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Com.companyname.automobile</groupId> <artifactId>Trucks</artifactId> <packaging>War</packaging> <version>1.0-snapshot</version> <name>Trucks Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupid> <artifactid >junit</artifactid> < Version>3.8.1</version> < Scope>test</scope> </ Dependency> </dependencies> <build> <finalname>trucks </finalname> </build></ Project>
MAVEN also created a sample JSP source file
Open C: > MVN > Trucks > src > Main > WebApp > folder, you will see index.jsp.
<body> Hello world! </body>
Building a Web Application
Let's open the command console, go to the C:MVN rucks directory and execute the following command MVN command.
C:MVNRUCKS>MVN Clean Package
MAVEN will start building the project.
[INFO] Scanning for projects ... [INFO]-------------------------------------------------------------------[INFO] Building trucks Maven webapp[info] Task-segment: [Clean, package][info]-------------------------------------------------------------------[INFO] [ Clean:clean {Execution:default-clean}][info] [resources:resources {execution:default-resources}][warning] Using Platform encoding (Cp1252 actually) to copy filtered resources,i.e. Build is Platform dependent! [INFO] Copying 0 Resource[info] [compiler:compile {execution:default-compile}][info] No sources to compile[info] [resources: testresources {execution:default-testresources}][warning] Using platform encoding (Cp1252 actually) to copy filtered Res ources,i.e. Build is Platform dependent! [INFO] Skip non existing resourcedirectory c:mvnruckssrcestesources[info] [compiler:testcompile {execution: Default-testcompile}][info] No sources to compile[info] [surefire:test {execution:default-test}][info] no tests to run. [INFO] [War:waR {Execution:default-war}][info] packaging Webapp[info] assembling webapp[trucks] in [C:mvnrucksargetrucks][info] Processing war Project[info] Copying WebApp Resources[c:mvnruckssrcmainwebapp][info] WebApp assembled in[77 Msecs][INFO ] Building War:c:mvnrucksargetrucks.war[info]------------------------------------------------------------------- [INFO] BUILD Successful[info]-------------------------------------------------------------------[INFO] Total Time:3 Seconds[info] finished at:tue Jul 11:22:45 IST 2012[info] Final Memory:11m/85m[info]-------------------------------- -----------------------------------
Deploying Web Applications
Now copy the created Trucks.war to C: > MVN > Trucks > Target > folder to the WebApp directory of the Web server, and then restart the Web server.
Testing the Web Application
To run a Web application by using a URL: http://<server-name>:<port-number>/trucks/index.jsp
Verify the output.
"Go" Building a MAVEN Web project