Maven Tutorial
Maven Version
Maven Website
What is a Build Tool?
Installing Maven
Maven Overview - Core Concepts
Maven vs. Ant
Maven POM Files
Super POM
Effective POM
Maven Settings File
Running Maven
Maven Directory Structure
Project Dependencies
External Dependencies
Snapshot Dependencies
Maven Repositories
Maven Build Life Cycles, Phases and Goals
Maven Build Profiles
Maven Plugins
Maven教程
Maven版本
Maven的网站
构建工具是什么?
安装Maven
Maven概述——核心概念
Maven和蚂蚁
Maven POM文件
超级POM
有效的POM
Maven设置文件
运行Maven
Maven的目录结构
项目依赖项
外部依赖关系
快照的依赖性
Maven存储库
Maven构建生命周期、阶段和目标
Maven构建配置文件
Maven插件
Maven is a powerful build
tool for Java software projects. Actually, you can build software projects
using other languages too, but Maven is developed in Java, and is thus
historically used more for Java projects.
Maven是一个强大的Java软件项目构建工具。事实上,你也可以使用其他语言构建的软件项目,但Maven是Java开发的,因此历史上用于Java项目。
The purpose[A1] of this Maven tutorial[A2] is to make you
understand how Maven works. Therefore[A3] this
tutorial focuses[A4] on the core concepts[A5] of Maven. Once you
understand the core concepts, it is much easier to lookup[A6] the
fine detail in the Maven documentation, or search for it on the internet.
这个Maven教程的目的是让你理解Maven是如何工作的。因此本教程的重点是Maven的核心概念。一旦你理解核心概念,它更容易查找Maven的细节的文档,或在互联网上搜索它。
Actually,
the Maven developers claim[A7] that Maven is more than
just a build tool. You can read what they believe it is, in their
document Philosophy[A8] of Maven. But for
now, just think of it as a build tool. You will find out what Maven really
is, once you understand it and start using it.
实际上,Maven开发者声称Maven不仅仅是一个构建工具。你可以阅读他们相信这是什么,在他们的文档的Maven哲学。但是现在,只是把它看作一个构建工具。你会发现Maven是什么,一旦你理解它并开始使用它。
The
first version of this Maven tutorial is based on Maven 3.0.5. However,
this tutorial has been updated in several places since the first version of
this tutorial. The updates were tested with Maven 3.3.3.
这个Maven教程的第一个版本是基于Maven
3.0.5。不过,本教程已经更新在本教程的第一个版本以来的几个地方。的更新与Maven 3.3.3测试。
A build
tool is a tool that automates[A9] everything related to
building the software project. Building a software project typically[A10] includes one or more of
these activities:
Generating source code (if auto-generated code is used in the project).
Generating documentation from the source code.
Compiling source code.
Packaging compiled code into JAR files or ZIP files.
Installing the packaged code on a server, in a repository or somewhere else.
一个构建工具是一个工具,自动构建软件项目相关的一切。建立一个软件项目通常包括一个或多个活动:
生成的源代码(如果使用自动生成的代码的项目)。
生成文档的源代码。
编译源代码。
编译后的代码打包成JAR文件或ZIP文件。
安装打包代码在一个服务器上,在一个存储库或其他地方。
Any
given software project may have more activities than these needed to build the
finished software. Such activities can normally be plugged into a build
tool, so these activities can be automated too.
任何软件项目可能比这些活动需要构建完成的软件。这些活动通常可以插入一个构建工具,这些活动也可以是自动进行的。
The advantage[A11] of automating
the build process[A12] is
that you minimize[A13] the risk of humans
making errors while building the software manually. Additionally, an
automated build tool is typically faster than a human performing the same steps
manually.
自动化构建过程的优点是,你减少人类犯错误的风险,而手动构建的软件。此外,一个自动构建工具通常是速度比人工手动执行相同的步骤。
To
install Maven on your own system (computer), go to the Maven download
page and follow the instructions there. In summary, what you need to
do is:
Set the JAVA_HOME environment variable to point to a valid Java SDK
(e.g. Java 8).
Download and unzip Maven.
Set the M2_HOME environment variable to point to the directory you
unzipped Maven to.
Set the M2 environment variable to point
to M2_HOME/bin (%M2_HOME%\bin on Windows, $M2_HOME/bin on
unix).
Add M2 to the PATH environment variable (%M2% on
Windows, $M2 on unix).
Open a command prompt and type 'mvn -version' (without quotes) and press enter.
安装Maven在您自己的系统(计算机),去Maven下载页面,按提示操作。总之,你需要做的是:
设置JAVA_HOME环境变量指向一个有效的Java
SDK(例如Java 8)。
下载并解压缩Maven。
设置M2_HOME环境变量,使之指向您解压的目录Maven。
M2设置环境变量,使之指向M2_HOME /
bin(% M2_HOME % \ bin在Windows上,美元M2_HOME unix / bin)。
M2添加到PATH环境变量(% M2%在Windows上,美元M2在unix)。
打开一个命令提示符并输入mvn - version(没有引号)和按回车。
After
typing in the mvn -version command you should be able to see Maven
execute, and the version number of Maven written out to the command prompt.
Note: Maven uses Java when executing, so you need Java installed too (and
the JAVA_HOME environment variable set as explained
above). Maven 3.0.5 needs a Java version 1.5 or later. I use Maven
3.3.3 with Java 8 (u45).
I have a tutorial about installing the Java SDK in case you are not
familiar with that. Remember, it has to be an SDK (Software Developer
Kit), not just a JRE (Java Runtime Environment). The JRE does not contain
a Java compiler. Only the SDK does.
后输入mvn您应该能够看到Maven - version命令执行,和Maven版本号写入命令提示符。
注意:Maven使用Java在执行,所以你也需要安装Java(JAVA_HOME环境变量设置为上面的解释)。Maven 3.0.5需要一个Java版本1.5或更高版本。我使用Maven 3.3.3与Java 8(u45)。
我有一个关于安装Java SDK的教程,以防你不熟悉。记住,它必须是一个SDK(软件开发工具包),而不只是一个JRE(Java运行时环境)。JRE不包含一个Java编译器。只有SDK。
Maven
is centered around the concept of POM files (Project Object Model). A POM
file is an XML representation of project resources like source code, test code,
dependencies (external JARs used) etc. The POM contains references to all of
these resources. The POM file should be located in the root directory of
the project it belongs to.
Maven是围绕POM文件(项目对象模型)的概念。POM文件是一个XML表示的项目资源(如源代码、测试代码、依赖外部jar(使用)等。POM包含对所有这些资源的引用。POM文件应该位于所属项目的根目录。
Here is
a diagram illustrating how Maven uses the POM file, and what the POM file
primarily contains:
这是一个图说明如何Maven POM文件使用,和POM文件主要包含:

Overview
of Maven core concepts.Maven核心概念的概述。
These concepts are explained
briefly below to give you an overview, and then in more detail in their own
sections later in this tutorial.
这些概念简要解释下面给你一个概述,然后详细的部分在本教程。
POM Files
When you execute a Maven
command you give Maven a POM file to execute the commands on. Maven will
then execute the command on the resources described in the POM.
Build Life Cycles, Phases and Goals
The build process in
Maven is split up into build life cycles, phases and goals. A build life
cycle consists of a sequence of build phases, and each build phase consists of
a sequence of goals. When you run Maven you pass a command to
Maven. This command is the name of a build life cycle, phase or
goal. If a life cycle is requested executed, all build phases in that life
cycle are executed. If a build phase is requested executed, all build phases
before it in the pre-defined sequence of build phases are executed too.
POM文件
当你执行一个Maven命令你给Maven POM文件上执行命令。Maven将执行命令在POM中描述的资源。
建立生命周期、阶段和目标
在Maven构建过程分成构建生命周期、阶段和目标。构建生命周期由一序列构建阶段,并且每个构建阶段由一系列的目标。当您运行Maven你通过Maven命令。这个命令的名字是构建生命周期、阶段或目标。如果请求生命周期执行,所有构建阶段生命周期执行。如果一个构建阶段要求执行,所有构建阶段之前,在构建阶段的预定义的序列执行。
Dependencies and Repositories
One of the first goals
Maven executes is to check the dependencies needed by your
project. Dependencies are external JAR files (Java libraries) that your
project uses. If the dependencies are not found in the local Maven
repository, Maven downloads them from a central Maven repository and puts them
in your local repository. The local repository is just a directory on your
computer's hard disk. You can specify where the local repository should be
located if you want to (I do). You can also specify which remote
repository to use for downloading dependencies. All this will be explained
in more detail later in this tutorial.
依赖关系和存储库
Maven执行的第一个目标是检查您的项目所需的依赖项。依赖关系是外部JAR文件(Java库),您的项目使用。如果依赖不是本地Maven资源库中,Maven下载从中央Maven存储库,并将它们在你的本地存储库。本地存储库是一个目录在您的计算机上的硬盘。您可以指定本地存储库应该处的位置如果你想(我)。您还可以指定使用哪一个远程存储库下载依赖关系。这一切将会更详细地解释在本教程。
Build Plugins
Build plugins are used
to insert extra goals into a build phase. If you need to perform a set of
actions for your project which are not covered by the standard Maven build
phases and goals, you can add a plugin to the POM file. Maven has some
standard plugins you can use, and you can also implement your own in Java if
you need to.
构建插件
构建插件用于插入额外的目标变成一个构建阶段。如果您需要为您的项目执行的一组操作,不受标准Maven构建阶段和目标,您可以添加一个插件POM文件。Maven有一些标准的插件可以使用,还可以在Java中实现自己的如果你需要。
Build Profiles
Build profiles are used if you
need to build your project in different ways. For instance, you may need
to build your project for your local computer, for development and
test. And you may need to build it for deployment on your production
environment. These two builds may be different. To enable different
builds you can add different build profiles to your POM files. When
executing Maven you can tell which build profile to use.
构建配置文件
构建配置文件使用如果你需要以不同的方式来构建您的项目。例如,您可能需要为您的本地计算机构建您的项目,开发和测试。你可能需要构建它部署在您的生产环境。这两个版本可能会有所不同。使不同的构建可以添加不同的构建配置文件到你的POM文件。当执行Maven可以告诉构建配置文件来使用。
Maven vs. Ant
Ant is another popular
build tool by Apache. If you are used to Ant and you are trying to learn
Maven, you will notice a difference in the approach of the two projects.
Ant uses an imperative
approach, meaning you specify in the Ant build file what actions Ant should
take. You can specify low level actions like copying files, compiling code
etc. You specify the actions, and you also specify the sequence in which they
are carried out. Ant has no default directory layout.
Maven uses a more declarative
approach, meaning that you specify in the Maven POM file what to
build, but now how to build it. The POM file describes your
project resources - not how to build it. Contrarily, an Ant file describes
how to build your project. In Maven, how to build your project is
predefined in the Maven Build Life Cycles, Phases and Goals.
Maven和蚂蚁
Apache
Ant是另一个流行的构建工具。如果你习惯于Ant,你想学习Maven,您将注意到两个项目的不同方法。
Ant使用命令式的方法,这意味着您指定的Ant构建文件蚂蚁应该采取什么行动。您可以指定低水平复制文件等操作,编译代码等您指定的动作,你也指定的顺序进行。蚂蚁没有默认的目录布局。
Maven使用更多的声明性方法,这意味着您指定的Maven POM文件构建什么,但现在如何构建它。POM文件描述您的项目资源,而不是如何构建它。相反,一个Ant文件描述如何构建您的项目。在Maven,如何构建您的项目是预定义的Maven构建生命周期的阶段和目标。
Maven POM Files
A Maven POM file (Project
Object Model) is an XML file that describe the resources of the
project. This includes the directories where the source code, test source
etc. is located in, what external dependencies (JAR files) your projects has
etc.
The POM file describes what to build, but most often
not how to build it. How to build it is up to the Maven build
phases and goals. You can insert custom actions (goals) into the Maven
build phase if you need to, though.
Each project has a POM
file. The POM file is named pom.xml and should be located in the
root directory of your project. A project divided into subprojects will
typically have one POM file for the parent project, and one POM file for each
subproject. This structure allows both the total project to be built in
one step, or any of the subprojects to be built separately.
Maven POM文件
一个Maven POM文件(项目对象模型)是一个XML文件,描述项目的资源。这包括源代码的目录、测试等位于来源,外部依赖(JAR文件)项目等。
POM文件描述了如何构建,但通常不是如何构建它。如何构建由Maven构建阶段和目标。你可以插入自定义动作(目标)到Maven构建阶段,如果你需要。
每个项目的POM文件。名为POM的POM文件。xml和应位于您的项目的根目录。一个项目分成子项目通常会有一个父项目的POM文件,每个子项目和一个POM文件。这个结构允许的项目总建在一个步骤中,或任何的子项目分别建成。
Throughout
the rest of this section I will describe the most important parts of the POM
file. For a full reference of the POM file, see the Maven POM
Reference.
在本节的其余部分,我将描述POM文件的最重要的部分。完整的POM文件的参考,请参阅Maven POM参考。
Here is a minimal POM file:这是一个最小的POM文件:
<project xmlns="http://maven./POM/4.0.0"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./POM/4.0.0
http://maven./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jenkov</groupId>
<artifactId>java-web-crawler</artifactId>
<version>1.0.0</version>
</project>
The modelVersion element
sets what version of the POM model you are using. Use the one matching the
Maven version you are using. Version 4.0.0 matches Maven version 2 and 3.
modelVersion元素集您使用的是什么版本的POM模型。使用一个匹配您正在使用Maven版本。版本4.0.0匹配Maven版本2和3。
The groupId element
is a unique ID for an organization, or a project (an open source project, for
instance). Most often you will use a group ID which is similar to the root
Java package name of the project. For instance, for my Java Web Crawler
project I may choose the group ID com.jenkov. If the project was an
open source project with many independent contributors, perhaps it would make
more sense to use a group ID related to the project than an a group ID related
to my company. Thus, com.javawebcrawler could be used.
The group ID does not
have to be a Java package name, and does not need to use the . notation
(dot notation) for separating words in the ID. But, if you do, the project will
be located in the Maven repository under a directory structure matching the
group ID. Each . is replaced with a directory separator, and each word
thus represents a directory. The group ID com.jenkov would then
be located in a directory calledMAVEN_REPO/com/jenkov. The MAVEN_REPO part
of the directory name will be replaced with the directory path of the Maven
repository.
组织的groupId元素是一个惟一的ID,或者一个项目(一个开源项目,例如)。通常您将使用一组ID,类似于根项目的Java包名称。例如,我可以选择我的Java Web爬虫项目com.jenkov的组ID。如果项目是一个开放源码项目,许多独立的贡献者,也许更有意义使用组ID相关的项目与我公司相关的组ID。因此,com。可以使用javawebcrawler。
组ID并不需要一个Java包名称,并且不需要使用。符号(点符号)分隔单词的ID。但是,如果你这样做,这个项目将位于Maven存储库组ID相匹配的目录结构中。。被替换为一个目录分隔符,因此每个单词代表一个目录。com的组ID。jenkov将位于一个目录calledMAVEN_REPO /com/jenkov。MAVEN_REPO目录名称的一部分将会取代Maven存储库的目录路径。
The artifactId element
contains the name of the project you are building. In the case of my Java
Web Crawler project, the artifact ID would be java-web-crawler. The
artifact ID is used as name for a subdirectory under the group ID directory in
the Maven repository. The artifact ID is also used as part of the name of
the JAR file produced when building the project. The output of the build
process, the build result that is, is called an artifact in Maven. Most
often it is a JAR, WAR or EAR file, but it could also be something else.
The versionId element
contains the version number of the project. If your project has been
released in different versions, for instance an open source API, then it is
useful to version the builds. That way users of your project can refer to
a specific version of your project. The version number is used as a name
for a subdirectory under the artifact ID directory. The version number is
also used as part of the name of the artifact built.
artifactId元素包含您正在构建的项目的名称。在我的Java Web爬虫项目的情况下,将java-web-crawler工件ID。工件ID作为名称的组ID目录下的子目录Maven存储库。工件ID也作为名字的一部分构建项目时产生的JAR文件。构建过程的输出,构建结果,被称为一个工件在Maven。通常是一个JAR、WAR或EAR文件,但它也可以是其他的东西。
versionId元素包含项目的版本号。如果你的项目已经发布在不同的版本中,例如一个开源API,然后是有用的构建版本。这样你的项目的用户可以参考您的项目的一个特定的版本。版本号是用作工件ID目录下的子目录的名称。版本号也被用作工件的名称的一部分。
The
above groupId, artifactId and version elements would
result in a JAR file being built and put into the local Maven repository at the
following path (directory and file name):
MAVEN_REPO/com/jenkov/java-web-crawler/1.0.0/java-web-crawler-1.0.0.jar
If your project uses the Maven directory structure, and your project has
no external dependencies, then the above minimal POM file is all you need to
build your project.
If your project does not
follow the standard directory structure, has external dependencies, or need
special actions during building, you will need to add more elements to the POM
file. These elements are listed in the Maven POM reference (see link
above).
In general you can specify a lot of things in the POM which gives Maven more
details about how to build your projects. See the Maven POM reference for
more information about what can be specified.
上面的groupId,artifactId和版本元素会导致在建一个JAR文件放入本地Maven存储库在以下路径(目录和文件名称):
MAVEN_REPO
/com/jenkov/java-web-crawler/1.0.0/java-web-crawler-1.0.0.jar
如果您的项目使用Maven的目录结构,和您的项目没有外部依赖,那么上述最小POM文件都是需要构建您的项目。
如果你的项目不遵循标准的目录结构,外部依赖,或需要特殊的动作在建设期间,您需要添加更多的元素POM文件。这些元素中列出的Maven POM参考(见上面的链接)。
一般来说您可以指定一个Maven POM中很多东西给更多的细节关于如何构建您的项目。有关更多信息,请参见Maven POM引用可以指定。
Super POM
All Maven POM files inherit
from a super POM. If no super POM is specified, the POM file inherits from
the base POM. Here is a diagram illustrating that:
超级POM
所有Maven POM文件从一个超级POM继承。如果没有指定超级POM,POM文件继承了POM的基地。这是一个图说明:

Super
POM and POM inheritance.超级POM和POM继承。
You can
make a POM file explicitly[A14] inherit[A15] from another POM file. That
way you can change the settings across all inheriting POM's via their common
super POM. You specify the super POM at the top of a POM file like this:
你可以做一个POM文件明确继承另一个POM文件。这样你可以更改设置在所有继承通过他们共同的超级POM POM的。您指定超级POM POM文件的顶部是这样的:
<project xmlns="http://maven./POM/4.0.0"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./POM/4.0.0
http://maven./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
...
</project>
An
inheriting POM file may override settings from a super POM. Just specify
new settings in the inheriting POM file.
POM inheritance is also covered in more detail in the Maven POM reference.
一个继承POM文件可能覆盖从超级POM设置。只是在继承POM文件中指定新设置。
POM的继承也详细介绍Maven POM参考。
Effective POM
With all this POM
inheritance it may be hard to know what the total POM file looks like when
Maven executes. The total POM file (result of all inheritance) is called
the effective POM. You can get Maven to show you the effective POM
using this command:
mvn help:effective-pom
This command will make Maven write out the effective POM to the command line
prompt.
有效的POM
所有这些POM继承它可能很难知道总Maven POM文件的样子时执行。总的POM文件(继承)的结果称为有效砰的一声。你可以得到Maven向你们展示有效的POM使用这个命令:
mvn帮助:effective-pom
这个命令将Maven POM写出有效命令行提示符。
Maven Settings File
Maven has two settings
files. In the settings files you can configure settings for Maven across
all Maven POM files. For instance, you can configure:
Location of local repository
Active build profile
Etc.
The settings files are
called settings.xml. The two settings files are located at:
The Maven installation directory: $M2_HOME/conf/settings.xml
The user's home
directory: ${user.home}/.m2/settings.xml
Both files are optional. If both files are present, the values in the user
home settings file overrides the values in the Maven installation settings
file.
You can read more about the Maven settings files in the Maven Settings
Reference.
Maven设置文件
Maven文件有两个设置。在设置文件中您可以配置设置Maven所有Maven POM文件。例如,您可以配置:
本地存储库的位置
积极构建配置文件
等。
被称为settings.xml设置文件。这两个设置文件位于:
Maven安装目录:$ M2_HOME / conf / settings.xml
用户的主目录:$ { user.home } / .m2 / settings.xml
两个文件是可选的。如果文件存在,则在用户家里的值设置文件覆盖Maven安装设置文件中的值。
你可以阅读更多关于Maven设置Maven中的文件设置参考。
Running Maven
When you
have installed Maven and have created a POM file and put
the POM file in the root directory of your project, you can run Maven on your
project.
Running Maven is done by executing the mvn command from a command
prompt. When executing the mvncommand you pass the name of
a build life cycle, phase or goal to it, which Maven then
executes. Here is an example:
mvn install
This
command executes the build phase called install (part of
the default build life cycle), which builds the project and copies
the packaged JAR file into the local Maven repository. Actually, this
command executes all build phases before install in the build phase
sequence, before executing the install build phase.
You can execute multiple build life cycles or phases by passing more than one
argument to the mvncommand. Here is an example:
mvn clean install
This command first
executes the clean build life cycle, which removes compiled classes
from the Maven output directory, and then it executes the install build
phase.
You can also execute a Maven goal (a subpart of a build phase) by passing the
build phase and goal name concatenated with a : in between, as parameter to the
Maven command. Here is an example:
mvn dependency:copy-dependencies
This command executes the copy-dependencies goal of
the dependency build phase.
运行Maven
当你安装了Maven和创造了一个POM文件,把POM文件在您的项目的根目录,您可以运行Maven项目。
运行Maven是通过执行mvn命令从命令提示符。当执行mvncommand你通过构建生命周期的名字,阶段或目标,Maven然后执行。这是一个例子:
mvn install
这个命令执行构建阶段称为安装(默认构建生命周期的一部分),构建项目,打包的JAR文件复制到本地Maven存储库。实际上,这个命令执行所有构建阶段在构建阶段的顺序安装之前,在执行之前安装构建阶段。
您可以执行多个构建生命周期或阶段mvncommand通过传递多个参数。这是一个例子:
mvn全新安装
这个命令首先执行清洁建造生命周期,从Maven输出目录中删除已编译类,然后执行安装构建阶段。
您还可以执行一个Maven目标(一个构建阶段的组成部分)通过构建阶段和目标连接名称:,Maven命令的参数。这是一个例子:
mvn dependency:copy-dependencies
这个命令执行的copy-dependencies目标构建阶段的依赖。
Running Maven
When you have installed
Maven and have created a POM file and put the POM file in the
root directory of your project, you can run Maven on your project.
Running Maven is done by executing the mvn command from a command
prompt. When executing the mvncommand you pass the name of
a build life cycle, phase or goal to it, which Maven then
executes. Here is an example:
Maven Directory Structure
Maven has a standard directory
structure. If you follow that directory structure for your project, you do
not need to specify the directories of your source code, test code etc. in your
POM file.
You can see the full directory layout in the Introduction to the Maven
Standard Directory Layout.
Here are the most important directories:
运行Maven
当你安装了Maven和创造了一个POM文件,把POM文件在您的项目的根目录,您可以运行Maven项目。
运行Maven是通过执行mvn命令从命令提示符。当执行mvncommand你通过构建生命周期的名字,阶段或目标,Maven然后执行。这是一个例子:
Maven的目录结构
Maven标准目录结构。如果你遵循你的项目目录结构,您不需要指定源代码的目录,POM文件测试代码等。
你可以看到完整的目录布局在Maven标准目录布局。
这里有最重要的目录:
- src
- main
- java
- resources
- webapp
- test
- java
- resources
- target
The src directory
is the root directory of your source code and test code. The main directory
is the root directory for source code related to the application itself (not
test code). The test directory contains the test source
code. The java directories
under main and test contains the Java code for the
application itself (under main) and the Java code for the tests (under
test).
The resources directory contains other resources needed by your
project. This could be property files used for internationalization of an
application, or something else.
The webapp directory
contains your Java web application, if your project is a web
application. The webappdirectory will then be the root directory of
the web application. Thus the webapp directory contains
the WEB-INF directory etc.
The target directory is created by Maven. It contains all the
compiled classes, JAR files etc. produced by Maven. When executing the clean build
phase, it is the target directory which is cleaned.
src目录是根目录的源代码和测试代码。源代码的主要目录是根目录与应用程序本身(而不是测试代码)。测试目录包含测试源代码。java目录下主要包含java代码和测试应用程序本身(主要)和java代码的测试(测试)。
资源目录包含项目所需的其他资源。这可能是属性文件用于应用程序的国际化,或者其他东西。
webapp目录包含您的Java web应用程序,如果你的项目是一个web应用程序。webappdirectory将web应用程序的根目录。因此,webapp目录包含web - inf目录等。
目标目录是由Maven。它包含所有已编译类的JAR文件等由Maven。当执行清洁的构建阶段,清洗的目标目录。
Project Dependencies
Unless your project is small,
your project may need external Java APIs or frameworks which are packaged in
their own JAR files. These JAR files are needed on the classpath when you
compile your project code.
Keeping your project
up-to-date with the correct versions of these external JAR files can be a
comprehensive task. Each external JAR may again also need other external
JAR files etc. Downloading all these external dependencies (JAR files)
recursively and making sure that the right versions are downloaded is
cumbersome. Especially when your project grows big, and you get more and
more external dependencies.
Luckily, Maven has built-in
dependency management. You specify in the POM file what external libraries
your project depends on, and which version, and then Maven downloads them for
you and puts them in your local Maven repository. If any of these external
libraries need other libraries, then these other libraries are also downloaded
into your local Maven repository.
You specify your project
dependencies inside the dependencies element in the POM
file. Here is an example:
项目依赖项
除非你的项目很小,您的项目可能需要外部Java api或框架打包的JAR文件。这些JAR文件需要在类路径中当你编译您的项目的代码。
保持您的项目与这些外部JAR文件的正确版本最新的可以是一个综合的任务。每个外部JAR可能还需要再下载其他外部JAR文件等等。所有这些外部依赖(JAR文件)递归和确保正确的版本下载很麻烦。特别是当你的项目越来越大,得到越来越多的外部依赖。
幸运的是,Maven内置的依赖关系管理。您指定的POM文件外部库项目取决于什么,哪个版本,然后Maven下载给你,并在本地Maven资源库中。如果这些外部库需要其他的库,那么这些其他图书馆也下载到本地Maven资源库中。
您指定您的项目依赖内部元素POM文件的依赖关系。这是一个例子:
<project xmlns="http://maven./POM/4.0.0"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./POM/4.0.0
http://maven./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jenkov.crawler</groupId>
<artifactId>java-web-crawler</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>
Notice the
dependencies element in bold. Inside it are
two dependency elements. Each dependency element
describes an external dependency.
Each dependency is described by
its groupId, artifactId and version. You may remember
that this is also how you identified your own project in the beginning of the
POM file. The example above needs
theorg.jsoup group's jsoup artifact in version 1.7.1, and
the junit group's junit artifact in version 4.8.1.
When this POM file is executed
by Maven, the two dependencies will be downloaded from a central Maven
repository and put into your local Maven repository. If the dependencies
are already found in your local repository, Maven will not download
them. Only if the dependencies are missing will they be downloaded into
your local repository.
Sometimes a given
dependency is not available in the central Maven repository. You can then
download the dependency yourself and put it into your local Maven
repository. Remember to put it into a subdirectory structure matching
the groupId, artifactId and version. Replace all dots
(.) with / and separate the groupId, artifactId and version with
/ too. Then you have your subdirectory structure.
The two dependencies
downloaded by the example above will be put into the following subdirectories:
MAVEN_REPOSITORY_ROOT/junit/junit/4.8.1
MAVEN_REPOSITORY_ROOT/org/jsoup/jsoup/1.7.1
请注意元素大胆的依赖关系。里面有两个元素的依赖。每个依赖元素描述外部依赖。
每个依赖描述groupId,artifactId和版本。你可能还记得,这是你如何确定自己的项目在POM文件的开始。上面的示例需要theorg。jsoup集团jsoup工件在版本1.7.1上,junit集团在4.8.1版本的junit工件。
当这个执行的Maven POM文件,这两个依赖项将从一个中央Maven存储库下载到本地Maven资源库中。如果依赖已经发现在你的本地存储库,Maven不会下载它们。只有缺少的依赖关系会被下载到你的本地存储库。
有时一个给定的依赖不是中央Maven存储库中可用。然后您可以依赖自己,把它下载到本地Maven资源库中。记得把它放到一个子目录结构匹配的groupId,artifactId和版本。替换所有点(.)/和单独的groupId / artifactId和版本。然后你的子目录结构。
上面的两个附件下载的示例将放在以下目录:
MAVEN_REPOSITORY_ROOT / junit junit / 4.8.1
MAVEN_REPOSITORY_ROOT /org/jsoup/jsoup/1.7.1
External Dependencies
An
external dependency in Maven is a dependency (JAR file) which is not located in
a Maven repository (neiterh local, central or remote repository). It may
be located somewhere on your local hard disk, for instance in
the lib directory of a webapp, or somewhere else. The word
"external" thus means external to the Maven repository system - not
just external to the project. Most dependencies are external to the
project, but few are external to the repository system (not located in a
repository).
You configure an external dependency like this:
外部依赖关系
外部依赖关系在Maven的依赖项(JAR文件)不是位于Maven存储库(neiterh地方、中央或远程存储库)。它可能位于本地硬盘上的某个地方,例如webapp的lib目录,或其他地方。“外部”一词因此意味着外部到Maven存储库系统——不仅仅是外部的项目。大多数依赖外部的项目,但很少有外部存储库系统(而不是位于存储库)。
你配置一个外部依赖关系是这样的:
<dependency>
<groupId>mydependency</groupId>
<artifactId>mydependency</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\war\WEB-INF\lib\mydependency.jar</systemPath>
</dependency>
The groupId and artifactId are
both set to the name of the dependency. The name of the API used, that
is. The scope element value is set
to system. The systemPath element is set to point to the
location of the JAR file containing the
dependency. The ${basedir} points to the directory where the POM
is located. The rest of the path is relative from that directory.
groupId和artifactId都将依赖项的名称。API使用的名称。元素值设置为系统范围。systemPath元素设置为指向包含依赖JAR文件的位置。$ { basedir }指向POM所在的目录。其余的从该目录路径是相对的。
Snapshot Dependencies
Snapshot
dependencies are dependencies (JAR files) which are under development. Instead
of constantly updating the version numbers to get the latest version, you can
depend on a snapshot version of the project. Snapshot versions are always
downloaded into your local repository for every build, even if a matching
snapshot version is already located in your local repository. Always
downloading the snapshot dependencies assures that you always have the latest
version in your local repository, for every build.
You can tell Maven that
your project is a snapshot version simply by appending -SNAPSHOT to
the version number in the beginning of the POM (where you also set
the groupId and artifactId). Here is a versionelement
example:
快照的依赖性.
快照依赖关系正在开发的依赖项(JAR文件)。而不是不断更新最新版本的版本号,你可以依靠一个快照版本的项目。快照版本总是为每一个构建下载到你的本地存储库,即使一个匹配的快照版本已经位于您的本地存储库中。总是下载依赖快照确保你总是有最新版本在你的本地存储库,为每一个构建。
你可以告诉Maven项目是一个快照版本只需添加版本号的快照POM的开始(你也设置groupId和artifactId)。这是一个versionelement的例子:
<version>1.0-SNAPSHOT</version>
Notice the -SNAPSHOT appended to the version number.
Depending on a snapshot
version is also done by appending the -SNAPSHOT after the version
number when configuring dependencies. Here is an example:
注意到快照添加到版本号。
根据快照版本也由附加后的快照版本号在配置依赖关系。这是一个例子:
<dependency>
<groupId>com.jenkov</groupId>
<artifactId>java-web-crawler</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
The -SNAPSHOT appended to the version number
tells Maven that this is a snapshot version.
You can configure how often Maven shall download snapshot dependencies in
the Maven Settings File.
快照添加到版本号告诉Maven版本,这是一个快照。
您可以配置Maven多久下载快照在Maven依赖设置文件。
Maven Repositories
Maven repositories are
directories of packaged JAR files with extra meta data. The meta data are
POM files describing the projects each packaged JAR file belongs to, including
what external dependencies each packaged JAR has. It is this meta data
that enables Maven to download dependencies of your dependencies recursively,
until the whole tree of dependencies is download and put into your local
repository.
Maven repositories are covered
in more detail in the Maven Introduction to Repositories, but here is a
quick overview.
Maven has three types of repository:
Local repository
Central repository
Remote repository
Maven searches these
repositories for dependencies in the above sequence. First in the local
repository, then in the central repository, and third in remote repositories if
specified in the POM.
Maven存储库
Maven存储库目录打包的JAR文件的额外的元数据。元数据是描述项目的POM文件每个属于打包的JAR文件,包括外部依赖每个打包的JAR。正是这种元数据,使Maven下载依赖你的递归地依赖,直到整棵树的依赖关系是下载并放入您的本地存储库中。
Maven存储库会详细介绍Maven存储库简介,但这里是一个快速概述。
Maven有三种类型的存储库:
本地存储库
中央存储库
远程存储库
Maven搜索这些存储库的依赖性在上面的序列。首先在本地存储库,然后在中央存储库,和第三如果POM中指定远程存储库。
Here is a diagram illustrating the three repository types and
their location:
这是一个图说明三个库类型和位置:

Maven
Repository Types and Location. Maven存储库类型和位置。
Local Repository
A local repository is a
directory on the developer's computer. This repository will contain all
the dependencies Maven downloads. The same Maven repository is typically
used for several different projects. Thus Maven only needs to download the
dependencies once, even if multiple projects depends on them (e.g. Junit).
Your own projects can also be
built and installed in your local repository, using the mvn
install command. That way your other projects can use the packaged
JAR files of your own projects as external dependencies by specifying them as
external dependencies inside their Maven POM files.
By default Maven puts your local repository inside your user home directory on
your local computer. However, you can change the location of the local
repository by setting the directory inside your Maven settings file. Your
Maven settings file is also located in your user-home/.m2 directory
and is calledsettings.xml. Here is how you specify another location for
your local repository:
本地存储库
本地存储库是一个开发人员的计算机上的目录。这个存储库将包含所有下载Maven的依赖关系。相同的Maven存储库通常用于不同的项目。因此Maven只需要下载的依赖关系,即使多个项目(例如Junit)取决于他们。
自己的项目也可以建造和安装在你的本地存储库,使用mvn install命令。这样你的其他项目可以使用JAR文件打包自己的项目外部依赖通过指定外部依赖关系在他们的Maven POM文件。
默认Maven将您的本地存储库在本地电脑上用户的主目录。但是,你可以改变本地存储库的位置通过设置目录在您的Maven设置文件。Maven设置文件也位于你的户主/。m2目录和called settings.xml。这是你如何为你的本地存储库:指定另一个位置.
<settings>
<localRepository>
d:\data\java\products\maven\repository
</localRepository>
</settings>
Central Repository
The central Maven
repository is a repository provided by the Maven community. By default
Maven looks in this central repository for any dependencies needed but not
found in your local repository. Maven then downloads these dependencies
into your local repository. You need no special configuration to access
the central repository.
Remote Repository
A remote repository is a
repository on a web server from which Maven can download dependencies, just
like the central repository. A remote repository can be located anywhere
on the internet, or inside a local network.
A remote repository is often
used for hosting projects internal to your organization, which are shared by
multiple projects. For instance, a common security project might be used
across multiple internal projects. This security project should not be
accessible to the outside world, and should thus not be hosted in the public,
central Maven repository. Instead it can be hosted in an internal remote
repository.
中央存储库
中央Maven存储库是一个Maven存储库提供的社区。默认Maven中央存储库中查找所需依赖关系,但没有发现在你的本地存储库。Maven然后下载这些依赖关系到你的本地存储库。你不需要任何特殊配置访问中央存储库。
远程存储库
一个远程存储库是一个Maven存储库web服务器上可以下载依赖,就像中央存储库。一个远程存储库可以位于任何地方在互联网上,或者在一个本地网络。
一个远程存储库通常用于举办项目组织的内部,是由多个项目共享。例如,一个共同的安全项目可能使用多个内部项目。这个安全项目不应该访问的外部世界,并因此不应该驻留在公众,中央Maven存储库。相反,它可以驻留在一个内部的远程存储库。
Dependencies found in a remote
repository are also downloaded and put into your local repository by Maven.
You can configure a remote
repository in the POM file. Put the following XML elements right after
the<dependencies> element:
您可以配置一个远程存储库的POM文件。把下面的XML元素之后<dependencies> element:
<repositories>
<repository>
<id>jenkov.code</id>
<url>http://maven./maven2/lib</url>
</repository>
</repositories>
Maven Build Life Cycles, Phases and Goals
When Maven builds a software
project it follows a build life cycle. The build life cycle is divided
into build phases, and the build phases are divided into build
goals. Maven build life cycles, build phases and goals are described in
more detail in the Maven Introduction to Build Phases, but here I will
give you a quick overview.
Build Life Cycles
Maven has 3 built-in build life cycles. These are:
default
clean
site
Each of these build life
cycles takes care of a different aspect of building a software
project. Thus, each of these build life cycles are executed independently
of each other. You can get Maven to execute more than one build life
cycle, but they will be executed in sequence, separately from each other, as if
you had executed two separate Maven commands.
The default life
cycle handles everything related to compiling and packaging your
project. The clean life cycle handles everything related to
removing temporary files from the output directory, including generated source
files, compiled classes, previous JAR files etc. The site life cycle
handles everything related to generating documentation for your
project. In fact, site can generate a complete website with
documentation for your project.
Maven构建生命周期、阶段和目标
Maven构建软件项目的时候是一个构建生命周期。构建生命周期分为构建阶段,和构建阶段分为构建目标。Maven构建生命周期、构建阶段和目标更详细地描述了Maven介绍构建阶段,但在这里我将给你一个快速概述。
建立生命周期
Maven 3内置构建生命周期。这些都是:
默认的
清洁
网站
每一个构建生命周期负责建立一个软件项目的不同方面。因此,每一个构建生命周期是彼此独立地执行。你可以得到Maven执行多个构建生命周期,但他们将按顺序执行,彼此分开,因为如果你有两个单独的Maven命令执行。
缺省生命周期处理一切有关编译和打包您的项目。清洁处理一切有关生命周期从输出目录删除临时文件,包括生成的源文件,编译类,先前的JAR文件等。该网站生命周期处理一切相关的为您的项目生成文档。事实上,网站可以生成一个完整的网站为您的项目文档。
Build Phases
Each build life cycle is
divided into a sequence of build phases, and the build phases are again
subdivided into goals. Thus, the total build process is a sequence of
build life cycle(s), build phases and goals.
You can execute either a whole
build life cycle like clean or site,
a build phase like install which is part of the default build
life cycle, or a build goal like dependency:copy-dependencies. Note:
You cannot execute the default life
cycle directly. You have to specify a build phase or goal inside the default life
cycle.
When you execute a build
phase, all build phases before that build phase in this standard phase sequence
are executed. Thus, executing the install build
phase really means executing all build phases before the install phase,
and then execute the install phase
after that.
The default life
cycle is of most interest since that is what builds the code. Since you
cannot execute the default life
cycle directly, you need to execute a build phase or goal from the default life
cycle. The default life
cycle has an extensive sequence of build phases and goals, ,so I will not
describe them all here. The most commonly used build phases are:
构建阶段
每个构建生命周期分为一系列的构建阶段,和构建阶段又分为目标。因此,整个构建过程是构建生命周期的序列,构建阶段和目标。
您可以执行一个整体构建生命周期清洁或网站,一个构建阶段像安装默认构建生命周期的一部分,或构建目标像依赖:copy-dependencies。注意:你不能直接执行thedefault生命周期。您必须指定一个构建阶段或目标内部默认的生命周期。
当您执行一个构建阶段,构建阶段之前,在执行本标准相序建立阶段。因此,执行安装构建阶段的真正含义之前执行所有构建阶段安装阶段,然后执行安装阶段。
缺省生命周期是最感兴趣的,因为这就是构建代码。既然你不能直接执行the default生命周期中,您需要执行 一个构建阶段或目标从默认的生命周期。defaultlife周期有一个广泛的序列构建阶段和目标,,所以我不会描述他们都在这里。最常用的构建阶段是:
Build Phase
|
Description
|
validate
|
Validates that
the project is correct and all necessary information is available. This also
makes sure the dependencies are downloaded.
|
compile
|
Compiles the
source code of the project.
|
test
|
Runs the tests
against the compiled source code using a suitable unit testing framework.
These tests should not require the code be packaged or deployed.
|
package
|
Packs the compiled
code in its distributable format, such as a JAR.
|
install
|
Install the
package into the local repository, for use as a dependency in other projects
locally.
|
deploy
|
Copies the final
package to the remote repository for sharing with other developers and
projects.
|
You execute one of these build phases by passing its name to
the mvn command. Here is an example:
你执行一个构建阶段通过mvn命令的名称。这是一个例子:
This example executes
the package build phase, and thus also all build phases before it in
Maven's predefined build phase sequence.
If the standard Maven build phases and goals are not enough to build your
project, you can create Maven plugins to add the extra build
functionality you need.
Build Goals
Build goals are the finest
steps in the Maven build process. A goal can be bound to one or more build
phases, or to none at all. If a goal is not bound to any build phase, you
can only execute it by passing the goals name to
the mvn command. If a goal is bound to multiple build phases,
that goal will get executed during each of the build phases it is bound to.
这个例子执行包构建阶段,因此也在Maven构建阶段之前的预定义的序列构建阶段。
如果标准Maven构建阶段和目标并不足以构建您的项目,您可以创建Maven插件添加额外的构建功能需要。
建立目标
建立目标是最好的Maven构建过程中的步骤。一个目标可以绑定到一个或多个构建阶段,或根本没有。如果一个目标是没有绑定到构建阶段,你只能通过执行它的目标名称mvn命令。如果目标是绑定到多个构建阶段,这一目标将在每次执行构建阶段的必然。
Maven Build Profiles
Maven build profiles
enable you to build your project using different configurations. Instead
of creating two separate POM files, you can just specify a profile with the
different build configuration, and build your project with this build profile
when needed.
You can read the full story
about build profiles in the Maven POM reference under Profiles. Here
I will give you a quick overview though.
Maven构建配置文件
Maven构建配置文件使您能够使用不同的配置构建您的项目。而不是创建两个单独的POM文件,你可以指定一个配置文件与不同的构建配置,并建立项目的构建配置文件。
您可以阅读完整的故事构建配置文件下的Maven POM参考资料。在这里我将给你一个快速概述。
Maven build profiles are
specified inside the POM file, inside the profiles element. Each
build profile is nested inside a profile element. Here is an
example:
Maven构建配置文件指定在POM文件,内部配置文件元素。每个构建文件元素嵌套在一个概要文件。这是一个例子:
<project xmlns="http://maven./POM/4.0.0"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./POM/4.0.0
http://maven./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jenkov.crawler</groupId>
<artifactId>java-web-crawler</artifactId>
<version>1.0.0</version>
<profiles>
<profile>
<id>test</id>
<activation>...</activation>
<build>...</build>
<modules>...</modules>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<dependencies>...</dependencies>
<reporting>...</reporting>
<dependencyManagement>...</dependencyManagement>
<distributionManagement>...</distributionManagement>
</profile>
</profiles>
</project>
A build profile describes what
changes should be made to the POM file when executing under that build profile. This
could be changing the applications configuration file to use etc. The elements
inside theprofile element will override the values of the elements with
the same name further up in the POM.
Inside
the profile element you can see a activation element. This
element describes the condition that triggers this build profile to be
used. One way to choose what profile is being executed is in the
settings.xml file. There you can set the active profile. Another
way is to add -P profile-name to the Maven command line. See the
profile documentation for more information.
构建配置文件描述了变化时应该对POM文件执行构建配置文件。这可能是改变应用程序配置文件使用等。theprofile元素内的元素将会覆盖具有相同名称的元素的值进一步砰的一声。
在配置文件元素可以看到激活元素。这个元素描述了使用条件,触发这个构建配置文件。选择概要文件被执行的一个方法是在settings.xml文件中。你可以设置当前文件。另一种方法是- p配置文件名添加到Maven命令行。看到这个概要文件文档的更多信息。
Maven Plugins
Maven plugins enable you to
add your own actions to the build process. You do so by creating a simple
Java class that extends a special Maven class, and then create a POM for the
project. The plugin should be located in its own project.
To keep this tutorial short, I
will refer to the Maven Plugin Developers Centre for more information
about developing plugins.
Maven插件
Maven插件使您可以添加自己的行动来构建过程。你这样做,通过创建一个简单的Java类,它扩展了一个特殊的Maven类,然后创建一个POM的项目。这个插件应该位于自己的项目。
保持本教程,我将参考Maven插件开发者中心开发插件的更多信息。