Maven archetypes make it easy to create a base structure for some common Java applications. In this tutorial I am going to show you how to get a simple Java web application up and running in just a few minutes.
Tools I used:
Creating the project
In STS select File > New > Other… or just press Crtl + N. In the wizard search for maven and select Maven Project.
In the next window make sure you leave Create a simple project (skip archetype selection) unchecked as we want to create the project using an archetype. Below this option you can specify if you would like the project to be in the default workspace location or somewhere else. Both is fine, just remember which directory your project is in.
Clicking next again will take you to the archetype selection window. In the filter field type webapp and from the filtered list choose the one that has maven-achetype-webapp as it’s artifact id.
As the last step, fill in the desired parameters for the archetype. If want your app to be accessible from the same URL as in the example, please use the artifact id seen on the screenshot below.
Finally click Finish to create the project. A new Maven project is created for you with all the necessary files and folders for a basic web application.
Running the project
To open this web application in a browser you need an application server (e.g. Tomcat). Luckily with Maven’s Tomcat plugin you don’t need to install the server manually because Maven will automatically download an instance of it for you. You can set additional configuration options in the pom.xml for the server, but for this simple example this is not necessary.
To run this project navigate to the project’s directory in the command line and issue the following command:
mvn clean install tomcat:run
This will compile the project and install it in your local Maven repository and also deploy it to the Tomcat server. The server by default starts on the 8080 port. You can access the application by typing http://localhost:8080/simple-maven-webapp/ in your browser. The last part of this URL is in this case the same as the artifact id. This is because Maven automatically generated a pom.xml for you and set the <finalName>
tag to the value of the artifact id.
<build> <finalName>simple-maven-webapp</finalName> </build>
Note: There are other ways to change the url under which your application is available, but if you don’t specify anything else, the finalName will be used.