Manage Maven Project Using Eclipse

Maven java project can be created and maintained in command line. If you do not know how to achieve that, you can read  How To Create Java Project With Maven,  How To Use Maven To Build And Run Java Project.  But it is more effective to use Java IDE such as Eclipse to manage it. In this article you can learn below maven tasks processed within eclipse.

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

Eclipse is the most popular Java IDE that can be used to easily edit, debug, compile and package java project. But how can we use it with our maven java project then can make management to be more easily?

Create A 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 above command complete, you can see below folder “C:\WorkSpace\MvnExampleProject\Dev2qaMavenExample”.
    example maven java project folder structure

Import Existing Maven Java Project Into Eclipse

  1. Open dos window and run “cd C:/WorkSpace/MvnExampleProject/Dev2qaMavenExample”.
  2. Run “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.
    translate maven project into eclipse project
  3. Open eclipse, click “File —> Import” menu.
    click eclipse file import menu
  4. Choose “Existing Projects Into Workspace” in popup dialog.
    select existing project into workspace in popup dialog
  5. Click “Browse” button to select the below folder C:\WorkSpace\MvnExampleProject\Dev2qaMavenExample
    click browse button to select the project directory just translated
  6. Click “Finish” button. Now you can see your project in left panel list.
    maven java project in left list panel

Create A New Maven Project With Eclipse

When you go to Eclipse package list page, you will find that maven has been integrated into it after the Kepler package release.

READ :   Build And Run Java Project With Maven

has integrate maven plugin eclipse package

So you can directly use maven functions if you use above packages.

  1. Click “File —> New —> Maven Project”.
    create maven project in eclipse
  2. Click “Browse” button to choose workspace.
    choose mave project workspace
  3. Click Next, and Select “maven-archetype-quickstart” plugin.
    select maven-archetype-quickstart plugin
  4. Click Next, input group id, artifact id and package name in the next dialog. The artifact id will be used as the project name. And you can edit artifact id and package name separately.
    input group id, artifact id and package name
  5. Click “Finish” button, then you will see the new project is listed in left panel.
    newly created maven project in eclipse

Add Dependency In Eclipse

  1. Right click pom.xml, click “Maven —> Add Dependency” in popup menu. You can also do other actions such as add plugin there.
    add dependency in eclipse
  2. In the popup dialog you may see “Index downloads are disabled, search results may be incomplete.” warning message as below.
    index downloads are disable
  3. To resolve this problem, Click “Window —> Preferences”
    click window preferences menu
  4. Click Maven in left panel, and check below checkbox in right panel.
    Download repository index updates on startup,
    Download Artifact Sources, Download Artifact JavaDoc
    check Download repository index updates on startup checkbox
  5. Click OK, and restart eclipse. After restart, you may see a progress bar in bottom, that is just the process of repository index updates. This will take some minutes.
    update maven repository index when eclipse startup
  6. Now return to step2, input testng in search box, then select org.testng:testng dependency.
    select testng dependency
  7. Click OK and wait for some time, you can see the dependency updated in pom.xml
    testng dependency has been added in pom.xml

Run Command In Eclipse

  1. Right click pom.xml, click “Run As” menu. You can click to run almost all command in the popup menu.
    run maven command in eclipse
  2. You can see the output log in console when click to run one command.
    maven log output in eclipse console
READ :   TestNG Eclipse Plugin

Change Repository In Eclipse

  1. Click ” Window —> Preferences ” menu.
    click window preferences menu
  2. Click “Maven —> User Settings” in left panel. Click “Browse” button in right panel to select your local installed maven configure settings.xml file. Click “Reindex” button.
    select custom local maven repository
(Visited 189 times, 1 visits today)

Leave a Reply

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