Creating Project Structure with Maven

As mentioned earlier MAVEN helps us in creating the project structure, managing and downloading the dependencies(MAVEN adds few default dependencies specific to project structures).

How does MAVEN know all this – meaning how is it able to differentiate between project structures, how does it know which project structure needs which dependency?

All this intelligence that MAVEN has comes from repositories, there are repositories that are available online, MAVEN talks to those repositories and gets the information and that’s how MAVEN helps us performing all these activities, in a way we need to make sure that our machine is connected to internet because MAVEN needs to go online and get all this information.

Lets start using MAVEN…for the start lets take a look as how MAVEN helps us in managing project directories/structures.

Step #1: Create a directory and go in to that directory.

Maven directory

Step #2: In order to create a project structure or project prototype which we call as an artifact in MAVEN’s perspective, we have a command known as mvn archetype:generate

When you run this command for the first time, MAVEN will download all the required plugins and it takes a while until it prompts us to choose a number.

Maven archetype

Maven downloads

After a whole lot of downloading, MAVEN has a prompt displayed for me and says “Choose a number or apply filter” and 493 is the default. Now what is this number?

If you see on the command window ahead of the prompt, there is a list of different archetypes. What an archetype means is…it’s a model, it’s a prototype as how we want our project to be structured – meaning project structure for a Web Application varies from a non-web application. MAVEN provides myriad predefined archetypes.

To know the list of all this archetypes, we can write the output to a notepad using below command.

Maven archtypes text

When we executed this command all the output is written to a .txt file

Maven demo

When we open the file and try to locate what does archetype# 493 implies, we will see that it is a “Sample MAVEN project”

Maven text archetype

If we would like to develop a Web Application project, then we might end up entering either of the below mentioned numbers.

Maven app folder

Note: This number may vary from version to version, its not necessary for this to be same on all versions.

SO if we choose one number MAVEN will create one blank application with all the known and required dependencies and jars and have it ready for us.

So let me go with default as of now. By clicking enter in the above screen you agree that the archetype to be created belongs to 493 and if we don’t choose to opt 493, users can also give their preferred prototype that maven should create.

Now it prompts to choose the versions…

Maven arch versions

This versions are the versions of the archetypes itself. As we can see there are various version #’s that it has prompted us with, but lets go with default for now and it will download more plugins related to this archetype.

And after that, it should be prompting us for few properties like groupId, artifcatID, version.

groupId is usually analogous to a package name
artifactId is usually analogous to class name and this will be used while packing it as a jar or war.
version is just a number to differentiate the history.

We will learn more on what this all really means once we start exploring pom.xml, but for now here is what we need to identify them as…

groupId – com.apk.mavendemo
artifactId - MavenDemoApp
version -

Maven properties

It would then prompt for a confirmation for the inputs that we have entered, say Y and its finished.

Maven build success

Now lets see what MAVEN has really done.

Maven folder

It has created a directory called MavenDemoApp. Lets see what exactly is inside this directory.

Maven pom

There is a folder call src and and pom.xml. If we open the pom.xml we should see three nodes – groupId, artifactId and version. And these were the inputs that we had provided during the archetype generation. And packaging is mentioned as jar and this happens to be default.

Maven pom directory

There is a name node which is going to be the name of the application. This can be different from the artifactId and what happens if it is different is our application will be called by this name node but when we are actually saving this application in the repository the artifactId will be the artifactId provided.

Maven project name

We’ll get back to the dependencies node a bit later. Now lets get back to the folder structure…It has created two folders here, one as main and the other as test.

Maven folder structure

We can keep all our java classes inside main. It has also organised the package for us and created a sample .java file. And we can place all our Junit test cases inside test folder.

Maven test folder

Maven has done all this for us, it has created all the folder structure, package directories and a pom.xml which has information about the application itself and what the dependencies are.

Maven final structure

Build Tools: 

Comments

excellent tutorial !!!!

Add new comment

Image CAPTCHA