Maven in 5 Minutes 快速上手

Prerequisites 先决条件

You must have an understanding of how to install software on your computer. If you do not know how to do this, please ask someone at your office, school, etc or pay someone to explain this to you. The Maven mailing lists are not the best place to ask for this advice.

你必须知道如何在你的电脑上安装软件。如果你还不知道,请向身边的人寻求帮助。不建议给我们发邮件问这种问题

Installation 安装

Maven is a Java tool, so you must have Java installed in order to proceed.
First, download Maven and follow the installation instructions. After that, type the following in a terminal or in a command prompt:

  1. mvn --version

It should print out your installed version of Maven, for example: :::info

  1. Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)
  2. Maven home: D:\apache-maven-3.6.0\bin..
  3. Java version: 1.8.0_161, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_161\jre
  4. Default locale: nl_NL, platform encoding: Cp1252
  5. OS name: “windows 7”, version: “6.1”, arch: “amd64”, family: “windows” :::

image.png

Depending upon your network setup, you may require extra configuration. Check out the Guide to Configuring Maven if necessary.
If you are using Windows, you should look at Windows Prerequisites to ensure that you are prepared to use Maven on Windows.
**

Creating a Project

You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:
您需要某个地方来存放项目,在某个地方创建一个目录,并在该目录中启动一个 shell。在命令行上,执行以下 Maven 目标:

  1. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app
  2. -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
  3. -DinteractiveMode=false
  1. mvn archetype:generate "-DgroupId=com.mycompany.app" "-DartifactId=my-app"
  2. "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4"
  3. "-DinteractiveMode=false"

If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don’t worry, there are ways to fix that.
如果你刚刚安装了 Maven,第一次运行可能需要一段时间。这是因为 Maven 正在将最新的工件 (插件罐和其他文件) 下载到您的本地存储库中。在命令成功之前,您可能还需要执行几次。这是因为远程服务器可能会在下载完成前超时。别担心,有办法解决这个问题。


You will notice that the _generate
goal created a directory with the same name given as the artifactId. Change into that directory.

  1. cd my-app

Under this directory you will notice the following standard project structure.

  1. my-app
  2. |-- pom.xml
  3. `-- src
  4. |-- main
  5. | `-- java
  6. | `-- com
  7. | `-- mycompany
  8. | `-- app
  9. | `-- App.java
  10. `-- test
  11. `-- java
  12. `-- com
  13. `-- mycompany
  14. `-- app
  15. `-- AppTest.java

The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml file is the project’s Project Object Model, or POM.

The POM

The pom.xml file is the core of a project’s configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
Pom.xml 文件是 Maven 中项目配置的核心。它是一个配置文件,包含以您想要的方式构建项目所需的大部分信息。POM 是巨大的,它的复杂性可能令人望而生畏,但是没有必要理解所有的复杂性来有效地使用它。

This project’s POM is:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.mycompany.app</groupId>
  5. <artifactId>my-app</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <properties>
  8. <maven.compiler.source>1.7</maven.compiler.source>
  9. <maven.compiler.target>1.7</maven.compiler.target>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.12</version>
  16. <scope>test</scope>
  17. </dependency>
  18. </dependencies>
  19. </project>

What did I just do?

You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin that provides the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This archetype:generate goal created a simple project based upon a maven-archetype-quickstart archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is “deal with various jboss items”.

您执行了 Maven 目标 archetype:generate,并将各种参数传递给该目标。前缀 archetype 是提供目标的插件。如果你熟悉 Ant,你可能会认为这类似于一项 task。这个 archetype:generate 目标基于maven-archetype-quickstart原型创建了一个简单的项目。现在就说插件是一个目标的集合,具有一般的共同目的就足够了。例如 jboss-maven-plugin,其目的是 “处理各种 jboss 项目”。

Build the Project

  1. mvn package

The command line will print out various actions, and end with the following:
image.png

Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile

You may test the newly compiled and packaged JAR with the following command:

  1. java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
  2. Which will print the quintessential:
  3. Hello World!

phase 是maven构建生命周期的一个步骤,而且是有序列的,当指定一个 phase执行时,Maven 会执行这个 phase 的所有前置步骤~

Running Maven Tools

Maven Phases

Although hardly a comprehensive list, these are the most common default lifecycle phases executed.

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

There are two other Maven lifecycles of note beyond the default list above. They are

  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project

Phases are actually mapped to underlying goals. The specific goals executed per phase is dependant upon the packaging type of the project. For example, package executes jar:jar if the project type is a JAR, and war:war if the project type is - you guessed it - a WAR.
An interesting thing to note is that phases and goals may be executed in sequence.

  1. mvn clean dependency:copy-dependencies package

This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).

Generating the Site

  1. mvn site

This phase generates a site based upon information on the project’s pom. You can look at the documentation generated under target/site.

Conclusion

We hope this quick overview has piqued your interest in the versatility of Maven. Note that this is a very truncated quick-start guide. Now you are ready for more comprehensive details concerning the actions you have just performed. Check out the Maven Getting Started Guide.