What is Maven, What is Archetype and Creating a Maven Project
Issues with legacy build frameworks(ANT)
Earlier with Ant, if we have to build a java project we need to put the logic for every task like compile, run in the build.xml. Though, these tasks are common across all the java projects. There is no way we can reuse. Below are some of the other pain points in Ant build framework.- Writing the logic for Project folder creation
- Download and copy all the dependent Jars into your project
- Writing the logic for reusable tasks like compile, test, run
What is Maven?
Maven is an alternative build framework which addresses all the pain points mentioned above. Though its usage is not limited to build, in this post we will limit the scope to build itself.
we will see how maven simplifies the above concerns.
What is Archetype?
Maven maintains a template for each project type and calls this template as Archetype. These archetypes exist for almost every project type in the maven repository. There are archetypes exists for Java Web app, Hibernate, Spring MVC etc.,
Each Archetype maintains what it takes to create a project type. It includes folder structure, libraries, sample java prototypes etc., So, we can use these archetypes for setting up the projects. This is a fast way of setting up the project. If you are not happy with this approach. You can add your project dependencies manually to your maven file.
Each Archetype maintains what it takes to create a project type. It includes folder structure, libraries, sample java prototypes etc., So, we can use these archetypes for setting up the projects. This is a fast way of setting up the project. If you are not happy with this approach. You can add your project dependencies manually to your maven file.
Creating a project from Archetype(aka template)
When we install maven on a windows machine, a folder .m2 will be created in C drive(in windows) where .m2 is your Maven local repository.
Your local repository is more or less a clean slate. Inside .m2 folder, you should see 'archetype-catalog.xml'(fig1) (catalog file of remote repository) which represents a list of archetypes from maven central repository. This file is required if we want to create a project using Archetype.
(If this file is file is not present you should download this file from the link: http://repo.maven.apache.org/maven2/archetype-catalog.xml)
Your local repository is more or less a clean slate. Inside .m2 folder, you should see 'archetype-catalog.xml'(fig1) (catalog file of remote repository) which represents a list of archetypes from maven central repository. This file is required if we want to create a project using Archetype.
(If this file is file is not present you should download this file from the link: http://repo.maven.apache.org/maven2/archetype-catalog.xml)
![]() |
| fig1:archetype-catalog.xml |
Once you copy this file to your local repositories you will be able to access all the archetypes in the Maven repositories. So, we have the setup ready. Let's start creating a simple Spring boot project using a Maven archetype.
Below are the steps for creating for creating a maven Spring boot project(for example) in eclipse IDE.
Step-1
Below screenshot shows the list of archetypes available with fresh installation of Maven. Eclipse parses the 'archetype-catalog.xml' file and shows the archetype list. Here, we see there is no Spring boot archetype for creating the Spring boot project. Because, default maven installation does not contain every artifact. We will have to download the additional artifacts by clicking on Configure.

Step-2
I have added the repository location of Spring by clicking on Configure-> Add Remote Catalog

Step-3
After adding the remote repository location of Spring, I could access the Spring-boot archetype. I selected the Archetype and proceeded to next screen
Step-4
For every archetype(Project template), there are some place holders left for the user to fill in.we will be filling in those details on the last screen.
Group Id: Package structure
Artifact Id: <Artifact-Name>.jar
Version: 0.0.1-SNAPSHOT (In Development Phase) or 0.0.1(After Released)
Package: Project Name

This completes the Maven project creation. And you can see the project gets created in eclipse.



Comments
Post a Comment