Maven 简明教程

Maven - Eclipse IDE

Eclipse 提供了一个出色的插件 m2eclipse ,该插件将 Maven 和 Eclipse 无缝集成在一起。

Eclipse provides an excellent plugin m2eclipse which seamlessly integrates Maven and Eclipse together.

m2eclipse 的一些功能如下所示:

Some of features of m2eclipse are listed below −

  1. You can run Maven goals from Eclipse.

  2. You can view the output of Maven commands inside the Eclipse, using its own console.

  3. You can update maven dependencies with IDE.

  4. You can Launch Maven builds from within Eclipse.

  5. It does the dependency management for Eclipse build path based on Maven’s pom.xml.

  6. It resolves Maven dependencies from the Eclipse workspace without installing to local Maven repository (requires dependency project be in same workspace).

  7. It automatic downloads the required dependencies and sources from the remote Maven repositories.

  8. It provides wizards for creating new Maven projects, pom.xml and to enable Maven support on existing projects

  9. It provides quick search for dependencies in remote Maven repositories.

Installing m2eclipse plugin

使用以下链接之一安装 m2eclipse:

Use one of the following links to install m2eclipse −

Eclipse

URL

Eclipse 3.5 (Gallileo)

Installing m2eclipse in Eclipse 3.5 (Gallileo)

Eclipse 3.6 (Helios)

Installing m2eclipse in Eclipse 3.6 (Helios)

以下示例将帮助您利用集成 Eclipse 和 maven 的好处。

Following example will help you to leverage benefits of integrating Eclipse and maven.

Import a maven project in Eclipse

  1. Open Eclipse.

  2. Select File > Import > option.

  3. Select Maven Projects Option. Click on Next Button.

import project
  1. Select Project location, where a project was created using Maven. We’ve created a Java Project consumer Banking in the previous chapters. Go to ‘Creating Java Project’ chapter, to see how to create a project using Maven.

  2. Click Finish Button.

import project2

现在,您可以在 eclipse 中看到 Maven 项目。

Now, you can see the maven project in eclipse.

eclipse project structure

现在,查看一下 consumer Banking 项目属性。您可以看到 Eclipse 已将 Maven 依赖项添加到 java 构建路径。

Now, have a look at consumer Banking project properties. You can see that Eclipse has added Maven dependencies to java build path.

java build path2

现在,是使用 eclipse 的 maven 能力构建这个项目的时候了。

Now, it is time to build this project using maven capability of eclipse.

  1. Right Click on consumerBanking project to open context menu.

  2. Select Run as option.

  3. Then maven package option.

Maven 将开始构建项目。您可以在 Eclipse 控制台中看到以下输出 −

Maven will start building the project. You can see the output in Eclipse Console as follows −

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.bank:consumerBanking >----------------
[INFO] Building consumerBanking 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ consumerBanking ---
[INFO] Deleting C:\MVN\consumerBanking\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ consumerBanking ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ consumerBanking ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ consumerBanking ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ consumerBanking ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ consumerBanking ---
[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.028 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ consumerBanking ---
[INFO] Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.663 s
[INFO] Finished at: 2021-12-13T17:34:27+05:30
[INFO] ------------------------------------------------------------------------

现在,右键单击 App.java。选择 Run As 选项。然后选择 Java Application

Now, right click on App.java. Select Run As option. Then select Java Application.

run maven build

您将看到如下结果 −

You will see the result as follows −

Hello World!