Create new Maven project - In 2 minutes

You are here : Home / Core Java Tutorials



STEP 1 >
Go to File, New, Other..


STEP 2 >
Type Maven project and select Next >


STEP 3 >
Tick Create a simple project (skip archetype selection)
Enter project Location = (I have used default workspace location)



STEP 4 >
Enter Group Id : com.ankit (this is package name)
Enter Artifact Id :  MyMavenProject (This is project name displayed in eclipse)
Enter Version : 0.0.1-SNAPSHOT
Enter Packaging : jar (available options are jar, war or pom)
Enter Name : MyFirstMavenProject (Actual Project name - This name is not displayed in eclipse)  (Optional)
Enter Description : This is my first Maven Project (Optional)

And click Finish


STEP 5 >
Project structure in Eclipse>
pom.xml is the deployment descriptor file.

pom.xml looks like this >
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.ankit</groupId>
   <artifactId>MyMavenProject</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>MyFirstMavenProject</name>
   <description>This is MyFirstMavenProject</description>
</project>



STEP 5.1 >
Currently it is not a java project (J icon is missing)
We will make it java project  (Observer carefully : You can see J icon - This j indicates that it's a java project)



STEP 5.2 >
Now, Project structure in Eclipse>
You can see that JRE System Library is added now.


If in case you face this error - Source folder is not a Java project - in eclipse. Click here to solve it.



STEP 6 >
Adding dependencies in pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.ankit</groupId>
   <artifactId>MyMavenProject</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>MyFirstMavenProject</name>
   <description>This is MyFirstMavenProject</description>
   <dependencies>
          <!-- Add dependency -->
          <dependency>
                 <groupId>commons-lang</groupId>
                 <artifactId>commons-lang</artifactId>
                 <version>2.2</version>
          </dependency>
          <!-- You can add more dependency -->
   </dependencies>
</project>


STEP 7 >
Now, Right click on pom.xml > select Run As > Then Maven clean  

Now, Right click on pom.xml > select Run As > Then Maven install

Refresh the project (F5 or by doing right click on project) - Required only if dependency are not updated.

Right click on project - Maven - Update Project..  (Alt + F5) - Required only if dependency are still not updated.



STEP 8 >
Now, add New Java Class - MyMainClass
And run it as Java Application

package main.java;
import org.apache.commons.lang.StringUtils;
public class MyMainClass {
   public static void main(String[] args) {
          String str = "abcd";
          System.out.println(StringUtils.isAlpha(str));
   }
}
/* Output
true
*/

Output is > true
You can see that jar (apache-commons) which contains org.apache.commons.lang.StringUtils was loaded just by defining its dependency in pom.xml and then following step 7.



STEP 9 >
See final directory structure



STEP 10 > Download
Now, it’s time to Download Project :) Click here.




Read : How to Import Maven project in eclipse - And Troubleshooting while importing





Related links >>

Maven error : No compiler is provided in this environment. You are running on a JRE rather than a JDK?


Difference between Maven Module and Maven Project


eEdit
Must read for you :