Maven 简明教程

Maven - IntelliJ IDEA IDE Integration

IntelliJ IDEA 内置了对 Maven 的支持。在本示例中,我们使用 IntelliJ IDEA Community Edition 11.1。

IntelliJ IDEA has in-built support for Maven. We are using IntelliJ IDEA Community Edition 11.1 in this example.

IntelliJ IDEA 的一些功能列在下面:

Some of the features of IntelliJ IDEA are listed below −

  1. You can run Maven goals from IntelliJ IDEA.

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

  3. You can update maven dependencies within IDE.

  4. You can Launch Maven builds from within IntelliJ IDEA.

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

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

  7. IntelliJ IDEA automatically downloads the required dependencies and sources from the remote Maven repositories.

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

以下示例将帮助您充分利用将 IntelliJ IDEA 和 Maven 集成的优势。

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

Create a new project in IntelliJ IDEA

我们将使用 New Project Wizard 导入 Maven 项目。

We will import Maven project using New Project Wizard.

  1. Open IntelliJ IDEA.

  2. Select File Menu > New Project Option.

  3. Select import project from existing model.

ij new project step1
  1. Select Maven option

ij new project step2
  1. Select Project location, where a project was created using Maven. We have created a Java Project consumerBanking. Go to ‘Creating Java Project' chapter, to see how to create a project using Maven.

ij new project step3
  1. Select Maven project to import.

ij new project step4
  1. Enter name of the project and click finish.

ij new project step5
  1. Now, you can see the maven project in IntelliJ IDEA. Have a look at consumerBanking project external libraries. You can see that IntelliJ IDEA has added Maven dependencies to its build path under Maven section.

ij project structure

Build a maven project in IntelliJ IDEA

现在,是时候使用 IntelliJ IDEA 的功能来构建此项目了。

Now, it is time to build this project using capability of IntelliJ IDEA.

  1. Select consumerBanking project.

  2. Select Buid menu > Rebuild Project Option

你可以在 IntelliJ IDEA 控制台中看到输出

You can see the output in IntelliJ IDEA Console

4:01:56 PM Compilation completed successfully

Run Application in IntelliJ IDEA

  1. Select consumerBanking project.

  2. Right click on App.java to open context menu.

  3. select Run App.main()

ij run app

你将在 IntelliJ IDEA 控制台中看到结果。

You will see the result in IntelliJ IDEA Console.

"C:\Program Files\Java\jdk1.6.0_21\bin\java"
-Didea.launcher.port=7533
"-Didea.launcher.bin.path=
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 11.1.2\bin"
-Dfile.encoding=UTF-8
-classpath "C:\Program Files\Java\jdk1.6.0_21\jre\lib\charsets.jar;

C:\Program Files\Java\jdk1.6.0_21\jre\lib\deploy.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\javaws.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\jce.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\jsse.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\management-agent.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\plugin.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\resources.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\rt.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\dnsns.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\localedata.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\sunjce_provider.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\sunmscapi.jar;
C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\sunpkcs11.jar

C:\MVN\consumerBanking\target\classes;
C:\Program Files\JetBrains\
IntelliJ IDEA Community Edition 11.1.2\lib\idea_rt.jar"
com.intellij.rt.execution.application.AppMain com.companyname.bank.App
Hello World!

Process finished with exit code 0