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的網(wǎng)站
構(gòu)建工具是什么?
安裝Maven
Maven概述——核心概念
Maven和螞蟻
Maven POM文件
超級(jí)POM
有效的POM
Maven設(shè)置文件
運(yùn)行Maven
Maven的目錄結(jié)構(gòu)
項(xiàng)目依賴(lài)項(xiàng)
外部依賴(lài)關(guān)系
快照的依賴(lài)性
Maven存儲(chǔ)庫(kù)
Maven構(gòu)建生命周期、階段和目標(biāo)
Maven構(gòu)建配置文件
Maven插件
Maven is a powerful buildtool for Java software projects. Actually, you can build software projectsusing other languages too, but Maven is developed in Java, and is thushistorically used more for Java projects.
Maven是一個(gè)強(qiáng)大的Java軟件項(xiàng)目構(gòu)建工具。事實(shí)上,你也可以使用其他語(yǔ)言構(gòu)建的軟件項(xiàng)目,但Maven是Java開(kāi)發(fā)的,因此歷史上用于Java項(xiàng)目。
The
[A1] of this Maven
[A2] is to make youunderstand how Maven works.
[A3] thistutorial
[A4] on the core
[A5] of Maven. Once youunderstand the core concepts, it is much easier to
[A6] thefine detail in the Maven documentation, or search for it on the internet.
這個(gè)Maven教程的目的是讓你理解Maven是如何工作的。因此本教程的重點(diǎn)是Maven的核心概念。一旦你理解核心概念,它更容易查找Maven的細(xì)節(jié)的文檔,或在互聯(lián)網(wǎng)上搜索它。
Actually,the Maven developers
[A7] that Maven is more thanjust a build tool. You can read what they believe it is, in theirdocument
[A8] of Maven. But fornow, just think of it as a build tool. You will find out what Maven reallyis, once you understand it and start using it.
實(shí)際上,Maven開(kāi)發(fā)者聲稱(chēng)Maven不僅僅是一個(gè)構(gòu)建工具。你可以閱讀他們相信這是什么,在他們的文檔的Maven哲學(xué)。但是現(xiàn)在,只是把它看作一個(gè)構(gòu)建工具。你會(huì)發(fā)現(xiàn)Maven是什么,一旦你理解它并開(kāi)始使用它。
Thefirst 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 ofthis tutorial. The updates were tested with Maven 3.3.3.
這個(gè)Maven教程的第一個(gè)版本是基于Maven3.0.5。不過(guò),本教程已經(jīng)更新在本教程的第一個(gè)版本以來(lái)的幾個(gè)地方。的更新與Maven 3.3.3測(cè)試。
A buildtool is a tool that
[A9] everything related tobuilding the software project. Building a software project
[A10] includes one or more ofthese 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.
一個(gè)構(gòu)建工具是一個(gè)工具,自動(dòng)構(gòu)建軟件項(xiàng)目相關(guān)的一切。建立一個(gè)軟件項(xiàng)目通常包括一個(gè)或多個(gè)活動(dòng):
生成的源代碼(如果使用自動(dòng)生成的代碼的項(xiàng)目)。
生成文檔的源代碼。
編譯源代碼。
編譯后的代碼打包成JAR文件或ZIP文件。
安裝打包代碼在一個(gè)服務(wù)器上,在一個(gè)存儲(chǔ)庫(kù)或其他地方。
Anygiven software project may have more activities than these needed to build thefinished software. Such activities can normally be plugged into a buildtool, so these activities can be automated too.
任何軟件項(xiàng)目可能比這些活動(dòng)需要構(gòu)建完成的軟件。這些活動(dòng)通常可以插入一個(gè)構(gòu)建工具,這些活動(dòng)也可以是自動(dòng)進(jìn)行的。
The
[A11] of the build
[A12] isthat you
[A13] the risk of humansmaking errors while building the software manually. Additionally, anautomated build tool is typically faster than a human performing the same stepsmanually.
自動(dòng)化構(gòu)建過(guò)程的優(yōu)點(diǎn)是,你減少人類(lèi)犯錯(cuò)誤的風(fēng)險(xiǎn),而手動(dòng)構(gòu)建的軟件。此外,一個(gè)自動(dòng)構(gòu)建工具通常是速度比人工手動(dòng)執(zhí)行相同的步驟。
Toinstall Maven on your own system (computer), go to the Maven downloadpage and follow the instructions there. In summary, what you need todo 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 youunzipped Maven to.
Set the M2 environment variable to pointto M2_HOME/bin (%M2_HOME%\bin on Windows, $M2_HOME/bin onunix).
Add M2 to the PATH environment variable (%M2% onWindows, $M2 on unix).
Open a command prompt and type 'mvn -version' (without quotes) and press enter.
安裝Maven在您自己的系統(tǒng)(計(jì)算機(jī)),去Maven下載頁(yè)面,按提示操作??傊?你需要做的是:
設(shè)置JAVA_HOME環(huán)境變量指向一個(gè)有效的JavaSDK(例如Java 8)。
下載并解壓縮Maven。
設(shè)置M2_HOME環(huán)境變量,使之指向您解壓的目錄Maven。
M2設(shè)置環(huán)境變量,使之指向M2_HOME /bin(% M2_HOME % \ bin在Windows上,美元M2_HOME unix / bin)。
M2添加到PATH環(huán)境變量(% M2%在Windows上,美元M2在unix)。
打開(kāi)一個(gè)命令提示符并輸入mvn - version(沒(méi)有引號(hào))和按回車(chē)。
Aftertyping in the mvn -version command you should be able to see Mavenexecute, and the version number of Maven written out to the command prompt.
Note: Maven uses Java when executing, so you need Java installed too (andthe JAVA_HOME environment variable set as explainedabove). Maven 3.0.5 needs a Java version 1.5 or later. I use Maven3.3.3 with Java 8 (u45).
I have a tutorial about installing the Java SDK in case you are notfamiliar with that. Remember, it has to be an SDK (Software DeveloperKit), not just a JRE (Java Runtime Environment). The JRE does not containa Java compiler. Only the SDK does.
后輸入mvn您應(yīng)該能夠看到Maven - version命令執(zhí)行,和Maven版本號(hào)寫(xiě)入命令提示符。
注意:Maven使用Java在執(zhí)行,所以你也需要安裝Java(JAVA_HOME環(huán)境變量設(shè)置為上面的解釋)。Maven 3.0.5需要一個(gè)Java版本1.5或更高版本。我使用Maven 3.3.3與Java 8(u45)。
我有一個(gè)關(guān)于安裝Java SDK的教程,以防你不熟悉。記住,它必須是一個(gè)SDK(軟件開(kāi)發(fā)工具包),而不只是一個(gè)JRE(Java運(yùn)行時(shí)環(huán)境)。JRE不包含一個(gè)Java編譯器。只有SDK。
Mavenis centered around the concept of POM files (Project Object Model). A POMfile is an XML representation of project resources like source code, test code,dependencies (external JARs used) etc. The POM contains references to all ofthese resources. The POM file should be located in the root directory ofthe project it belongs to.
Maven是圍繞POM文件(項(xiàng)目對(duì)象模型)的概念。POM文件是一個(gè)XML表示的項(xiàng)目資源(如源代碼、測(cè)試代碼、依賴(lài)外部jar(使用)等。POM包含對(duì)所有這些資源的引用。POM文件應(yīng)該位于所屬項(xiàng)目的根目錄。
Here isa diagram illustrating how Maven uses the POM file, and what the POM fileprimarily contains:
這是一個(gè)圖說(shuō)明如何Maven POM文件使用,和POM文件主要包含:
Overviewof Maven core concepts.Maven核心概念的概述。
These concepts are explainedbriefly below to give you an overview, and then in more detail in their ownsections later in this tutorial.
這些概念簡(jiǎn)要解釋下面給你一個(gè)概述,然后詳細(xì)的部分在本教程。
POM Files
When you execute a Mavencommand you give Maven a POM file to execute the commands on. Maven willthen execute the command on the resources described in the POM.
Build Life Cycles, Phases and Goals
The build process inMaven is split up into build life cycles, phases and goals. A build lifecycle consists of a sequence of build phases, and each build phase consists ofa sequence of goals. When you run Maven you pass a command toMaven. This command is the name of a build life cycle, phase orgoal. If a life cycle is requested executed, all build phases in that lifecycle are executed. If a build phase is requested executed, all build phasesbefore it in the pre-defined sequence of build phases are executed too.
POM文件
當(dāng)你執(zhí)行一個(gè)Maven命令你給Maven POM文件上執(zhí)行命令。Maven將執(zhí)行命令在POM中描述的資源。
建立生命周期、階段和目標(biāo)
在Maven構(gòu)建過(guò)程分成構(gòu)建生命周期、階段和目標(biāo)。構(gòu)建生命周期由一序列構(gòu)建階段,并且每個(gè)構(gòu)建階段由一系列的目標(biāo)。當(dāng)您運(yùn)行Maven你通過(guò)Maven命令。這個(gè)命令的名字是構(gòu)建生命周期、階段或目標(biāo)。如果請(qǐng)求生命周期執(zhí)行,所有構(gòu)建階段生命周期執(zhí)行。如果一個(gè)構(gòu)建階段要求執(zhí)行,所有構(gòu)建階段之前,在構(gòu)建階段的預(yù)定義的序列執(zhí)行。
Dependencies and Repositories
One of the first goalsMaven executes is to check the dependencies needed by yourproject. Dependencies are external JAR files (Java libraries) that yourproject uses. If the dependencies are not found in the local Mavenrepository, Maven downloads them from a central Maven repository and puts themin your local repository. The local repository is just a directory on yourcomputer's hard disk. You can specify where the local repository should belocated if you want to (I do). You can also specify which remoterepository to use for downloading dependencies. All this will be explainedin more detail later in this tutorial.
依賴(lài)關(guān)系和存儲(chǔ)庫(kù)
Maven執(zhí)行的第一個(gè)目標(biāo)是檢查您的項(xiàng)目所需的依賴(lài)項(xiàng)。依賴(lài)關(guān)系是外部JAR文件(Java庫(kù)),您的項(xiàng)目使用。如果依賴(lài)不是本地Maven資源庫(kù)中,Maven下載從中央Maven存儲(chǔ)庫(kù),并將它們?cè)谀愕谋镜卮鎯?chǔ)庫(kù)。本地存儲(chǔ)庫(kù)是一個(gè)目錄在您的計(jì)算機(jī)上的硬盤(pán)。您可以指定本地存儲(chǔ)庫(kù)應(yīng)該處的位置如果你想(我)。您還可以指定使用哪一個(gè)遠(yuǎn)程存儲(chǔ)庫(kù)下載依賴(lài)關(guān)系。這一切將會(huì)更詳細(xì)地解釋在本教程。
Build Plugins
Build plugins are usedto insert extra goals into a build phase. If you need to perform a set ofactions for your project which are not covered by the standard Maven buildphases and goals, you can add a plugin to the POM file. Maven has somestandard plugins you can use, and you can also implement your own in Java ifyou need to.
構(gòu)建插件
構(gòu)建插件用于插入額外的目標(biāo)變成一個(gè)構(gòu)建階段。如果您需要為您的項(xiàng)目執(zhí)行的一組操作,不受標(biāo)準(zhǔn)Maven構(gòu)建階段和目標(biāo),您可以添加一個(gè)插件POM文件。Maven有一些標(biāo)準(zhǔn)的插件可以使用,還可以在Java中實(shí)現(xiàn)自己的如果你需要。
Build Profiles
Build profiles are used if youneed to build your project in different ways. For instance, you may needto build your project for your local computer, for development andtest. And you may need to build it for deployment on your productionenvironment. These two builds may be different. To enable differentbuilds you can add different build profiles to your POM files. Whenexecuting Maven you can tell which build profile to use.
構(gòu)建配置文件
構(gòu)建配置文件使用如果你需要以不同的方式來(lái)構(gòu)建您的項(xiàng)目。例如,您可能需要為您的本地計(jì)算機(jī)構(gòu)建您的項(xiàng)目,開(kāi)發(fā)和測(cè)試。你可能需要構(gòu)建它部署在您的生產(chǎn)環(huán)境。這兩個(gè)版本可能會(huì)有所不同。使不同的構(gòu)建可以添加不同的構(gòu)建配置文件到你的POM文件。當(dāng)執(zhí)行Maven可以告訴構(gòu)建配置文件來(lái)使用。
Maven vs. Ant
Ant is another popularbuild tool by Apache. If you are used to Ant and you are trying to learnMaven, you will notice a difference in the approach of the two projects.
Ant uses an imperativeapproach, meaning you specify in the Ant build file what actions Ant shouldtake. You can specify low level actions like copying files, compiling codeetc. You specify the actions, and you also specify the sequence in which theyare carried out. Ant has no default directory layout.
Maven uses a more declarativeapproach, meaning that you specify in the Maven POM file what tobuild, but now how to build it. The POM file describes yourproject resources - not how to build it. Contrarily, an Ant file describeshow to build your project. In Maven, how to build your project ispredefined in the Maven Build Life Cycles, Phases and Goals.
Maven和螞蟻
ApacheAnt是另一個(gè)流行的構(gòu)建工具。如果你習(xí)慣于Ant,你想學(xué)習(xí)Maven,您將注意到兩個(gè)項(xiàng)目的不同方法。
Ant使用命令式的方法,這意味著您指定的Ant構(gòu)建文件螞蟻應(yīng)該采取什么行動(dòng)。您可以指定低水平復(fù)制文件等操作,編譯代碼等您指定的動(dòng)作,你也指定的順序進(jìn)行。螞蟻沒(méi)有默認(rèn)的目錄布局。
Maven使用更多的聲明性方法,這意味著您指定的Maven POM文件構(gòu)建什么,但現(xiàn)在如何構(gòu)建它。POM文件描述您的項(xiàng)目資源,而不是如何構(gòu)建它。相反,一個(gè)Ant文件描述如何構(gòu)建您的項(xiàng)目。在Maven,如何構(gòu)建您的項(xiàng)目是預(yù)定義的Maven構(gòu)建生命周期的階段和目標(biāo)。
Maven POM Files
A Maven POM file (ProjectObject Model) is an XML file that describe the resources of theproject. This includes the directories where the source code, test sourceetc. is located in, what external dependencies (JAR files) your projects hasetc.
The POM file describes what to build, but most oftennot how to build it. How to build it is up to the Maven buildphases and goals. You can insert custom actions (goals) into the Mavenbuild phase if you need to, though.
Each project has a POMfile. The POM file is named pom.xml and should be located in theroot directory of your project. A project divided into subprojects willtypically have one POM file for the parent project, and one POM file for eachsubproject. This structure allows both the total project to be built inone step, or any of the subprojects to be built separately.
Maven POM文件
一個(gè)Maven POM文件(項(xiàng)目對(duì)象模型)是一個(gè)XML文件,描述項(xiàng)目的資源。這包括源代碼的目錄、測(cè)試等位于來(lái)源,外部依賴(lài)(JAR文件)項(xiàng)目等。
POM文件描述了如何構(gòu)建,但通常不是如何構(gòu)建它。如何構(gòu)建由Maven構(gòu)建階段和目標(biāo)。你可以插入自定義動(dòng)作(目標(biāo))到Maven構(gòu)建階段,如果你需要。
每個(gè)項(xiàng)目的POM文件。名為POM的POM文件。xml和應(yīng)位于您的項(xiàng)目的根目錄。一個(gè)項(xiàng)目分成子項(xiàng)目通常會(huì)有一個(gè)父項(xiàng)目的POM文件,每個(gè)子項(xiàng)目和一個(gè)POM文件。這個(gè)結(jié)構(gòu)允許的項(xiàng)目總建在一個(gè)步驟中,或任何的子項(xiàng)目分別建成。
Throughoutthe rest of this section I will describe the most important parts of the POMfile. For a full reference of the POM file, see the Maven POMReference.
在本節(jié)的其余部分,我將描述POM文件的最重要的部分。完整的POM文件的參考,請(qǐng)參閱Maven POM參考。
Here is a minimal POM file:這是一個(gè)最小的POM文件:
<project xmlns=""
xmlns:xsi=""
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jenkov</groupId>
<artifactId>java-web-crawler</artifactId>
<version>1.0.0</version>
</project>
The modelVersion elementsets what version of the POM model you are using. Use the one matching theMaven version you are using. Version 4.0.0 matches Maven version 2 and 3.
modelVersion元素集您使用的是什么版本的POM模型。使用一個(gè)匹配您正在使用Maven版本。版本4.0.0匹配Maven版本2和3。
The groupId elementis a unique ID for an organization, or a project (an open source project, forinstance). Most often you will use a group ID which is similar to the rootJava package name of the project. For instance, for my Java Web Crawlerproject I may choose the group ID com.jenkov. If the project was anopen source project with many independent contributors, perhaps it would makemore sense to use a group ID related to the project than an a group ID relatedto my company. Thus, com.javawebcrawler could be used.
The group ID does nothave 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 willbe located in the Maven repository under a directory structure matching thegroup ID. Each . is replaced with a directory separator, and each wordthus represents a directory. The group ID com.jenkov would thenbe located in a directory calledMAVEN_REPO/com/jenkov. The MAVEN_REPO partof the directory name will be replaced with the directory path of the Mavenrepository.
組織的groupId元素是一個(gè)惟一的ID,或者一個(gè)項(xiàng)目(一個(gè)開(kāi)源項(xiàng)目,例如)。通常您將使用一組ID,類(lèi)似于根項(xiàng)目的Java包名稱(chēng)。例如,我可以選擇我的Java Web爬蟲(chóng)項(xiàng)目com.jenkov的組ID。如果項(xiàng)目是一個(gè)開(kāi)放源碼項(xiàng)目,許多獨(dú)立的貢獻(xiàn)者,也許更有意義使用組ID相關(guān)的項(xiàng)目與我公司相關(guān)的組ID。因此,com。可以使用javawebcrawler。
組ID并不需要一個(gè)Java包名稱(chēng),并且不需要使用。符號(hào)(點(diǎn)符號(hào))分隔單詞的ID。但是,如果你這樣做,這個(gè)項(xiàng)目將位于Maven存儲(chǔ)庫(kù)組ID相匹配的目錄結(jié)構(gòu)中。。被替換為一個(gè)目錄分隔符,因此每個(gè)單詞代表一個(gè)目錄。com的組ID。jenkov將位于一個(gè)目錄calledMAVEN_REPO /com/jenkov。MAVEN_REPO目錄名稱(chēng)的一部分將會(huì)取代Maven存儲(chǔ)庫(kù)的目錄路徑。
The artifactId elementcontains the name of the project you are building. In the case of my JavaWeb Crawler project, the artifact ID would be java-web-crawler. Theartifact ID is used as name for a subdirectory under the group ID directory inthe Maven repository. The artifact ID is also used as part of the name ofthe JAR file produced when building the project. The output of the buildprocess, the build result that is, is called an artifact in Maven. Mostoften it is a JAR, WAR or EAR file, but it could also be something else.
The versionId elementcontains the version number of the project. If your project has beenreleased in different versions, for instance an open source API, then it isuseful to version the builds. That way users of your project can refer toa specific version of your project. The version number is used as a namefor a subdirectory under the artifact ID directory. The version number isalso used as part of the name of the artifact built.
artifactId元素包含您正在構(gòu)建的項(xiàng)目的名稱(chēng)。在我的Java Web爬蟲(chóng)項(xiàng)目的情況下,將java-web-crawler工件ID。工件ID作為名稱(chēng)的組ID目錄下的子目錄Maven存儲(chǔ)庫(kù)。工件ID也作為名字的一部分構(gòu)建項(xiàng)目時(shí)產(chǎn)生的JAR文件。構(gòu)建過(guò)程的輸出,構(gòu)建結(jié)果,被稱(chēng)為一個(gè)工件在Maven。通常是一個(gè)JAR、WAR或EAR文件,但它也可以是其他的東西。
versionId元素包含項(xiàng)目的版本號(hào)。如果你的項(xiàng)目已經(jīng)發(fā)布在不同的版本中,例如一個(gè)開(kāi)源API,然后是有用的構(gòu)建版本。這樣你的項(xiàng)目的用戶(hù)可以參考您的項(xiàng)目的一個(gè)特定的版本。版本號(hào)是用作工件ID目錄下的子目錄的名稱(chēng)。版本號(hào)也被用作工件的名稱(chēng)的一部分。
Theabove groupId, artifactId and version elements wouldresult in a JAR file being built and put into the local Maven repository at thefollowing 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 hasno external dependencies, then the above minimal POM file is all you need tobuild your project.
If your project does notfollow the standard directory structure, has external dependencies, or needspecial actions during building, you will need to add more elements to the POMfile. These elements are listed in the Maven POM reference (see linkabove).
In general you can specify a lot of things in the POM which gives Maven moredetails about how to build your projects. See the Maven POM reference formore information about what can be specified.
上面的groupId,artifactId和版本元素會(huì)導(dǎo)致在建一個(gè)JAR文件放入本地Maven存儲(chǔ)庫(kù)在以下路徑(目錄和文件名稱(chēng)):
MAVEN_REPO/com/jenkov/java-web-crawler/1.0.0/java-web-crawler-1.0.0.jar
如果您的項(xiàng)目使用Maven的目錄結(jié)構(gòu),和您的項(xiàng)目沒(méi)有外部依賴(lài),那么上述最小POM文件都是需要構(gòu)建您的項(xiàng)目。
如果你的項(xiàng)目不遵循標(biāo)準(zhǔn)的目錄結(jié)構(gòu),外部依賴(lài),或需要特殊的動(dòng)作在建設(shè)期間,您需要添加更多的元素POM文件。這些元素中列出的Maven POM參考(見(jiàn)上面的鏈接)。
一般來(lái)說(shuō)您可以指定一個(gè)Maven POM中很多東西給更多的細(xì)節(jié)關(guān)于如何構(gòu)建您的項(xiàng)目。有關(guān)更多信息,請(qǐng)參見(jiàn)Maven POM引用可以指定。
Super POM
All Maven POM files inheritfrom a super POM. If no super POM is specified, the POM file inherits fromthe base POM. Here is a diagram illustrating that:
超級(jí)POM
所有Maven POM文件從一個(gè)超級(jí)POM繼承。如果沒(méi)有指定超級(jí)POM,POM文件繼承了POM的基地。這是一個(gè)圖說(shuō)明:
SuperPOM and POM inheritance.超級(jí)POM和POM繼承。
You canmake a POM file
[A14] [A15] from another POM file. Thatway you can change the settings across all inheriting POM's via their commonsuper POM. You specify the super POM at the top of a POM file like this:
你可以做一個(gè)POM文件明確繼承另一個(gè)POM文件。這樣你可以更改設(shè)置在所有繼承通過(guò)他們共同的超級(jí)POM POM的。您指定超級(jí)POM POM文件的頂部是這樣的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/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>
Aninheriting POM file may override settings from a super POM. Just specifynew settings in the inheriting POM file.
POM inheritance is also covered in more detail in the Maven POM reference.
一個(gè)繼承POM文件可能覆蓋從超級(jí)POM設(shè)置。只是在繼承POM文件中指定新設(shè)置。
POM的繼承也詳細(xì)介紹Maven POM參考。
Effective POM
With all this POMinheritance it may be hard to know what the total POM file looks like whenMaven executes. The total POM file (result of all inheritance) is calledthe effective POM. You can get Maven to show you the effective POMusing this command:
mvn help:effective-pom
This command will make Maven write out the effective POM to the command lineprompt.
有效的POM
所有這些POM繼承它可能很難知道總Maven POM文件的樣子時(shí)執(zhí)行。總的POM文件(繼承)的結(jié)果稱(chēng)為有效砰的一聲。你可以得到Maven向你們展示有效的POM使用這個(gè)命令:
mvn幫助:effective-pom
這個(gè)命令將Maven POM寫(xiě)出有效命令行提示符。
Maven Settings File
Maven has two settingsfiles. In the settings files you can configure settings for Maven acrossall Maven POM files. For instance, you can configure:
Location of local repository
Active build profile
Etc.
The settings files arecalled settings.xml. The two settings files are located at:
The Maven installation directory: $M2_HOME/conf/settings.xml
The user's homedirectory: ${user.home}/.m2/settings.xml
Both files are optional. If both files are present, the values in the userhome settings file overrides the values in the Maven installation settingsfile.
You can read more about the Maven settings files in the Maven SettingsReference.
Maven設(shè)置文件
Maven文件有兩個(gè)設(shè)置。在設(shè)置文件中您可以配置設(shè)置Maven所有Maven POM文件。例如,您可以配置:
本地存儲(chǔ)庫(kù)的位置
積極構(gòu)建配置文件
等。
被稱(chēng)為settings.xml設(shè)置文件。這兩個(gè)設(shè)置文件位于:
Maven安裝目錄:$ M2_HOME / conf / settings.xml
用戶(hù)的主目錄:$ { user.home } / .m2 / settings.xml
兩個(gè)文件是可選的。如果文件存在,則在用戶(hù)家里的值設(shè)置文件覆蓋Maven安裝設(shè)置文件中的值。
你可以閱讀更多關(guān)于Maven設(shè)置Maven中的文件設(shè)置參考。
Running Maven
When youhave installed Maven and have created a POM file and putthe POM file in the root directory of your project, you can run Maven on yourproject.
Running Maven is done by executing the mvn command from a commandprompt. When executing the mvncommand you pass the name ofa build life cycle, phase or goal to it, which Maven thenexecutes. Here is an example:
mvn install
Thiscommand executes the build phase called install (part ofthe default build life cycle), which builds the project and copiesthe packaged JAR file into the local Maven repository. Actually, thiscommand executes all build phases before install in the build phasesequence, before executing the install build phase.
You can execute multiple build life cycles or phases by passing more than oneargument to the mvncommand. Here is an example:
mvn clean install
This command firstexecutes the clean build life cycle, which removes compiled classesfrom the Maven output directory, and then it executes the install buildphase.
You can also execute a Maven goal (a subpart of a build phase) by passing thebuild phase and goal name concatenated with a : in between, as parameter to theMaven command. Here is an example:
mvn dependency:copy-dependencies
This command executes the copy-dependencies goal ofthe dependency build phase.
運(yùn)行Maven
當(dāng)你安裝了Maven和創(chuàng)造了一個(gè)POM文件,把POM文件在您的項(xiàng)目的根目錄,您可以運(yùn)行Maven項(xiàng)目。
運(yùn)行Maven是通過(guò)執(zhí)行mvn命令從命令提示符。當(dāng)執(zhí)行mvncommand你通過(guò)構(gòu)建生命周期的名字,階段或目標(biāo),Maven然后執(zhí)行。這是一個(gè)例子:
mvn install
這個(gè)命令執(zhí)行構(gòu)建階段稱(chēng)為安裝(默認(rèn)構(gòu)建生命周期的一部分),構(gòu)建項(xiàng)目,打包的JAR文件復(fù)制到本地Maven存儲(chǔ)庫(kù)。實(shí)際上,這個(gè)命令執(zhí)行所有構(gòu)建階段在構(gòu)建階段的順序安裝之前,在執(zhí)行之前安裝構(gòu)建階段。
您可以執(zhí)行多個(gè)構(gòu)建生命周期或階段mvncommand通過(guò)傳遞多個(gè)參數(shù)。這是一個(gè)例子:
mvn全新安裝
這個(gè)命令首先執(zhí)行清潔建造生命周期,從Maven輸出目錄中刪除已編譯類(lèi),然后執(zhí)行安裝構(gòu)建階段。
您還可以執(zhí)行一個(gè)Maven目標(biāo)(一個(gè)構(gòu)建階段的組成部分)通過(guò)構(gòu)建階段和目標(biāo)連接名稱(chēng):,Maven命令的參數(shù)。這是一個(gè)例子:
mvn dependency:copy-dependencies
這個(gè)命令執(zhí)行的copy-dependencies目標(biāo)構(gòu)建階段的依賴(lài)。
Running Maven
When you have installedMaven and have created a POM file and put the POM file in theroot directory of your project, you can run Maven on your project.
Running Maven is done by executing the mvn command from a commandprompt. When executing the mvncommand you pass the name ofa build life cycle, phase or goal to it, which Maven thenexecutes. Here is an example:
Maven Directory Structure
Maven has a standard directorystructure. If you follow that directory structure for your project, you donot need to specify the directories of your source code, test code etc. in yourPOM file.
You can see the full directory layout in the Introduction to the MavenStandard Directory Layout.
Here are the most important directories:
運(yùn)行Maven
當(dāng)你安裝了Maven和創(chuàng)造了一個(gè)POM文件,把POM文件在您的項(xiàng)目的根目錄,您可以運(yùn)行Maven項(xiàng)目。
運(yùn)行Maven是通過(guò)執(zhí)行mvn命令從命令提示符。當(dāng)執(zhí)行mvncommand你通過(guò)構(gòu)建生命周期的名字,階段或目標(biāo),Maven然后執(zhí)行。這是一個(gè)例子:
Maven的目錄結(jié)構(gòu)
Maven標(biāo)準(zhǔn)目錄結(jié)構(gòu)。如果你遵循你的項(xiàng)目目錄結(jié)構(gòu),您不需要指定源代碼的目錄,POM文件測(cè)試代碼等。
你可以看到完整的目錄布局在Maven標(biāo)準(zhǔn)目錄布局。
這里有最重要的目錄:
- src - main - java - resources - webapp - test - java - resources- target
The src directoryis the root directory of your source code and test code. The main directoryis the root directory for source code related to the application itself (nottest code). The test directory contains the test sourcecode. The java directoriesunder main and test contains the Java code for theapplication itself (under main) and the Java code for the tests (undertest).
The resources directory contains other resources needed by yourproject. This could be property files used for internationalization of anapplication, or something else.
The webapp directorycontains your Java web application, if your project is a webapplication. The webappdirectory will then be the root directory ofthe web application. Thus the webapp directory containsthe WEB-INF directory etc.
The target directory is created by Maven. It contains all thecompiled classes, JAR files etc. produced by Maven. When executing the clean buildphase, it is the target directory which is cleaned.
src目錄是根目錄的源代碼和測(cè)試代碼。源代碼的主要目錄是根目錄與應(yīng)用程序本身(而不是測(cè)試代碼)。測(cè)試目錄包含測(cè)試源代碼。java目錄下主要包含java代碼和測(cè)試應(yīng)用程序本身(主要)和java代碼的測(cè)試(測(cè)試)。
資源目錄包含項(xiàng)目所需的其他資源。這可能是屬性文件用于應(yīng)用程序的國(guó)際化,或者其他東西。
webapp目錄包含您的Java web應(yīng)用程序,如果你的項(xiàng)目是一個(gè)web應(yīng)用程序。webappdirectory將web應(yīng)用程序的根目錄。因此,webapp目錄包含web - inf目錄等。
目標(biāo)目錄是由Maven。它包含所有已編譯類(lèi)的JAR文件等由Maven。當(dāng)執(zhí)行清潔的構(gòu)建階段,清洗的目標(biāo)目錄。
Project Dependencies
Unless your project is small,your project may need external Java APIs or frameworks which are packaged intheir own JAR files. These JAR files are needed on the classpath when youcompile your project code.
Keeping your projectup-to-date with the correct versions of these external JAR files can be acomprehensive task. Each external JAR may again also need other externalJAR files etc. Downloading all these external dependencies (JAR files)recursively and making sure that the right versions are downloaded iscumbersome. Especially when your project grows big, and you get more andmore external dependencies.
Luckily, Maven has built-independency management. You specify in the POM file what external librariesyour project depends on, and which version, and then Maven downloads them foryou and puts them in your local Maven repository. If any of these externallibraries need other libraries, then these other libraries are also downloadedinto your local Maven repository.
You specify your projectdependencies inside the dependencies element in the POMfile. Here is an example:
項(xiàng)目依賴(lài)項(xiàng)
除非你的項(xiàng)目很小,您的項(xiàng)目可能需要外部Java api或框架打包的JAR文件。這些JAR文件需要在類(lèi)路徑中當(dāng)你編譯您的項(xiàng)目的代碼。
保持您的項(xiàng)目與這些外部JAR文件的正確版本最新的可以是一個(gè)綜合的任務(wù)。每個(gè)外部JAR可能還需要再下載其他外部JAR文件等等。所有這些外部依賴(lài)(JAR文件)遞歸和確保正確的版本下載很麻煩。特別是當(dāng)你的項(xiàng)目越來(lái)越大,得到越來(lái)越多的外部依賴(lài)。
幸運(yùn)的是,Maven內(nèi)置的依賴(lài)關(guān)系管理。您指定的POM文件外部庫(kù)項(xiàng)目取決于什么,哪個(gè)版本,然后Maven下載給你,并在本地Maven資源庫(kù)中。如果這些外部庫(kù)需要其他的庫(kù),那么這些其他圖書(shū)館也下載到本地Maven資源庫(kù)中。
您指定您的項(xiàng)目依賴(lài)內(nèi)部元素POM文件的依賴(lài)關(guān)系。這是一個(gè)例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/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 thedependencies element in bold. Inside it aretwo dependency elements. Each dependency elementdescribes an external dependency.
Each dependency is described byits groupId, artifactId and version. You may rememberthat this is also how you identified your own project in the beginning of thePOM file. The example above needstheorg.jsoup group's jsoup artifact in version 1.7.1, andthe junit group's junit artifact in version 4.8.1.
When this POM file is executedby Maven, the two dependencies will be downloaded from a central Mavenrepository and put into your local Maven repository. If the dependenciesare already found in your local repository, Maven will not downloadthem. Only if the dependencies are missing will they be downloaded intoyour local repository.
Sometimes a givendependency is not available in the central Maven repository. You can thendownload the dependency yourself and put it into your local Mavenrepository. Remember to put it into a subdirectory structure matchingthe 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 dependenciesdownloaded 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
請(qǐng)注意元素大膽的依賴(lài)關(guān)系。里面有兩個(gè)元素的依賴(lài)。每個(gè)依賴(lài)元素描述外部依賴(lài)。
每個(gè)依賴(lài)描述groupId,artifactId和版本。你可能還記得,這是你如何確定自己的項(xiàng)目在POM文件的開(kāi)始。上面的示例需要theorg。jsoup集團(tuán)jsoup工件在版本1.7.1上,junit集團(tuán)在4.8.1版本的junit工件。
當(dāng)這個(gè)執(zhí)行的Maven POM文件,這兩個(gè)依賴(lài)項(xiàng)將從一個(gè)中央Maven存儲(chǔ)庫(kù)下載到本地Maven資源庫(kù)中。如果依賴(lài)已經(jīng)發(fā)現(xiàn)在你的本地存儲(chǔ)庫(kù),Maven不會(huì)下載它們。只有缺少的依賴(lài)關(guān)系會(huì)被下載到你的本地存儲(chǔ)庫(kù)。
有時(shí)一個(gè)給定的依賴(lài)不是中央Maven存儲(chǔ)庫(kù)中可用。然后您可以依賴(lài)自己,把它下載到本地Maven資源庫(kù)中。記得把它放到一個(gè)子目錄結(jié)構(gòu)匹配的groupId,artifactId和版本。替換所有點(diǎn)(.)/和單獨(dú)的groupId / artifactId和版本。然后你的子目錄結(jié)構(gòu)。
上面的兩個(gè)附件下載的示例將放在以下目錄:
MAVEN_REPOSITORY_ROOT / junit junit / 4.8.1
MAVEN_REPOSITORY_ROOT /org/jsoup/jsoup/1.7.1
External Dependencies
Anexternal dependency in Maven is a dependency (JAR file) which is not located ina Maven repository (neiterh local, central or remote repository). It maybe located somewhere on your local hard disk, for instance inthe lib directory of a webapp, or somewhere else. The word"external" thus means external to the Maven repository system - notjust external to the project. Most dependencies are external to theproject, but few are external to the repository system (not located in arepository).
You configure an external dependency like this:
外部依賴(lài)關(guān)系
外部依賴(lài)關(guān)系在Maven的依賴(lài)項(xiàng)(JAR文件)不是位于Maven存儲(chǔ)庫(kù)(neiterh地方、中央或遠(yuǎn)程存儲(chǔ)庫(kù))。它可能位于本地硬盤(pán)上的某個(gè)地方,例如webapp的lib目錄,或其他地方?!巴獠俊币辉~因此意味著外部到Maven存儲(chǔ)庫(kù)系統(tǒng)——不僅僅是外部的項(xiàng)目。大多數(shù)依賴(lài)外部的項(xiàng)目,但很少有外部存儲(chǔ)庫(kù)系統(tǒng)(而不是位于存儲(chǔ)庫(kù))。
你配置一個(gè)外部依賴(lài)關(guān)系是這樣的:
<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 areboth set to the name of the dependency. The name of the API used, thatis. The scope element value is setto system. The systemPath element is set to point to thelocation of the JAR file containing thedependency. The ${basedir} points to the directory where the POMis located. The rest of the path is relative from that directory.
groupId和artifactId都將依賴(lài)項(xiàng)的名稱(chēng)。API使用的名稱(chēng)。元素值設(shè)置為系統(tǒng)范圍。systemPath元素設(shè)置為指向包含依賴(lài)JAR文件的位置。$ { basedir }指向POM所在的目錄。其余的從該目錄路徑是相對(duì)的。
Snapshot Dependencies
Snapshotdependencies are dependencies (JAR files) which are under development. Insteadof constantly updating the version numbers to get the latest version, you candepend on a snapshot version of the project. Snapshot versions are alwaysdownloaded into your local repository for every build, even if a matchingsnapshot version is already located in your local repository. Alwaysdownloading the snapshot dependencies assures that you always have the latestversion in your local repository, for every build.
You can tell Maven thatyour project is a snapshot version simply by appending -SNAPSHOT tothe version number in the beginning of the POM (where you also setthe groupId and artifactId). Here is a versionelementexample:
快照的依賴(lài)性.
快照依賴(lài)關(guān)系正在開(kāi)發(fā)的依賴(lài)項(xiàng)(JAR文件)。而不是不斷更新最新版本的版本號(hào),你可以依靠一個(gè)快照版本的項(xiàng)目。快照版本總是為每一個(gè)構(gòu)建下載到你的本地存儲(chǔ)庫(kù),即使一個(gè)匹配的快照版本已經(jīng)位于您的本地存儲(chǔ)庫(kù)中。總是下載依賴(lài)快照確保你總是有最新版本在你的本地存儲(chǔ)庫(kù),為每一個(gè)構(gòu)建。
你可以告訴Maven項(xiàng)目是一個(gè)快照版本只需添加版本號(hào)的快照POM的開(kāi)始(你也設(shè)置groupId和artifactId)。這是一個(gè)versionelement的例子:
<version>1.0-SNAPSHOT</version>
Notice the -SNAPSHOT appended to the version number.
Depending on a snapshotversion is also done by appending the -SNAPSHOT after the versionnumber when configuring dependencies. Here is an example:
注意到快照添加到版本號(hào)。
根據(jù)快照版本也由附加后的快照版本號(hào)在配置依賴(lài)關(guān)系。這是一個(gè)例子:
<dependency> <groupId>com.jenkov</groupId> <artifactId>java-web-crawler</artifactId> <version>1.0-SNAPSHOT</version></dependency>
The -SNAPSHOT appended to the version numbertells Maven that this is a snapshot version.
You can configure how often Maven shall download snapshot dependencies inthe Maven Settings File.
快照添加到版本號(hào)告訴Maven版本,這是一個(gè)快照。
您可以配置Maven多久下載快照在Maven依賴(lài)設(shè)置文件。
Maven Repositories
Maven repositories aredirectories of packaged JAR files with extra meta data. The meta data arePOM files describing the projects each packaged JAR file belongs to, includingwhat external dependencies each packaged JAR has. It is this meta datathat enables Maven to download dependencies of your dependencies recursively,until the whole tree of dependencies is download and put into your localrepository.
Maven repositories are coveredin more detail in the Maven Introduction to Repositories, but here is aquick overview.
Maven has three types of repository:
Local repository
Central repository
Remote repository
Maven searches theserepositories for dependencies in the above sequence. First in the localrepository, then in the central repository, and third in remote repositories ifspecified in the POM.
Maven存儲(chǔ)庫(kù)
Maven存儲(chǔ)庫(kù)目錄打包的JAR文件的額外的元數(shù)據(jù)。元數(shù)據(jù)是描述項(xiàng)目的POM文件每個(gè)屬于打包的JAR文件,包括外部依賴(lài)每個(gè)打包的JAR。正是這種元數(shù)據(jù),使Maven下載依賴(lài)你的遞歸地依賴(lài),直到整棵樹(shù)的依賴(lài)關(guān)系是下載并放入您的本地存儲(chǔ)庫(kù)中。
Maven存儲(chǔ)庫(kù)會(huì)詳細(xì)介紹Maven存儲(chǔ)庫(kù)簡(jiǎn)介,但這里是一個(gè)快速概述。
Maven有三種類(lèi)型的存儲(chǔ)庫(kù):
本地存儲(chǔ)庫(kù)
中央存儲(chǔ)庫(kù)
遠(yuǎn)程存儲(chǔ)庫(kù)
Maven搜索這些存儲(chǔ)庫(kù)的依賴(lài)性在上面的序列。首先在本地存儲(chǔ)庫(kù),然后在中央存儲(chǔ)庫(kù),和第三如果POM中指定遠(yuǎn)程存儲(chǔ)庫(kù)。
Here is a diagram illustrating the three repository types andtheir location:
這是一個(gè)圖說(shuō)明三個(gè)庫(kù)類(lèi)型和位置:
MavenRepository Types and Location. Maven存儲(chǔ)庫(kù)類(lèi)型和位置。
Local Repository
A local repository is adirectory on the developer's computer. This repository will contain allthe dependencies Maven downloads. The same Maven repository is typicallyused for several different projects. Thus Maven only needs to download thedependencies once, even if multiple projects depends on them (e.g. Junit).
Your own projects can also bebuilt and installed in your local repository, using the mvninstall command. That way your other projects can use the packagedJAR files of your own projects as external dependencies by specifying them asexternal dependencies inside their Maven POM files.
By default Maven puts your local repository inside your user home directory onyour local computer. However, you can change the location of the localrepository by setting the directory inside your Maven settings file. YourMaven settings file is also located in your user-home/.m2 directoryand is calledsettings.xml. Here is how you specify another location foryour local repository:
本地存儲(chǔ)庫(kù)
本地存儲(chǔ)庫(kù)是一個(gè)開(kāi)發(fā)人員的計(jì)算機(jī)上的目錄。這個(gè)存儲(chǔ)庫(kù)將包含所有下載Maven的依賴(lài)關(guān)系。相同的Maven存儲(chǔ)庫(kù)通常用于不同的項(xiàng)目。因此Maven只需要下載的依賴(lài)關(guān)系,即使多個(gè)項(xiàng)目(例如Junit)取決于他們。
自己的項(xiàng)目也可以建造和安裝在你的本地存儲(chǔ)庫(kù),使用mvn install命令。這樣你的其他項(xiàng)目可以使用JAR文件打包自己的項(xiàng)目外部依賴(lài)通過(guò)指定外部依賴(lài)關(guān)系在他們的Maven POM文件。
默認(rèn)Maven將您的本地存儲(chǔ)庫(kù)在本地電腦上用戶(hù)的主目錄。但是,你可以改變本地存儲(chǔ)庫(kù)的位置通過(guò)設(shè)置目錄在您的Maven設(shè)置文件。Maven設(shè)置文件也位于你的戶(hù)主/。m2目錄和called settings.xml。這是你如何為你的本地存儲(chǔ)庫(kù):指定另一個(gè)位置.
<settings> <localRepository> d:\data\java\products\maven\repository </localRepository></settings>
Central Repository
The central Mavenrepository is a repository provided by the Maven community. By defaultMaven looks in this central repository for any dependencies needed but notfound in your local repository. Maven then downloads these dependenciesinto your local repository. You need no special configuration to accessthe central repository.
Remote Repository
A remote repository is arepository on a web server from which Maven can download dependencies, justlike the central repository. A remote repository can be located anywhereon the internet, or inside a local network.
A remote repository is oftenused for hosting projects internal to your organization, which are shared bymultiple projects. For instance, a common security project might be usedacross multiple internal projects. This security project should not beaccessible to the outside world, and should thus not be hosted in the public,central Maven repository. Instead it can be hosted in an internal remoterepository.
中央存儲(chǔ)庫(kù)
中央Maven存儲(chǔ)庫(kù)是一個(gè)Maven存儲(chǔ)庫(kù)提供的社區(qū)。默認(rèn)Maven中央存儲(chǔ)庫(kù)中查找所需依賴(lài)關(guān)系,但沒(méi)有發(fā)現(xiàn)在你的本地存儲(chǔ)庫(kù)。Maven然后下載這些依賴(lài)關(guān)系到你的本地存儲(chǔ)庫(kù)。你不需要任何特殊配置訪(fǎng)問(wèn)中央存儲(chǔ)庫(kù)。
遠(yuǎn)程存儲(chǔ)庫(kù)
一個(gè)遠(yuǎn)程存儲(chǔ)庫(kù)是一個(gè)Maven存儲(chǔ)庫(kù)web服務(wù)器上可以下載依賴(lài),就像中央存儲(chǔ)庫(kù)。一個(gè)遠(yuǎn)程存儲(chǔ)庫(kù)可以位于任何地方在互聯(lián)網(wǎng)上,或者在一個(gè)本地網(wǎng)絡(luò)。
一個(gè)遠(yuǎn)程存儲(chǔ)庫(kù)通常用于舉辦項(xiàng)目組織的內(nèi)部,是由多個(gè)項(xiàng)目共享。例如,一個(gè)共同的安全項(xiàng)目可能使用多個(gè)內(nèi)部項(xiàng)目。這個(gè)安全項(xiàng)目不應(yīng)該訪(fǎng)問(wèn)的外部世界,并因此不應(yīng)該駐留在公眾,中央Maven存儲(chǔ)庫(kù)。相反,它可以駐留在一個(gè)內(nèi)部的遠(yuǎn)程存儲(chǔ)庫(kù)。
Dependencies found in a remoterepository are also downloaded and put into your local repository by Maven.
You can configure a remoterepository in the POM file. Put the following XML elements right afterthe<dependencies> element:
您可以配置一個(gè)遠(yuǎn)程存儲(chǔ)庫(kù)的POM文件。把下面的XML元素之后<dependencies> element:
<repositories> <repository> <id>jenkov.code</id> <url>http://maven.jenkov.com/maven2/lib</url> </repository></repositories>
Maven Build Life Cycles, Phases and Goals
When Maven builds a softwareproject it follows a build life cycle. The build life cycle is dividedinto build phases, and the build phases are divided into buildgoals. Maven build life cycles, build phases and goals are described inmore detail in the Maven Introduction to Build Phases, but here I willgive you a quick overview.
Build Life Cycles
Maven has 3 built-in build life cycles. These are:
default
clean
site
Each of these build lifecycles takes care of a different aspect of building a softwareproject. Thus, each of these build life cycles are executed independentlyof each other. You can get Maven to execute more than one build lifecycle, but they will be executed in sequence, separately from each other, as ifyou had executed two separate Maven commands.
The default lifecycle handles everything related to compiling and packaging yourproject. The clean life cycle handles everything related toremoving temporary files from the output directory, including generated sourcefiles, compiled classes, previous JAR files etc. The site life cyclehandles everything related to generating documentation for yourproject. In fact, site can generate a complete website withdocumentation for your project.
Maven構(gòu)建生命周期、階段和目標(biāo)
Maven構(gòu)建軟件項(xiàng)目的時(shí)候是一個(gè)構(gòu)建生命周期。構(gòu)建生命周期分為構(gòu)建階段,和構(gòu)建階段分為構(gòu)建目標(biāo)。Maven構(gòu)建生命周期、構(gòu)建階段和目標(biāo)更詳細(xì)地描述了Maven介紹構(gòu)建階段,但在這里我將給你一個(gè)快速概述。
建立生命周期
Maven 3內(nèi)置構(gòu)建生命周期。這些都是:
默認(rèn)的
清潔
網(wǎng)站
每一個(gè)構(gòu)建生命周期負(fù)責(zé)建立一個(gè)軟件項(xiàng)目的不同方面。因此,每一個(gè)構(gòu)建生命周期是彼此獨(dú)立地執(zhí)行。你可以得到Maven執(zhí)行多個(gè)構(gòu)建生命周期,但他們將按順序執(zhí)行,彼此分開(kāi),因?yàn)槿绻阌袃蓚€(gè)單獨(dú)的Maven命令執(zhí)行。
缺省生命周期處理一切有關(guān)編譯和打包您的項(xiàng)目。清潔處理一切有關(guān)生命周期從輸出目錄刪除臨時(shí)文件,包括生成的源文件,編譯類(lèi),先前的JAR文件等。該網(wǎng)站生命周期處理一切相關(guān)的為您的項(xiàng)目生成文檔。事實(shí)上,網(wǎng)站可以生成一個(gè)完整的網(wǎng)站為您的項(xiàng)目文檔。
Build Phases
Each build life cycle isdivided into a sequence of build phases, and the build phases are againsubdivided into goals. Thus, the total build process is a sequence ofbuild life cycle(s), build phases and goals.
You can execute either a wholebuild life cycle like clean or site,a build phase like install which is part of the default buildlife cycle, or a build goal like dependency:copy-dependencies. Note:You cannot execute the default lifecycle directly. You have to specify a build phase or goal inside the default lifecycle.
When you execute a buildphase, all build phases before that build phase in this standard phase sequenceare executed. Thus, executing the install buildphase really means executing all build phases before the install phase,and then execute the install phaseafter that.
The default lifecycle is of most interest since that is what builds the code. Since youcannot execute the default lifecycle directly, you need to execute a build phase or goal from the default lifecycle. The default lifecycle has an extensive sequence of build phases and goals, ,so I will notdescribe them all here. The most commonly used build phases are:
構(gòu)建階段
每個(gè)構(gòu)建生命周期分為一系列的構(gòu)建階段,和構(gòu)建階段又分為目標(biāo)。因此,整個(gè)構(gòu)建過(guò)程是構(gòu)建生命周期的序列,構(gòu)建階段和目標(biāo)。
您可以執(zhí)行一個(gè)整體構(gòu)建生命周期清潔或網(wǎng)站,一個(gè)構(gòu)建階段像安裝默認(rèn)構(gòu)建生命周期的一部分,或構(gòu)建目標(biāo)像依賴(lài):copy-dependencies。注意:你不能直接執(zhí)行thedefault生命周期。您必須指定一個(gè)構(gòu)建階段或目標(biāo)內(nèi)部默認(rèn)的生命周期。
當(dāng)您執(zhí)行一個(gè)構(gòu)建階段,構(gòu)建階段之前,在執(zhí)行本標(biāo)準(zhǔn)相序建立階段。因此,執(zhí)行安裝構(gòu)建階段的真正含義之前執(zhí)行所有構(gòu)建階段安裝階段,然后執(zhí)行安裝階段。
缺省生命周期是最感興趣的,因?yàn)檫@就是構(gòu)建代碼。既然你不能直接執(zhí)行the default生命周期中,您需要執(zhí)行 一個(gè)構(gòu)建階段或目標(biāo)從默認(rèn)的生命周期。defaultlife周期有一個(gè)廣泛的序列構(gòu)建階段和目標(biāo),,所以我不會(huì)描述他們都在這里。最常用的構(gòu)建階段是:
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 tothe mvn command. Here is an example:
你執(zhí)行一個(gè)構(gòu)建階段通過(guò)mvn命令的名稱(chēng)。這是一個(gè)例子:
mvn package
This example executesthe package build phase, and thus also all build phases before it inMaven's predefined build phase sequence.
If the standard Maven build phases and goals are not enough to build yourproject, you can create Maven plugins to add the extra buildfunctionality you need.
Build Goals
Build goals are the fineststeps in the Maven build process. A goal can be bound to one or more buildphases, or to none at all. If a goal is not bound to any build phase, youcan only execute it by passing the goals name tothe 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.
這個(gè)例子執(zhí)行包構(gòu)建階段,因此也在Maven構(gòu)建階段之前的預(yù)定義的序列構(gòu)建階段。
如果標(biāo)準(zhǔn)Maven構(gòu)建階段和目標(biāo)并不足以構(gòu)建您的項(xiàng)目,您可以創(chuàng)建Maven插件添加額外的構(gòu)建功能需要。
建立目標(biāo)
建立目標(biāo)是最好的Maven構(gòu)建過(guò)程中的步驟。一個(gè)目標(biāo)可以綁定到一個(gè)或多個(gè)構(gòu)建階段,或根本沒(méi)有。如果一個(gè)目標(biāo)是沒(méi)有綁定到構(gòu)建階段,你只能通過(guò)執(zhí)行它的目標(biāo)名稱(chēng)mvn命令。如果目標(biāo)是綁定到多個(gè)構(gòu)建階段,這一目標(biāo)將在每次執(zhí)行構(gòu)建階段的必然。
Maven Build Profiles
Maven build profilesenable you to build your project using different configurations. Insteadof creating two separate POM files, you can just specify a profile with thedifferent build configuration, and build your project with this build profilewhen needed.
You can read the full storyabout build profiles in the Maven POM reference under Profiles. HereI will give you a quick overview though.
Maven構(gòu)建配置文件
Maven構(gòu)建配置文件使您能夠使用不同的配置構(gòu)建您的項(xiàng)目。而不是創(chuàng)建兩個(gè)單獨(dú)的POM文件,你可以指定一個(gè)配置文件與不同的構(gòu)建配置,并建立項(xiàng)目的構(gòu)建配置文件。
您可以閱讀完整的故事構(gòu)建配置文件下的Maven POM參考資料。在這里我將給你一個(gè)快速概述。
Maven build profiles arespecified inside the POM file, inside the profiles element. Eachbuild profile is nested inside a profile element. Here is anexample:
Maven構(gòu)建配置文件指定在POM文件,內(nèi)部配置文件元素。每個(gè)構(gòu)建文件元素嵌套在一個(gè)概要文件。這是一個(gè)例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/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 whatchanges should be made to the POM file when executing under that build profile. Thiscould be changing the applications configuration file to use etc. The elementsinside theprofile element will override the values of the elements withthe same name further up in the POM.
Insidethe profile element you can see a activation element. Thiselement describes the condition that triggers this build profile to beused. One way to choose what profile is being executed is in thesettings.xml file. There you can set the active profile. Anotherway is to add -P profile-name to the Maven command line. See theprofile documentation for more information.
構(gòu)建配置文件描述了變化時(shí)應(yīng)該對(duì)POM文件執(zhí)行構(gòu)建配置文件。這可能是改變應(yīng)用程序配置文件使用等。theprofile元素內(nèi)的元素將會(huì)覆蓋具有相同名稱(chēng)的元素的值進(jìn)一步砰的一聲。
在配置文件元素可以看到激活元素。這個(gè)元素描述了使用條件,觸發(fā)這個(gè)構(gòu)建配置文件。選擇概要文件被執(zhí)行的一個(gè)方法是在settings.xml文件中。你可以設(shè)置當(dāng)前文件。另一種方法是- p配置文件名添加到Maven命令行。看到這個(gè)概要文件文檔的更多信息。
Maven Plugins
Maven plugins enable you toadd your own actions to the build process. You do so by creating a simpleJava class that extends a special Maven class, and then create a POM for theproject. The plugin should be located in its own project.
To keep this tutorial short, Iwill refer to the Maven Plugin Developers Centre for more informationabout developing plugins.
Maven插件
Maven插件使您可以添加自己的行動(dòng)來(lái)構(gòu)建過(guò)程。你這樣做,通過(guò)創(chuàng)建一個(gè)簡(jiǎn)單的Java類(lèi),它擴(kuò)展了一個(gè)特殊的Maven類(lèi),然后創(chuàng)建一個(gè)POM的項(xiàng)目。這個(gè)插件應(yīng)該位于自己的項(xiàng)目。
保持本教程,我將參考Maven插件開(kāi)發(fā)者中心開(kāi)發(fā)插件的更多信息。
目標(biāo)、用途、意圖
教程
因此
重點(diǎn)
概念
查找
聲稱(chēng)
哲學(xué)
自動(dòng)構(gòu)建
通常
優(yōu)點(diǎn)
過(guò)程
使減少到最小
明確
繼承