To start with a Maven project, first thing is we need to create a
All the settings related to Maven project are specified under
- groupId uniquely identifies your project among all other projects.
- artifactId is the name name of jar file without version included in it
- version is the version number of the artifact, you can use versioning such as 1.0, 1.1.1 etc.
- packaging tells what is the final outcome of this POM going to be.
If you are working multiple Maven projects and/or modules where there are common dependencies across all projects, to avoid redundancies and duplicate configurations across these projects, you can create a parent project where common dependencies are defined and use that in child projects.
If there is any dependency or properties are configured in both parent POM as well as child POM, then child POM value takes the priority.
-
To create a parent project, all we have to do is in the
pom.xml , specifypackaging aspom . As you see in below pom.xml, we declared<packaging>pom</packaging> - Next, in your child
pom.xml , we can reference parent pom usingparent tag, it then automatically inherits dependencies, properties and other settings from parentpom.xml
Easiest way of creating Maven project in modern IDEs are to just create a
-
If you are using Intellij, right click on the
pom.xml and click on + Add as Maven Project. -
If you are using Eclipse, right click on the
project (not on pom.xml) and click on Configure -> Convert to Maven Project.
Intellij IDEA default installation comes up with an inbuilt Maven plugin. To create a Maven project, go to File New Project and choose Maven from Left side navigation menu and hit Next
Note: You can choose 'Create from archetype' if you wish to. (Archetype is a Maven project templating toolkit, meaning that template automatically adds necessary dependencies, plugins etc. in the pom.xml file.)
![Intellij IDEA create a new Maven project](/tech/SettingUpMavenProject/img/ij-create-maven-proj-1.png)
Enter a Name for the project and choose a directory Location for project files and hit Finish and your Maven project is ready.
![Intellij IDEA create a new Maven project, add project name and location](/tech/SettingUpMavenProject/img/ij-create-maven-proj-2.png)
If you want to change Maven related settings, such as Maven home (or) User settings file (or) Local repository location, go to File Settings, then on the left side menu options navigate through Build, Execution, Deployment Build Tools Maven
![Intellij IDEA Maven settings](/tech/SettingUpMavenProject/img/ij-create-maven-proj-3.png)
Eclipse default installation comes up with an inbuilt Maven plugin.
To create a Maven project, go to File New Other and select Maven Project option under Maven and hit Next
![Eclipse IDE create a new Maven project](/tech/SettingUpMavenProject/img/eclipse-create-maven-proj-1.png)
In the next screen check Create a simple project option and choose a Location directory for the project.
Note: If you can want to create your project from an archetype, you can uncheck 'Create a simple project' option. (Archetype is a Maven project templating toolkit, meaning that template automatically adds necessary dependencies, plugins etc. in the pom.xml file.)
![Eclipse IDE create a new Maven project, add location](/tech/SettingUpMavenProject/img/eclipse-create-maven-proj-2.png)
In the next screen, enter values for Group Id, Artifact Id and Version and hit Finish and your Maven project is ready.
![Eclipse IDE create a new Maven project, add group id, artifact id and version](/tech/SettingUpMavenProject/img/eclipse-create-maven-proj-3.png)
If you want to change Maven settings such Maven home (or) User settings file (or) Local repository, follow below steps
To change Maven home, go to Window Preferences, then on the left side navigation options, navigate through Maven Installations
![Eclipse IDE change Maven home](/tech/SettingUpMavenProject/img/eclipse-create-maven-proj-4.png)
Now, Click on Add to add new Maven home that you wish to change to, enter Installation home and Installation name (installation name could be any meaningful name, something like Maven3.1) and hit Finish
![Eclipse IDE change Maven home](/tech/SettingUpMavenProject/img/eclipse-create-maven-proj-5.png)
To change Maven settings file, navigate through Maven User Settings and select the settings file you wish to use.
![Eclipse IDE change Maven settings](/tech/SettingUpMavenProject/img/eclipse-create-maven-proj-6.png)