Maven 简明教程

Maven - NetBeans

NetBeans 6.7 及更高版本内置支持 Maven。在以前版本的案例中,Maven 插件可在插件管理器中找到。我们在本示例中使用的是 NetBeans 6.9。

NetBeans 6.7 and newer has in-built support for Maven. In case of previous version, Maven plugin is available in plugin Manager. We are using NetBeans 6.9 in this example.

以下是 NetBeans 的部分功能 −

Some of features of NetBeans are listed below −

  1. You can run Maven goals from NetBeans.

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

  3. You can update maven dependencies with IDE.

  4. You can Launch Maven builds from within NetBeans.

  5. NetBeans does the dependency management automatically based on Maven’s pom.xml.

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

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

  8. NetBeans provides wizards for creating new Maven projects, pom.xml.

  9. NetBeans provides a Maven Repository browser that enables you to view your local repository and registered external Maven repositories.

以下示例将帮助您利用集成 NetBeans 和 Maven 的优势。

Following example will help you to leverage benefits of integrating NetBeans and Maven.

Open a maven project in NetBeans

  1. Open NetBeans.

  2. Select File Menu > Open Project option.

  3. Select Project location, where a project was created using Maven. We’ve created a Java Project consumerBanking. Go to ‘Creating Java Project’ chapter, to see how to create a project using Maven.

nb open project

现在,您可以在 NetBeans 中看到以下 Maven 项目。查看 consumerBanking 项目库和测试库。您会看到 NetBeans 已将 Maven 依赖项添加到其构建路径中。

Now, you can see the maven project in NetBeans. Have a look at consumerBanking project Libraries and Test Libraries. You can see that NetBeans has added Maven dependencies to its build path.

nb project structure

Build a maven project in NetBeans

现在是使用 NetBeans 的 Maven 功能构建此项目的时候了。

Now, Its time to build this project using maven capability of NetBeans.

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

  2. Select Clean and Build as option.

nb build options

Maven 将开始构建项目。您可以在 NetBeans 控制台中看到输出,如下所示:

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

NetBeans: Executing 'mvn.bat -Dnetbeans.execution = true clean install'
NetBeans:      JAVA_HOME = C:\Program Files\Java\jdk1.6.0_21
Scanning for projects...
------------------------------------------------------------------------
Building consumerBanking
   task-segment: [clean, install]
------------------------------------------------------------------------
[clean:clean]
[resources:resources]
[WARNING] Using platform encoding (Cp1252 actually)
to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resources
[compiler:compile]
Compiling 2 source files to C:\MVN\consumerBanking\target\classes
[resources:testResources]
[WARNING] Using platform encoding (Cp1252 actually)
to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\MVN\consumerBanking\src\test\resources
[compiler:testCompile]
Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[surefire:test]
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.023 sec

Results :

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

[jar:jar]
Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.jar
[install:install]
Installing C:\MVN\consumerBanking\target\consumerBanking-1.0-SNAPSHOT.jar
to C:\Users\GB3824\.m2\repository\com\companyname\bank\consumerBanking\
1.0-SNAPSHOT\consumerBanking-1.0-SNAPSHOT.jar
------------------------------------------------------------------------
BUILD SUCCESSFUL
------------------------------------------------------------------------
Total time: 9 seconds
Finished at: Thu Jul 19 12:57:28 IST 2012
Final Memory: 16M/85M
------------------------------------------------------------------------

Run Application in NetBeans

现在,右键单击 App.java。选择 Run File 作为选项。您将在 NetBeans 控制台中看到结果。

Now, right click on App.java. Select Run File as option. You will see the result in the NetBeans Console.

NetBeans: Executing 'mvn.bat -Dexec.classpathScope = runtime
-Dexec.args = -classpath %classpath com.companyname.bank.App
-Dexec.executable = C:\Program Files\Java\jdk1.6.0_21\bin\java.exe
-Dnetbeans.execution = true process-classes
org.codehaus.mojo:exec-maven-plugin:1.1.1:exec'
NetBeans:      JAVA_HOME = C:\Program Files\Java\jdk1.6.0_21
Scanning for projects...
------------------------------------------------------------------------
Building consumerBanking
   task-segment: [process-classes,
   org.codehaus.mojo:exec-maven-plugin:1.1.1:exec]
------------------------------------------------------------------------
[resources:resources]
[WARNING] Using platform encoding (Cp1252 actually)
to copy filtered resources, i.e. build is platform dependent!
skip non existing resourceDirectory C:\MVN\consumerBanking\src\main\resources
[compiler:compile]
Nothing to compile - all classes are up to date
[exec:exec]
Hello World!
------------------------------------------------------------------------
BUILD SUCCESSFUL
------------------------------------------------------------------------
Total time: 1 second
Finished at: Thu Jul 19 14:18:13 IST 2012
Final Memory: 7M/64M
------------------------------------------------------------------------