How To Create Maven Project In Eclipse

This article will tell you how to create maven project in eclipse. It will implement the below tasks within the eclipse maven project.

  1. Create a java project.
  2. Import the existing project.
  3. Create a new project.
  4. Add dependency.
  5. Run command.
  6. Change repository.

1. Create Eclipse Maven Project.

  1. Create a folder C:/WorkSpace/MvnExampleProject.
  2. Open dos window and run “cd C:/WorkSpace/MvnExampleProject”.
  3. Run “mvn archetype:generate -DgroupId=com.dev2qa.example.mvn -DartifactId=Dev2qaMavenExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false”
  4. When the above command complete, you can see a folder “C:\WorkSpace\MvnExampleProject\Dev2qaMavenExample” has been created. In this folder, there is an src subfolder and a pom.xml file.

2. Import Existing Eclipse Maven Java Project.

  1. Open dos window and run “cd C:/WorkSpace/MvnExampleProject/Dev2qaMavenExample”.
  2. Run the mvn eclipse:eclipse command to add needed project files into it. This command will download a lot of plugin jar files and save them into your local repository. It will also generate all the project files that needed when import into eclipse.
  3. Open eclipse, click File —> Import menu.
  4. Choose General —> Existing Projects Into Workspace in the popup dialog.
  5. Click Browse button to select the folder C:\WorkSpace\MvnExampleProject\Dev2qaMavenExample.
  6. Click the Finish button. Now you can see your eclipse maven project in the eclipse IDE left panel.

3. Create A New Maven Project With Eclipse.

When you go to the eclipse package list page, you will find that maven has been integrated into it after the Kepler package release. So you can directly create a new eclipse maven project in eclipse IDE.

  1. Open eclipse, click File —> New —> Maven Project menu item.
  2. Click the Browse button to select your eclipse maven project saved folder in the eclipse New Maven project wizard dialog. After that click the Next button.
  3. In the Select an Archetype dialog, select the archetype that Group Id is org.apache.maven.archetypes and Artifact Id is maven-archetype-quickstart. This archetype contains an eclipse maven project template, it can create an empty eclipse maven project quickly.
  4. Click the Next button, input Group Id ( com.dev2qa ), Artifact Id ( Dev2qaUtil ), Package name ( com.dev2qa.util ) in the next dialog. The artifact id will be used as the project name. And you can edit artifact id and package name separately.
  5. Click the Finish button, then you will see the newly created eclipse maven project in the eclipse left panel. When you expand the eclipse maven project, you will see a Maven Dependencies directory under it, this folder contains all the maven libraries needed in this eclipse maven project.

4. Add Eclipse Maven Dependencies In Eclipse Maven Project.

  1. Right-click pom.xml, click Maven —> Add Dependency in the popup menu list.
  2. In the pop-up dialog, you may see the “Index downloads are disabled, search results may be incomplete.” warning message.
  3. To resolve this problem, Click Window —> Preferences menu item to open the eclipse Preferences dialog window.
  4. Click Maven in the left panel, and check the checkbox Download repository index updates on startup, Download Artifact Sources, Download Artifact JavaDoc in the right panel.
  5. Click OK, and restart eclipse. After restart, you may see a progress bar at the bottom of the Eclipse IDE. This is just the process of eclipse maven repository index updates. This will take some minutes.
  6. Now return to step2: Add Dependency dialog window, input the keyword testng in the Enter groupId, artifactId or sha1 prefix or pattern(*) search box, then select the org.testng:testng dependency. Then it will display the text org.testng in the top Group Id text box and text testng in the top Artifact Id text box.
  7. Click OK and wait for some time, you can see the dependency updated in the project pom.xml, it has added the below testng maven dependency XML element in the pom.xml file.

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.11</version>
    </dependency>
    

5. Run Maven Command In Eclipse.

  1. Right-click pom.xml, click the Run As menu item. It will popup a sub-menu list contains the maven command Maven build, Maven clean, Maven generate-sources, Maven install, Maven test. You can click either of the command menu items to run the command.
  2. You can see the output log in the eclipse Console when clicking one of the above commands to run.

6. Change Repository In Eclipse.

  1. Open eclipse and click Window —> Preferences menu item to open the eclipse Preferences window.
  2. Click Maven —> User Settings in the left panel.
  3. Click the Browse button in the right panel to select your local installed maven repository configuration settings.xml file. Then it will display the Local Repository folder in the Local Repository text box at the bottom of the right panel.
  4. Click the Reindex button at the end of the Local Repository text box to reindex the local repository.

References

  1. How To Use Maven Command To Create Java Project
  2. How To Use Maven To Build And Run Java Project

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Index