“Hello, World” Java Web Application using Java SE 6 + Tomcat 5.5 + Maven 2
See my previous posts to install Sun Java SE 6, Apache Tomcat 5.5/6, Apache Maven 2 on Windows and Ubuntu GNU/Linux operating systems:
- Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on Fedora GNU/Linux.
- Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on MS Windows.
Once all the required software components are installed, simply run the following command in the command prompt/shell to generate a basic Java web application project in the current working directory.
You can also run ‘mvn archetype:generate’ if you want to generate the project in interactive mode. The command will then prompt you for relevant information when creating the project.
[INFO] Scanning for projects…
[INFO] Searching repository for plugin with prefix: ‘archetype’.
[INFO] org.apache.maven.plugins: checking for updates from central
After all the necessary files are downloaded, you will be shown a list of supported archetypes and will be prompted to select the one that you want to generate.
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)
42: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/])
43: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/])
44: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/])
Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44) 15: :
Enter the appropriate number to select the archetype that you want to generate; for example, enter 18 to create a basic Java Web Application project. Next you will be prompted to enter values for groupId(say ‘hello’), artifactId(say ‘HelloWorld’), version(you can accept the default and just press the ENTER key) and package(say ‘war’). You will then be asked for confirmation; type Y and press the ENTER key. For more information on what the above fields mean, read about Maven Co-ordinates.
To learn more about Maven’s archetype plugin, read Maven 2 Archetype plugin usage page.
You can run the basic Java web application project created above in Tomcat web server by running the following command.
[lot of output]
[INFO] Starting tomcat server
[INFO] Starting Servlet Engine: Apache Tomcat/5.5.15
[INFO] XML validation disabled
[INFO] Initializing Coyote HTTP/1.1 on http-8080
[INFO] Starting Coyote HTTP/1.1 on http-8080
(If this is the first time you are running the above command, it downloads a lot of necessary files and stores them in the local Maven repository. This is a one-time operation.) You can now access the web application from a location like “http://localhost:8080/HelloWorld/”. ‘HelloWorld’ is the artifactId that we used when creating the sample application. You can read more about running and deploying applications from Maven to Tomcat web server on Maven Tomcat plugin page.
If Jetty web server is installed on your computer, you can also run your web application in Jetty by running the following command:
How easy it is to switch the container when you are developing Java web applications with the Maven build tool! You can read more about running and deploying applications from Maven to Jetty web server on Maven 2 Jetty Plugin page.