Maven in Eclipse
Eclipse offers a great plugin called m2eclipse which integrates Maven with Eclipse.
In the latest Eclipse versions, Maven is included by default. To access it, open Windows -> Preferences, and you should see the following screen:
Here are some features of m2eclipse:
- Allows running Maven goals from within the Eclipse environment.
- Provides a console to view Maven command outputs directly in Eclipse.
- Enables updating Maven dependencies within the IDE.
- Supports building Maven projects using Eclipse.
- Automates dependency management based on Maven's pom.xml.
- Resolves dependencies between Maven and Eclipse workspaces without needing to install to the local Maven repository (requires dependent projects to be in the same workspace).
- Automatically downloads required dependencies and sources from remote Maven repositories.
- Offers wizards for creating new Maven projects, pom.xml files, and enabling Maven support in existing projects.
- Provides quick search for dependencies in remote Maven repositories.
Importing a Maven Project into Eclipse
- Open Eclipse
- Select File > Import
- Choose Maven Projects and click Next.
- Select the project path, which is the storage location when you created the project with Maven. For example, we created a project named consumerBanking. Refer to Creating a Java Project with Maven for how to create a project with Maven.
- Click Finish.
Now, you can see the Maven project in Eclipse.
Check the properties of the consumerBanking project, and you'll find that Eclipse has added all Maven dependencies to its build path.
Let's build the Maven project using Eclipse's build functionality.
- Right-click the consumerBanking project to open the context menu
- Select Run
- Then choose Maven package
Maven starts building the project, and you can see the output logs in Eclipse's console.
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------
[INFO] Building consumerBanking
[INFO]
[INFO] Id: com.companyname.bank:consumerBanking:jar:1.0-SNAPSHOT
[INFO] task-segment: [package]
[INFO] -------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test]
[INFO] Surefire report directory:
C:\MVN\consumerBanking\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar] [INFO] ------------------------------------------------------------------- [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------- [INFO] Total time: 1 second [INFO] Finished at: Thu Jul 12 18:18:24 IST 2012 [INFO] Final Memory: 2M/15M [INFO] -------------------------------------------------------------------
Now, right-click on App.java and select the Run As option. Choose As Java App.
You will see the following result:
Hello World!