Create Java Project using Maven

In this tutorial, You will see, How can you create your first Java Project using Maven.

Actually, Creating a Maven project means creating a POM file and the standard directory layout. To create the Java Project using maven.

 

Firstly maven Should be installed on your machine properly.(Install Maven on Windows Machine)

Go to command line and fire the command –

mvn -version

If maven is installed properly this command will return the installed maven version number as the result.

 

Once you have assured that Maven is installed. Now create the Project Directory, create a new directory on your machine. This directory will be the root directory for your Maven project.

 

For example – I have created the directory named “MavenProject” in my D:/ Drive.

  1. Now open the command prompt.

  2. Go to your Project Directory.

  3. Run the command – mvn archetype:generate

  4. Now wait for some time, Maven will check the repositories.

  5. Now press Enter key until you get the text – Define value for property ‘groupId’: :

  6. Now enter your groupId ( I entered com.selenium)

  7. Now enter the artifactId (I entered selenium_framework)

  8. Enter the version number. I entered 1.0

  9. Now press Enter key.

  10. Press “Y” once system will ask.

  11. Finally, You will get the BUILD SUCCESS message.

Create Java Project using Maven

 

To create pom.xml file. Copy the above XML content, paste it into a notepad file and save that notepad file with name pom.xml in your project directory.

It means you have created your First Java project successfully.

You can see you have Project structure like –

Create Java Project using Maven

 

Create Java Source File 

 Inside the Java root source directory (src/main/java/com/selenium) create a new directory Sample.

 

Inside the Sample directory inserts a file named HelloWorld.java. Inside the HelloWorld.java file you put the following Java code:

 

Now if you want to make your project executable then go to your project Root Directory and type the command –

mvn compile 

 

After running this command, You will notice, your project structure has been changed. Let’s see the structure of our project after compile command.

Create Java Project using Maven

 

Building the Java Project

To run the Build you need to execute the command –

mvn package

 

Go to your project root Directory and run the above command. After this command, Maven will compile the Project and create a JAR file containing the compiled Java class.

 

Maven creates a target subdirectory inside the project root directory. Inside the target directory, you will find the finished JAR file. For me, it will create the JAR with name selenium_framework-1.0

 

You have now built your first Maven project! Congratulations!

 

0 Comment

Leave a Reply