Learn how to create Java web project using Apache Maven command line. Example of how to create web project using Maven Java.
This is a simple web project, so going to add nothing apart from generated files.
1. The environment used in web project
I have tested and created Java Maven web project in the following environments:
– Eclipse IDE (Luna)
– JDK 7
– Maven 3
– Apache Tomcat 7
2. Create web Project Using Maven command line
I’m assuming that maven is in place in your system. Use maven-archetype-webapp
to start a simple webapp maven project. The command is as:
mvn archetype:generate -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
example to create web project using mavan java command line
$ mvn archetype:generate -DgroupId=com.mysoftkey -DartifactId=WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
here, $ sign represents command line.
Console Output:
E:\workspaces\ws_key>mvn archetype:generate -DgroupId=com.mysoftkey -DartifactId=WebApp -DarchetypeArtifactId=maven-arche type-webapp -DinteractiveMode=false [INFO] Scanning for projects... [WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5: Failed to parse plugi n descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5 (C:\Users\ranjeet\.m2\repository\org\apache\maven\plugin s\maven-clean-plugin\2.5\maven-clean-plugin-2.5.jar): invalid distance too far back [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5: Failed to parse plugi n descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5 (C:\Users\ranjeet\.m2\repository\org\apache\maven\plugin s\maven-clean-plugin\2.5\maven-clean-plugin-2.5.jar): invalid distance too far back [INFO] [INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Batch mode [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.mysoftkey [INFO] Parameter: packageName, Value: com.mysoftkey [INFO] Parameter: package, Value: com.mysoftkey [INFO] Parameter: artifactId, Value: WebApp [INFO] Parameter: basedir, Value: E:\workspaces\ws_key [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: E:\workspaces\ws_key\WebApp [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.801 s [INFO] Finished at: 2016-11-14T12:11:18+05:30 [INFO] Final Memory: 15M/154M [INFO] ------------------------------------------------------------------------ E:\workspaces\ws_key>
3. Directory Structure of Web Project
Following are the directory structure of Generated Web Project.
. |-- src | `-- main | `-- java | |-- resources | |-- webapp | | `-- WEB-INF | | `-- web.xml | `-- index.jsp `-- pom.xml
Here, web.xml
, index.jsp
and pom.xml
are project specific file generated by Maven , in which you may have to change as per your requirement.
4. Import Maven Project
In my case, workspace location as: E:\workspaces\ws_key
or ( switch workspace, if you are in another workspace).
follow below step to import the generated project in Eclipse
Select File -> Import -> Maven -> Existing Maven Project -> click next button -> choose root directory (i.e. WebApp) folder of project.
WebApp/pom.xml
4.0.0 com.mysoftkey WebApp war 1.0-SNAPSHOT WebApp Maven Webapp http://maven.apache.org junit junit 3.8.1 test WebApp
WebApp/index.jsp
Hello World!
WebApp/WEB-INF/web.xml
Archetype Created Web Application
5. Configuring of Tomcat and Web Project
follow following steps to create local Tomcat server and to add project in local Tomcat Server
Window -> Preference -> Select ‘Server’ from Left tab -> select Run-time Environment under ‘Server’ -> click on Add button -> Select ‘Apache’ -> Select Apache Tomcat 7 under ‘Apache’ -> select checkbox ‘Create a new local server’ -> click Next button -> click on Browse button and choose Tomcat Home location of tomcat server -> click Finish button.
Add Project in Local Tomcat configured option:
Left-click on Tomcat server under Server Tab and add Web project, if Server tab is not then (Click ‘Window’ menu of Eclipse -> click ‘Show View’ -> click on the Server tab).
6. Run Web Project
Open browser and type URL: http://localhost:8080/WebApp/
Output on the browser: Hello World!
Note:
For simplicity , I have not added anything extra in Maven generated project. Although , you can add maven dependency as per requirement, add plugin in pom.xml
, update serverlet 2.3 to 2.5
at least in web.xml
, add run time script in jsp etc.
visit Java tutorial for more details
Visit Apache Maven Official Site for more details.
I hope you enjoyed how to create web project using maven Java. Happy Learning! 🙂