In this post, you will learn how to quickly create a Spring MVC project from a Maven’s archetype in command line prompt (on Windows) or terminal (Linux, Mac).

You know, one of tedious issues with Spring framework is that it requires a lot of boilerplate code for configuration - just to setup non-business stuffs. Though Spring Boot can speed up initial development time by implementing sensible defaults under the hood, sometimes we still need to create a Spring MVC project in the plain way (a standard Java web application that can be deployed on a servlet container like Tomcat).

First, make sure that you have Maven installed on your computer and setup the system PATH environment variable so you can run Maven’scommands from anywhere.

Change the current directory to the one where you want to put your Spring MVC project, for example:

cd g:\Java\Spring

And type the following command to create a Spring MVC project:

mvn archetype:generate ^
-DarchetypeGroupId=co.ntier ^
-DarchetypeArtifactId=spring-mvc-archetype ^
-DarchetypeVersion=1.0.2

Note that this command is quite long so I use the line-continuation character ^ (on Windows). On Linux, the line-continuation character is backslash (\):

mvn archetype:generate \
-DarchetypeGroupId=co.ntier \
-DarchetypeArtifactId=spring-mvc-archetype \
-DarchetypeVersion=1.0.2

Depend on your Maven, it might download necessary JAR files from Maven’s central repository. And then it will be generating project in interactive mode – asking for your project’s information, for example:

Define value for property 'groupId': net.codejava
Define value for property 'artifactId': MySpringMvc
Define value for property 'version' 1.0-SNAPSHOT: : 1.0
Define value for property 'package' net.codejava: : net.codejava

Then it asks you to confirm (press Y to confirm):

Confirm properties configuration:
groupId: net.codejava
artifactId: MySpringMvc
version: 1.0
package: net.codejava
 Y: : Y

Once confirmed, Maven will generate your Spring MVC project (BUILD SUCCESS):

create spring mvc project maven 

Note that this INFO line:

Project created from Archetype in dir: G:\Java\Spring\MySpringMvc

That means the Spring MVC project named MySpringMvc has been create successfully. Import this project in Eclipse, you will see the following project structure:

spring mvc project structure 

Explore the project and you can see it generated the following files:

  • pom.xml file that specifies dependencies for Spring MVC framework, Serlvet & JSP API, Spring test…
  • web.xml that configures Spring Dispatcher servlet.
  • MvcConfiguration class that configures Spring MVC’s view resolver and resources handler.
  • HomeController class is for Spring MVC controller with a handler method for the application’s context root.
  • home.jsp file is the homepage of the application.

Watch this video to see the steps visually:

However, since the spring-mvc-archetype was created long time ago, it uses old versions of Java, Spring, Servlet, JSP and some dependencies are no longer needed. So I recommend you to watch the following video to see how to update the Spring MVC project with new versions (Java 11, Spring framework 5, Servlet 4, etc):

BONUS: You can create your Spring MVC project faster by specifying all necessary information in one command. For example:

mvn archetype:generate ^
-DarchetypeGroupId=co.ntier ^
-DarchetypeArtifactId=spring-mvc-archetype ^
-DarchetypeVersion=1.0.2 ^
-DgroupId=net.codejava ^
-DartifactId=SpringMvcApp ^
-Dversion=1.0 ^
-Dpackage=net.codejava.spring ^
-DinteractiveMode=false

That’s how to use Maven’s command to quickly create a Spring MVC project. You can also use the project as a guide to your first, hello world Spring MVC application.

 

Related Spring Tutorials:

 

Other Spring Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment