maven搭建與使用(一)


下載安裝配置maven

首先到官網下載maven 目前最新版本為3.6.1

 

下載maven

下載好之后進行解壓安裝

 

maven解壓后路徑

配置環境變量

 

配置maven_home

 

配置path添加maven_home下的bin

 

cmd 運行mvn -version

 

查看mvn版本號

配置文件settting.xml的簡單配置
配置創建項目時默認的jdk版本,在<profiles></profiles>標簽中添加<profile></profile>,這樣每次update項目的時候就會變為默認的jdk版本,這里是默認全局設置的編譯版本

<profile>

<id>jdk-1.8</id>

<activation>

<activeByDefault>true</activeByDefault>

<jdk>1.8</jdk>

</activation>

<properties>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

</properties>

</profile>

配置私有倉庫的位置(maven所有的本地jar包都會放在私有倉庫里面),其默認位置是當前用戶目錄下.m2文件夾中。通過修改下面的路徑可以修改本地倉庫的位置。

<localRepository>E:\maven-repository</localRepository>

配置中央倉庫,使用阿里雲倉庫作為中央倉庫的鏡像,因為國外的倉庫訪問非常慢
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

setting.xml

<?xml version="1.0" encoding="UTF-8"?>

 

<!--

Licensed to the Apache Software Foundation (ASF) under one

or more contributor license agreements. See the NOTICE file

distributed with this work for additional information

regarding copyright ownership. The ASF licenses this file

to you under the Apache License, Version 2.0 (the

"License"); you may not use this file except in compliance

with the License. You may obtain a copy of the License at

 

http://www.apache.org/licenses/LICENSE-2.0

 

Unless required by applicable law or agreed to in writing,

software distributed under the License is distributed on an

"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

KIND, either express or implied. See the License for the

specific language governing permissions and limitations

under the License.

-->

 

<!--

| This is the configuration file for Maven. It can be specified at two levels:

|

| 1. User Level. This settings.xml file provides configuration for a single user,

| and is normally provided in ${user.home}/.m2/settings.xml.

|

| NOTE: This location can be overridden with the CLI option:

|

| -s /path/to/user/settings.xml

|

| 2. Global Level. This settings.xml file provides configuration for all Maven

| users on a machine (assuming they're all using the same Maven

| installation). It's normally provided in

| ${maven.home}/conf/settings.xml.

|

| NOTE: This location can be overridden with the CLI option:

|

| -gs /path/to/global/settings.xml

|

| The sections in this sample file are intended to give you a running start at

| getting the most out of your Maven installation. Where appropriate, the default

| values (values used when the setting is not specified) are provided.

|

|-->

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<!-- localRepository

| The path to the local repository maven will use to store artifacts.

|

| Default: ${user.home}/.m2/repository

 

-->

<localRepository>E:\maven-repository</localRepository>

<!-- interactiveMode

| This will determine whether maven prompts you when it needs input. If set to false,

| maven will use a sensible default value, perhaps based on some other setting, for

| the parameter in question.

|

| Default: true

<interactiveMode>true</interactiveMode>

-->

 

<!-- offline

| Determines whether maven should attempt to connect to the network when executing a build.

| This will have an effect on artifact downloads, artifact deployment, and others.

|

| Default: false

<offline>false</offline>

-->

 

<!-- pluginGroups

| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.

| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers

| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.

|-->

<pluginGroups>

<!-- pluginGroup

| Specifies a further group identifier to use for plugin lookup.

<pluginGroup>com.your.plugins</pluginGroup>

-->

</pluginGroups>

 

<!-- proxies

| This is a list of proxies which can be used on this machine to connect to the network.

| Unless otherwise specified (by system property or command-line switch), the first proxy

| specification in this list marked as active will be used.

|-->

<proxies>

<!-- proxy

| Specification for one proxy, to be used in connecting to the network.

|

<proxy>

<id>optional</id>

<active>true</active>

<protocol>http</protocol>

<username>proxyuser</username>

<password>proxypass</password>

<host>proxy.host.net</host>

<port>80</port>

<nonProxyHosts>local.net|some.host.com</nonProxyHosts>

</proxy>

-->

</proxies>

 

<!-- servers

| This is a list of authentication profiles, keyed by the server-id used within the system.

| Authentication profiles can be used whenever maven must make a connection to a remote server.

|-->

<servers>

<!-- server

| Specifies the authentication information to use when connecting to a particular server, identified by

| a unique name within the system (referred to by the 'id' attribute below).

|

| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are

| used together.

|

<server>

<id>deploymentRepo</id>

<username>repouser</username>

<password>repopwd</password>

</server>

-->

 

<!-- Another sample, using keys to authenticate.

<server>

<id>siteServer</id>

<privateKey>/path/to/private/key</privateKey>

<passphrase>optional; leave empty if not used.</passphrase>

</server>

-->

</servers>

 

<!-- mirrors

| This is a list of mirrors to be used in downloading artifacts from remote repositories.

|

| It works like this: a POM may declare a repository to use in resolving certain artifacts.

| However, this repository may have problems with heavy traffic at times, so people have mirrored

| it to several places.

|

| That repository definition will have a unique id, so we can create a mirror reference for that

| repository, to be used as an alternate download site. The mirror site will be the preferred

| server for that repository.

|-->

<mirrors>

<!-- mirror

| Specifies a repository mirror site to use instead of a given repository. The repository that

| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used

| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.

|

<mirror>

<id>mirrorId</id>

<mirrorOf>repositoryId</mirrorOf>

<name>Human Readable Name for this Mirror.</name>

<url>http://my.repository.com/repo/path</url>

</mirror>

-->

<mirror>

<id>nexus-aliyun</id>

<mirrorOf>central</mirrorOf>

<name>Nexus aliyun</name>

<url>http://maven.aliyun.com/nexus/content/groups/public</url>

</mirror>

</mirrors>

 

<!-- profiles

| This is a list of profiles which can be activated in a variety of ways, and which can modify

| the build process. Profiles provided in the settings.xml are intended to provide local machine-

| specific paths and repository locations which allow the build to work in the local environment.

|

| For example, if you have an integration testing plugin - like cactus - that needs to know where

| your Tomcat instance is installed, you can provide a variable here such that the variable is

| dereferenced during the build process to configure the cactus plugin.

|

| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles

| section of this document (settings.xml) - will be discussed later. Another way essentially

| relies on the detection of a system property, either matching a particular value for the property,

| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a

| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.

| Finally, the list of active profiles can be specified directly from the command line.

|

| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact

| repositories, plugin repositories, and free-form properties to be used as configuration

| variables for plugins in the POM.

|

|-->

<profiles>

<profile>

<id>downloadSources</id>

<properties>

<downloadSources>true</downloadSources>

<downloadJavadocs>true</downloadJavadocs>

</properties>

</profile>

<!-- profile

| Specifies a set of introductions to the build process, to be activated using one or more of the

| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>

| or the command line, profiles have to have an ID that is unique.

|

| An encouraged best practice for profile identification is to use a consistent naming convention

| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.

| This will make it more intuitive to understand what the set of introduced profiles is attempting

| to accomplish, particularly when you only have a list of profile id's for debug.

|

| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.

<profile>

<id>jdk-1.4</id>

 

<activation>

<jdk>1.4</jdk>

</activation>

 

<repositories>

<repository>

<id>jdk14</id>

<name>Repository for JDK 1.4 builds</name>

<url>http://www.myhost.com/maven/jdk14</url>

<layout>default</layout>

<snapshotPolicy>always</snapshotPolicy>

</repository>

</repositories>

</profile>

-->

<profile>

<id>jdk-1.8</id>

<activation>

<activeByDefault>true</activeByDefault>

<jdk>1.8</jdk>

</activation>

<properties>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>

</properties>

</profile>

 

 

 

<!--

| Here is another profile, activated by the system property 'target-env' with a value of 'dev',

| which provides a specific path to the Tomcat instance. To use this, your plugin configuration

| might hypothetically look like:

|

| ...

| <plugin>

| <groupId>org.myco.myplugins</groupId>

| <artifactId>myplugin</artifactId>

|

| <configuration>

| <tomcatLocation>${tomcatPath}</tomcatLocation>

| </configuration>

| </plugin>

| ...

|

| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to

| anything, you could just leave off the <value/> inside the activation-property.

|

<profile>

<id>env-dev</id>

 

<activation>

<property>

<name>target-env</name>

<value>dev</value>

</property>

</activation>

 

<properties>

<tomcatPath>/path/to/tomcat/instance</tomcatPath>

</properties>

</profile>

-->

</profiles>

<activeProfiles>

<activeProfile>downloadSources</activeProfile>

</activeProfiles>

<!-- activeProfiles

| List of profiles that are active for all builds.

|

<activeProfiles>

<activeProfile>alwaysActiveProfile</activeProfile>

<activeProfile>anotherAlwaysActiveProfile</activeProfile>

</activeProfiles>

-->

</settings>

eclipseintellij idea 配置maven

同步滾動:

maven下載與安裝

  1. 前往https://maven.apache.org/download.cgi下載最新版的Maven程序:

image

  1. 將文件解壓到E:\worksoftInstall\maveninstall\apache-maven-3.3.9目錄下:

     

    image.png

  2. 新建環境變量MAVEN_HOME,賦值:E:\worksoftInstall\maveninstall\apache-maven-3.3.9

image.png

  1. 編輯環境變量Path,追加%MAVEN_HOME%\bin;

image

  1. 至此,maven已經完成了安裝,我們可以通過DOS命令檢查一下我們是否安裝成功:mvn -v

image.png

三、配置Maven本地倉庫

  1. E 目錄下新建maven-repository文件夾,該目錄用作maven的本地庫。
  2. 打開E:\worksoftInstall\maveninstall\apache-maven-3.3.9\conf\settings.xml文件,查找下面這行代碼:

<localRepository>/path/to/local/repo</localRepository> 

localRepository節點默認是被注釋掉的,需要把它移到注釋之外,然后將localRepository節點的值改為我們在3.1中創建的目錄E:\maven-repository

  1. localRepository節點用於配置本地倉庫,本地倉庫其實起到了一個緩存的作用,它的默認地址是 C:\Users\用戶名.m2

當我們從maven中獲取jar包的時候,maven首先會在本地倉庫中查找,如果本地倉庫有則返回;如果沒有則從遠程倉庫中獲取包,並在本地庫中保存。

此外,我們在maven項目中運行mvn install,項目將會自動打包並安裝到本地倉庫中。

  1. 運行一下DOS命令:mvn help:system
    如果前面的配置成功,那么E:\maven-repository會出現org文件夾及其他文件。

eclipse下配置maven

1.打開Window->Preferences->Maven->Installations,右側點擊Add

 

  1. 設置maven的安裝目錄,然后Finish

     

    image.png

  2. 選中剛剛添加的maven,並Apply

     

    image.png

4.打開Window->Preferences->Maven->User Settings,配置如下並Apply

 

image.png

intellij idea中配置maven

1.打開-File-Settings

image.png

 

2 找到maven .配置maven環境 setting.xml路徑 本地倉儲

 

image.png

 

maven生命周期

maven的生命周期和相關命令

1.一個項目的構建過成通常包括清理、編譯、測試、打包、集成測試、驗證、部署等。Maven從中抽取了一套完善的、易擴展的生命周期。Maven的生命周期是抽象的,其中的具體任務都交由插件來完成。Maven為大多數構建任務編寫並綁定了默認的插件。

2.Maven定義了三套生命周期:cleandefaultsite,每個生命周期都包含了一些階段(phase)。三套生命周期相互獨立,但各個生命周期中的階段卻是有順序的,且后面的夾斷依賴於前面的階段。執行某個階段時,其前面的階段會依順序執行,但不會觸發另外兩套生命周期中的任何階段。

maven常見的命令

validate:驗證項目是正確的,所有必要的信息都是可用的
compile:
編譯項目的源代碼
test:
使用適當的單元測試框架測試編譯后的源代碼。這些測試不應要求將代碼打包或部署
package:
使用已編譯的代碼,並將其打包成可分布格式,例如JAR
verify:
對集成測試的結果進行任何檢查,以確保滿足質量標准
install:
將包安裝到本地存儲庫中,以便在本地其他項目中使用該包
deploy:
在構建環境中完成,將最終的包復制到遠程存儲庫中,以便與其他開發人員和項目共享。
這些生命周期階段(加上這里沒有顯示的其他生命周期階段)是按順序執行的,以完成默認的生命周期。鑒於上面的生命周期階段,這意味着當使用默認的生命周期時,Maven將會優先執行檢查項目(validate),然后將會嘗試編譯源代碼(compile),運行集成測試方案(test),驗證了集成測試(verify),驗證包安裝到本地存儲庫(install),然后將安裝包部署到遠程存儲庫(deploy)。

eclipse 執行mvn命令

 

右鍵 run as maven bulid

 

執行compile命令

編譯

打成war

 

日志輸出位置

 

target目錄下生成的war

idea 執行mvn命令

 

image.png

image.png

 

pom文件標簽使用

同步滾動:

pom作為項目對象模型。通過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者需要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關因素。

 <!--父項目的坐標。如果項目中沒有規定某個元素的值,那么父項目中的對應值即為項目的默認值。 坐標包括group IDartifact ID version--> 

 <parent> 

 <!--被繼承的父項目的構件標識符--> 

 <artifactId/> 

 <!--被繼承的父項目的全球唯一標識符--> 

 <groupId/> 

 <!--被繼承的父項目的版本--> 

 <version/> 

 <!-- 父項目的pom.xml文件的相對路徑。相對路徑允許你選擇一個不同的路徑。默認值是../pom.xmlMaven首先在構建當前項目的地方尋找父項 目的pom,其次在文件系統的這個位置(relativePath位置),然后在本地倉庫,最后在遠程倉庫尋找父項目的pom--> 

 <relativePath/> 

 </parent> 

 <!--聲明項目描述符遵循哪一個POM模型版本。模型本身的版本很少改變,雖然如此,但它仍然是必不可少的,這是為了當Maven引入了新的特性或者其他模型變更的時候,確保穩定性。--> 

 <modelVersion>4.0.0</modelVersion> 

 <!--項目的全球唯一標識符,通常使用全限定的包名區分該項目和其他項目。並且構建時生成的路徑也是由此生成, com.mycompany.app生成的相對路徑為:/com/mycompany/app--> 

 <groupId>asia.banseon</groupId> 

 <!-- 構件的標識符,它和group ID一起唯一標識一個構件。換句話說,你不能有兩個不同的項目擁有同樣的artifact IDgroupID;在某個 特定的group ID下,artifact ID也必須是唯一的。構件是項目產生的或使用的一個東西,Maven為項目產生的構件包括:JARs,源 碼,二進制發布和WARs等。--> 

 <artifactId>banseon-maven2</artifactId> 

 <!--項目產生的構件類型,例如jarwarearpom。插件可以創建他們自己的構件類型,所以前面列的不是全部構件類型--> 

 <packaging>jar</packaging> 

 <!--項目當前版本,格式為:主版本.次版本.增量版本-限定版本號--> 

 <version>1.0-SNAPSHOT</version> 

 <!--項目的名稱, Maven產生的文檔用--> 

 <name>banseon-maven</name> 

 <!--項目主頁的URL, Maven產生的文檔用--> 

 <url>http://www.baidu.com/banseon</url> 

 <!-- 項目的詳細描述, Maven 產生的文檔用。 當這個元素能夠用HTML格式描述時(例如,CDATA中的文本會被解析器忽略,就可以包含HTML 簽), 不鼓勵使用純文本描述。如果你需要修改產生的web站點的索引頁面,你應該修改你自己的索引頁文件,而不是調整這里的文檔。--> 

 <description>A maven project to study maven.</description> 

 <!--描述了這個項目構建環境中的前提條件。--> 

 <prerequisites> 

 <!--構建該項目或使用該插件所需要的Maven的最低版本--> 

 <maven/> 

 </prerequisites> 

 <!--項目的問題管理系統(Bugzilla, Jira, Scarab,或任何你喜歡的問題管理系統)的名稱和URL,本例為 jira--> 

 <issueManagement> 

 <!--問題管理系統(例如jira)的名字,--> 

 <system>jira</system> 

 <!--該項目使用的問題管理系統的URL--> 

 <url>http://jira.baidu.com/banseon</url> 

 </issueManagement> 

 <!--項目持續集成信息--> 

 <ciManagement> 

 <!--持續集成系統的名字,例如continuum--> 

 <system/> 

 <!--該項目使用的持續集成系統的URL(如果持續集成系統有web接口的話)。--> 

 <url/> 

 <!--構建完成時,需要通知的開發者/用戶的配置項。包括被通知者信息和通知條件(錯誤,失敗,成功,警告)--> 

 <notifiers> 

 <!--配置一種方式,當構建中斷時,以該方式通知用戶/開發者--> 

 <notifier> 

 <!--傳送通知的途徑--> 

 <type/> 

 <!--發生錯誤時是否通知--> 

 <sendOnError/> 

 <!--構建失敗時是否通知--> 

 <sendOnFailure/> 

 <!--構建成功時是否通知--> 

 <sendOnSuccess/> 

 <!--發生警告時是否通知--> 

 <sendOnWarning/> 

 <!--不贊成使用。通知發送到哪里--> 

 <address/> 

 <!--擴展配置項--> 

 <configuration/> 

 </notifier> 

 </notifiers> 

 </ciManagement> 

 <!--項目創建年份,4位數字。當產生版權信息時需要使用這個值。--> 

 <inceptionYear/> 

 <!--項目相關郵件列表信息--> 

 <mailingLists> 

 <!--該元素描述了項目相關的所有郵件列表。自動產生的網站引用這些信息。--> 

 <mailingList> 

 <!--郵件的名稱--> 

 <name>Demo</name> 

 <!--發送郵件的地址或鏈接,如果是郵件地址,創建文檔時,mailto: 鏈接會被自動創建--> 

 <post>banseon@126.com</post> 

 <!--訂閱郵件的地址或鏈接,如果是郵件地址,創建文檔時,mailto: 鏈接會被自動創建--> 

 <subscribe>banseon@126.com</subscribe> 

 <!--取消訂閱郵件的地址或鏈接,如果是郵件地址,創建文檔時,mailto: 鏈接會被自動創建--> 

 <unsubscribe>banseon@126.com</unsubscribe> 

 <!--你可以瀏覽郵件信息的URL--> 

 <archive>http:/hi.baidu.com/banseon/demo/dev/</archive> 

 </mailingList> 

 </mailingLists> 

 <!--項目開發者列表--> 

 <developers> 

 <!--某個項目開發者的信息--> 

 <developer> 

 <!--SCM里項目開發者的唯一標識符--> 

 <id>HELLO WORLD</id> 

 <!--項目開發者的全名--> 

 <name>banseon</name> 

 <!--項目開發者的email--> 

 <email>banseon@126.com</email> 

 <!--項目開發者的主頁的URL--> 

 <url/> 

 <!--項目開發者在項目中扮演的角色,角色元素描述了各種角色--> 

 <roles> 

 <role>Project Manager</role> 

 <role>Architect</role> 

 </roles> 

 <!--項目開發者所屬組織--> 

 <organization>demo</organization> 

 <!--項目開發者所屬組織的URL--> 

 <organizationUrl>http://hi.baidu.com/banseon</organizationUrl> 

 <!--項目開發者屬性,如即時消息如何處理等--> 

 <properties> 

 <dept>No</dept> 

 </properties> 

 <!--項目開發者所在時區, -1112范圍內的整數。--> 

 <timezone>-5</timezone> 

 </developer> 

 </developers> 

 <!--項目的其他貢獻者列表--> 

 <contributors> 

 <!--項目的其他貢獻者。參見developers/developer元素--> 

 <contributor> 

 <name/><email/><url/><organization/><organizationUrl/><roles/><timezone/><properties/> 

 </contributor> 

 </contributors> 

 <!--該元素描述了項目所有License列表。 應該只列出該項目的license列表,不要列出依賴項目的 license列表。如果列出多個license,用戶可以選擇它們中的一個而不是接受所有license--> 

 <licenses> 

 <!--描述了項目的license,用於生成項目的web站點的license頁面,其他一些報表和validation也會用到該元素。--> 

 <license> 

 <!--license用於法律上的名稱--> 

 <name>Apache 2</name> 

 <!--官方的license正文頁面的URL--> 

 <url>http://www.baidu.com/banseon/LICENSE-2.0.txt</url> 

 <!--項目分發的主要方式: 

 repo,可以從Maven庫下載 

 manual 用戶必須手動下載和安裝依賴--> 

 <distribution>repo</distribution> 

 <!--關於license的補充信息--> 

 <comments>A business-friendly OSS license</comments> 

 </license> 

 </licenses> 

 <!--SCM(Source Control Management)標簽允許你配置你的代碼庫,供Maven web站點和其它插件使用。--> 

 <scm> 

 <!--SCMURL,URL描述了版本庫和如何連接到版本庫。欲知詳情,請看SCMs提供的URL格式和列表。該連接只讀。--> 

 <connection> 

 scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk) 

 </connection> 

 <!--給開發者使用的,類似connection元素。即該連接不僅僅只讀--> 

 <developerConnection> 

 scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk 

 </developerConnection> 

 <!--當前代碼的標簽,在開發階段默認為HEAD--> 

 <tag/> 

 <!--指向項目的可瀏覽SCM庫(例如ViewVC或者Fisheye)的URL--> 

 <url>http://svn.baidu.com/banseon</url> 

 </scm> 

 <!--描述項目所屬組織的各種屬性。Maven產生的文檔用--> 

 <organization> 

 <!--組織的全名--> 

 <name>demo</name> 

 <!--組織主頁的URL--> 

 <url>http://www.baidu.com/banseon</url> 

 </organization> 

 <!--構建項目需要的信息--> 

 <build> 

 <!--該元素設置了項目源碼目錄,當構建項目的時候,構建系統會編譯目錄里的源碼。該路徑是相對於pom.xml的相對路徑。--> 

 <sourceDirectory/> 

 <!--該元素設置了項目腳本源碼目錄,該目錄和源碼目錄不同:絕大多數情況下,該目錄下的內容 會被拷貝到輸出目錄(因為腳本是被解釋的,而不是被編譯的)--> 

 <scriptSourceDirectory/> 

 <!--該元素設置了項目單元測試使用的源碼目錄,當測試項目的時候,構建系統會編譯目錄里的源碼。該路徑是相對於pom.xml的相對路徑。--> 

 <testSourceDirectory/> 

 <!--被編譯過的應用程序class文件存放的目錄。--> 

 <outputDirectory/> 

 <!--被編譯過的測試class文件存放的目錄。--> 

 <testOutputDirectory/> 

 <!--使用來自該項目的一系列構建擴展--> 

 <extensions> 

 <!--描述使用到的構建擴展。--> 

 <extension> 

 <!--構建擴展的groupId--> 

 <groupId/> 

 <!--構建擴展的artifactId--> 

 <artifactId/> 

 <!--構建擴展的版本--> 

 <version/> 

 </extension> 

 </extensions> 

 <!--當項目沒有規定目標(Maven2 叫做階段)時的默認值--> 

 <defaultGoal/> 

 <!--這個元素描述了項目相關的所有資源路徑列表,例如和項目相關的屬性文件,這些資源被包含在最終的打包文件里。--> 

 <resources> 

 <!--這個元素描述了項目相關或測試相關的所有資源路徑--> 

 <resource> 

 <!-- 描述了資源的目標路徑。該路徑相對target/classes目錄(例如${project.build.outputDirectory})。舉個例 子,如果你想資源在特定的包里(org.apache.maven.messages),你就必須該元素設置為org/apache/maven /messages。然而,如果你只是想把資源放到源碼目錄結構里,就不需要該配置。--> 

 <targetPath/> 

 <!--是否使用參數值代替參數名。參數值取自properties元素或者文件里配置的屬性,文件在filters元素里列出。--> 

 <filtering/> 

 <!--描述存放資源的目錄,該路徑相對POM路徑--> 

 <directory/> 

 <!--包含的模式列表,例如**/*.xml.--> 

 <includes/> 

 <!--排除的模式列表,例如**/*.xml--> 

 <excludes/> 

 </resource> 

 </resources> 

 <!--這個元素描述了單元測試相關的所有資源路徑,例如和單元測試相關的屬性文件。--> 

 <testResources> 

 <!--這個元素描述了測試相關的所有資源路徑,參見build/resources/resource元素的說明--> 

 <testResource> 

 <targetPath/><filtering/><directory/><includes/><excludes/> 

 </testResource> 

 </testResources> 

 <!--構建產生的所有文件存放的目錄--> 

 <directory/> 

 <!--產生的構件的文件名,默認值是${artifactId}-${version}--> 

 <finalName/> 

 <!--filtering開關打開時,使用到的過濾器屬性文件列表--> 

 <filters/> 

 <!--子項目可以引用的默認插件信息。該插件配置項直到被引用時才會被解析或綁定到生命周期。給定插件的任何本地配置都會覆蓋這里的配置--> 

 <pluginManagement> 

 <!--使用的插件列表 --> 

 <plugins> 

 <!--plugin元素包含描述插件所需要的信息。--> 

 <plugin> 

 <!--插件在倉庫里的group ID--> 

 <groupId/> 

 <!--插件在倉庫里的artifact ID--> 

 <artifactId/> 

 <!--被使用的插件的版本(或版本范圍)--> 

 <version/> 

 <!--是否從該插件下載Maven擴展(例如打包和類型處理器),由於性能原因,只有在真需要下載時,該元素才被設置成enabled--> 

 <extensions/> 

 <!--在構建生命周期中執行一組目標的配置。每個目標可能有不同的配置。--> 

 <executions> 

 <!--execution元素包含了插件執行需要的信息--> 

 <execution> 

 <!--執行目標的標識符,用於標識構建過程中的目標,或者匹配繼承過程中需要合並的執行目標--> 

 <id/> 

 <!--綁定了目標的構建生命周期階段,如果省略,目標會被綁定到源數據里配置的默認階段--> 

 <phase/> 

 <!--配置的執行目標--> 

 <goals/> 

 <!--配置是否被傳播到子POM--> 

 <inherited/> 

 <!--作為DOM對象的配置--> 

 <configuration/> 

 </execution> 

 </executions> 

 <!--項目引入插件所需要的額外依賴--> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 <!--任何配置是否被傳播到子項目--> 

 <inherited/> 

 <!--作為DOM對象的配置--> 

 <configuration/> 

 </plugin> 

 </plugins> 

 </pluginManagement> 

 <!--使用的插件列表--> 

 <plugins> 

 <!--參見build/pluginManagement/plugins/plugin元素--> 

 <plugin> 

 <groupId/><artifactId/><version/><extensions/> 

 <executions> 

 <execution> 

 <id/><phase/><goals/><inherited/><configuration/> 

 </execution> 

 </executions> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 <goals/><inherited/><configuration/> 

 </plugin> 

 </plugins> 

 </build> 

 <!--在列的項目構建profile,如果被激活,會修改構建處理--> 

 <profiles> 

 <!--根據環境參數或命令行參數激活某個構建處理--> 

 <profile> 

 <!--構建配置的唯一標識符。即用於命令行激活,也用於在繼承時合並具有相同標識符的profile--> 

 <id/> 

 <!--自動觸發profile的條件邏輯。Activationprofile的開啟鑰匙。profile的力量來自於它 

 能夠在某些特定的環境中自動使用某些特定的值;這些環境通過activation元素指定。activation元素並不是激活profile的唯一方式。--> 

 <activation> 

 <!--profile默認是否激活的標志--> 

 <activeByDefault/> 

 <!--當匹配的jdk被檢測到,profile被激活。例如,1.4激活JDK1.41.4.0_2,而!1.4激活所有版本不是以1.4開頭的JDK--> 

 <jdk/> 

 <!--當匹配的操作系統屬性被檢測到,profile被激活。os元素可以定義一些操作系統相關的屬性。--> 

 <os> 

 <!--激活profile的操作系統的名字--> 

 <name>Windows XP</name> 

 <!--激活profile的操作系統所屬家族( 'windows')--> 

 <family>Windows</family> 

 <!--激活profile的操作系統體系結構 --> 

 <arch>x86</arch> 

 <!--激活profile的操作系統版本--> 

 <version>5.1.2600</version> 

 </os> 

 <!--如果Maven檢測到某一個屬性(其值可以在POM中通過${名稱}引用),其擁有對應的名稱和值,Profile就會被激活。如果值 

 字段是空的,那么存在屬性名稱字段就會激活profile,否則按區分大小寫方式匹配屬性值字段--> 

 <property> 

 <!--激活profile的屬性的名稱--> 

 <name>mavenVersion</name> 

 <!--激活profile的屬性的值--> 

 <value>2.0.3</value> 

 </property> 

 <!--提供一個文件名,通過檢測該文件的存在或不存在來激活profilemissing檢查文件是否存在,如果不存在則激活 

 profile。另一方面,exists則會檢查文件是否存在,如果存在則激活profile--> 

 <file> 

 <!--如果指定的文件存在,則激活profile--> 

 <exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</exists> 

 <!--如果指定的文件不存在,則激活profile--> 

 <missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</missing> 

 </file> 

 </activation> 

 <!--構建項目所需要的信息。參見build元素--> 

 <build> 

 <defaultGoal/> 

 <resources> 

 <resource> 

 <targetPath/><filtering/><directory/><includes/><excludes/> 

 </resource> 

 </resources> 

 <testResources> 

 <testResource> 

 <targetPath/><filtering/><directory/><includes/><excludes/> 

 </testResource> 

 </testResources> 

 <directory/><finalName/><filters/> 

 <pluginManagement> 

 <plugins> 

 <!--參見build/pluginManagement/plugins/plugin元素--> 

 <plugin> 

 <groupId/><artifactId/><version/><extensions/> 

 <executions> 

 <execution> 

 <id/><phase/><goals/><inherited/><configuration/> 

 </execution> 

 </executions> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 <goals/><inherited/><configuration/> 

 </plugin> 

 </plugins> 

 </pluginManagement> 

 <plugins> 

 <!--參見build/pluginManagement/plugins/plugin元素--> 

 <plugin> 

 <groupId/><artifactId/><version/><extensions/> 

 <executions> 

 <execution> 

 <id/><phase/><goals/><inherited/><configuration/> 

 </execution> 

 </executions> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 <goals/><inherited/><configuration/> 

 </plugin> 

 </plugins> 

 </build> 

 <!--模塊(有時稱作子項目) 被構建成項目的一部分。列出的每個模塊元素是指向該模塊的目錄的相對路徑--> 

 <modules/> 

 <!--發現依賴和擴展的遠程倉庫列表。--> 

 <repositories> 

 <!--參見repositories/repository元素--> 

 <repository> 

 <releases> 

 <enabled/><updatePolicy/><checksumPolicy/> 

 </releases> 

 <snapshots> 

 <enabled/><updatePolicy/><checksumPolicy/> 

 </snapshots> 

 <id/><name/><url/><layout/> 

 </repository> 

 </repositories> 

 <!--發現插件的遠程倉庫列表,這些插件用於構建和報表--> 

 <pluginRepositories> 

 <!--包含需要連接到遠程插件倉庫的信息.參見repositories/repository元素--> 

 <pluginRepository> 

 <releases> 

 <enabled/><updatePolicy/><checksumPolicy/> 

 </releases> 

 <snapshots> 

 <enabled/><updatePolicy/><checksumPolicy/> 

 </snapshots> 

 <id/><name/><url/><layout/> 

 </pluginRepository> 

 </pluginRepositories> 

 <!--該元素描述了項目相關的所有依賴。 這些依賴組成了項目構建過程中的一個個環節。它們自動從項目定義的倉庫中下載。要獲取更多信息,請看項目依賴機制。--> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 <!--不贊成使用. 現在Maven忽略該元素.--> 

 <reports/> 

 <!--該元素包括使用報表插件產生報表的規范。當用戶執行"mvn site",這些報表就會運行。 在頁面導航欄能看到所有報表的鏈接。參見reporting元素--> 

 <reporting> 

 ...... 

 </reporting> 

 <!--參見dependencyManagement元素--> 

 <dependencyManagement> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 </dependencyManagement> 

 <!--參見distributionManagement元素--> 

 <distributionManagement> 

 ...... 

 </distributionManagement> 

 <!--參見properties元素--> 

 <properties/> 

 </profile> 

 </profiles> 

 <!--模塊(有時稱作子項目) 被構建成項目的一部分。列出的每個模塊元素是指向該模塊的目錄的相對路徑--> 

 <modules/> 

 <!--發現依賴和擴展的遠程倉庫列表。--> 

 <repositories> 

 <!--包含需要連接到遠程倉庫的信息--> 

 <repository> 

 <!--如何處理遠程倉庫里發布版本的下載--> 

 <releases> 

 <!--true或者false表示該倉庫是否為下載某種類型構件(發布版,快照版)開啟。 --> 

 <enabled/> 

 <!--該元素指定更新發生的頻率。Maven會比較本地POM和遠程POM的時間戳。這里的選項是:always(一直),daily(默認,每日),intervalX(這里X是以分鍾為單位的時間間隔),或者never(從不)。--> 

 <updatePolicy/> 

 <!--Maven驗證構件校驗文件失敗時該怎么做:ignore(忽略),fail(失敗),或者warn(警告)。--> 

 <checksumPolicy/> 

 </releases> 

 <!-- 如何處理遠程倉庫里快照版本的下載。有了releasessnapshots這兩組配置,POM就可以在每個單獨的倉庫中,為每種類型的構件采取不同的 策略。例如,可能有人會決定只為開發目的開啟對快照版本下載的支持。參見repositories/repository/releases元素 --> 

 <snapshots> 

 <enabled/><updatePolicy/><checksumPolicy/> 

 </snapshots> 

 <!--遠程倉庫唯一標識符。可以用來匹配在settings.xml文件里配置的遠程倉庫--> 

 <id>banseon-repository-proxy</id> 

 <!--遠程倉庫名稱--> 

 <name>banseon-repository-proxy</name> 

 <!--遠程倉庫URL,按protocol://hostname/path形式--> 

 <url>http://192.168.1.169:9999/repository/</url> 

 <!-- 用於定位和排序構件的倉庫布局類型-可以是default(默認)或者legacy(遺留)。Maven 2為其倉庫提供了一個默認的布局;然 而,Maven 1.x有一種不同的布局。我們可以使用該元素指定布局是default(默認)還是legacy(遺留)。--> 

 <layout>default</layout> 

 </repository> 

 </repositories> 

 <!--發現插件的遠程倉庫列表,這些插件用於構建和報表--> 

 <pluginRepositories> 

 <!--包含需要連接到遠程插件倉庫的信息.參見repositories/repository元素--> 

 <pluginRepository> 

 ...... 

 </pluginRepository> 

 </pluginRepositories> 

 

 <!--該元素描述了項目相關的所有依賴。 這些依賴組成了項目構建過程中的一個個環節。它們自動從項目定義的倉庫中下載。要獲取更多信息,請看項目依賴機制。--> 

 <dependencies> 

 <dependency> 

 <!--依賴的group ID--> 

 <groupId>org.apache.maven</groupId> 

 <!--依賴的artifact ID--> 

 <artifactId>maven-artifact</artifactId> 

 <!--依賴的版本號。 Maven 2, 也可以配置成版本號的范圍。--> 

 <version>3.8.1</version> 

 <!-- 依賴類型,默認類型是jar。它通常表示依賴的文件的擴展名,但也有例外。一個類型可以被映射成另外一個擴展名或分類器。類型經常和使用的打包方式對應, 盡管這也有例外。一些類型的例子:jarwarejb-clienttest-jar。如果設置extensions true,就可以在 plugin里定義新的類型。所以前面的類型的例子不完整。--> 

 <type>jar</type> 

 <!-- 依賴的分類器。分類器可以區分屬於同一個POM,但不同構建方式的構件。分類器名被附加到文件名的版本號后面。例如,如果你想要構建兩個單獨的構件成 JAR,一個使用Java 1.4編譯器,另一個使用Java 6編譯器,你就可以使用分類器來生成兩個單獨的JAR構件。--> 

 <classifier></classifier> 

 <!--依賴范圍。在項目發布過程中,幫助決定哪些構件被包括進來。欲知詳情請參考依賴機制。 

 - compile :默認范圍,用於編譯 

 - provided:類似於編譯,但支持你期待jdk或者容器提供,類似於classpath 

 - runtime: 在執行時需要使用 

 - test: 用於test任務時使用 

 - system: 需要外在提供相應的元素。通過systemPath來取得 

 - systemPath: 僅用於范圍為system。提供相應的路徑 

 - optional: 當項目自身被依賴時,標注依賴是否傳遞。用於連續依賴時使用--> 

 <scope>test</scope> 

 <!--僅供system范圍使用。注意,不鼓勵使用這個元素,並且在新的版本中該元素可能被覆蓋掉。該元素為依賴規定了文件系統上的路徑。需要絕對路徑而不是相對路徑。推薦使用屬性匹配絕對路徑,例如${java.home}--> 

 <systemPath></systemPath> 

 <!--當計算傳遞依賴時, 從依賴構件列表里,列出被排除的依賴構件集。即告訴maven你只依賴指定的項目,不依賴項目的依賴。此元素主要用於解決版本沖突問題--> 

 <exclusions> 

 <exclusion> 

 <artifactId>spring-core</artifactId> 

 <groupId>org.springframework</groupId> 

 </exclusion> 

 </exclusions> 

 <!--可選依賴,如果你在項目B中把C依賴聲明為可選,你就需要在依賴於B的項目(例如項目A)中顯式的引用對C的依賴。可選依賴阻斷依賴的傳遞性。--> 

 <optional>true</optional> 

 </dependency> 

 </dependencies> 

 <!--不贊成使用. 現在Maven忽略該元素.--> 

 <reports></reports> 

 <!--該元素描述使用報表插件產生報表的規范。當用戶執行"mvn site",這些報表就會運行。 在頁面導航欄能看到所有報表的鏈接。--> 

 <reporting> 

 <!--true,則,網站不包括默認的報表。這包括"項目信息"菜單中的報表。--> 

 <excludeDefaults/> 

 <!--所有產生的報表存放到哪里。默認值是${project.build.directory}/site--> 

 <outputDirectory/> 

 <!--使用的報表插件和他們的配置。--> 

 <plugins> 

 <!--plugin元素包含描述報表插件需要的信息--> 

 <plugin> 

 <!--報表插件在倉庫里的group ID--> 

 <groupId/> 

 <!--報表插件在倉庫里的artifact ID--> 

 <artifactId/> 

 <!--被使用的報表插件的版本(或版本范圍)--> 

 <version/> 

 <!--任何配置是否被傳播到子項目--> 

 <inherited/> 

 <!--報表插件的配置--> 

 <configuration/> 

 <!--一組報表的多重規范,每個規范可能有不同的配置。一個規范(報表集)對應一個執行目標 。例如,有123456789個報表。125構成A報表集,對應一個執行目標。258構成B報表集,對應另一個執行目標--> 

 <reportSets> 

 <!--表示報表的一個集合,以及產生該集合的配置--> 

 <reportSet> 

 <!--報表集合的唯一標識符,POM繼承時用到--> 

 <id/> 

 <!--產生報表集合時,被使用的報表的配置--> 

 <configuration/> 

 <!--配置是否被繼承到子POMs--> 

 <inherited/> 

 <!--這個集合里使用到哪些報表--> 

 <reports/> 

 </reportSet> 

 </reportSets> 

 </plugin> 

 </plugins> 

 </reporting> 

 <!-- 繼承自該項目的所有子項目的默認依賴信息。這部分的依賴信息不會被立即解析,而是當子項目聲明一個依賴(必須描述group ID artifact ID信息),如果group IDartifact ID以外的一些信息沒有描述,則通過group IDartifact ID 匹配到這里的依賴,並使用這里的依賴信息。--> 

 <dependencyManagement> 

 <dependencies> 

 <!--參見dependencies/dependency元素--> 

 <dependency> 

 ...... 

 </dependency> 

 </dependencies> 

 </dependencyManagement> 

 <!--項目分發信息,在執行mvn deploy后表示要發布的位置。有了這些信息就可以把網站部署到遠程服務器或者把構件部署到遠程倉庫。--> 

 <distributionManagement> 

 <!--部署項目產生的構件到遠程倉庫需要的信息--> 

 <repository> 

 <!--是分配給快照一個唯一的版本號(由時間戳和構建流水號)?還是每次都使用相同的版本號?參見repositories/repository元素--> 

 <uniqueVersion/> 

 <id>banseon-maven2</id> 

 <name>banseon maven2</name> 

 <url>file://${basedir}/target/deploy</url> 

 <layout/> 

 </repository> 

 <!--構件的快照部署到哪里?如果沒有配置該元素,默認部署到repository元素配置的倉庫,參見distributionManagement/repository元素--> 

 <snapshotRepository> 

 <uniqueVersion/> 

 <id>banseon-maven2</id> 

 <name>Banseon-maven2 Snapshot Repository</name> 

 <url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url> 

 <layout/> 

 </snapshotRepository> 

 <!--部署項目的網站需要的信息--> 

 <site> 

 <!--部署位置的唯一標識符,用來匹配站點和settings.xml文件里的配置--> 

 <id>banseon-site</id> 

 <!--部署位置的名稱--> 

 <name>business api website</name> 

 <!--部署位置的URL,按protocol://hostname/path形式--> 

 <url> 

 scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web 

 </url> 

 </site> 

 <!--項目下載頁面的URL。如果沒有該元素,用戶應該參考主頁。使用該元素的原因是:幫助定位那些不在倉庫里的構件(由於license限制)。--> 

 <downloadUrl/> 

 <!--如果構件有了新的group IDartifact ID(構件移到了新的位置),這里列出構件的重定位信息。--> 

 <relocation> 

 <!--構件新的group ID--> 

 <groupId/> 

 <!--構件新的artifact ID--> 

 <artifactId/> 

 <!--構件新的版本號--> 

 <version/> 

 <!--顯示給用戶的,關於移動的額外信息,例如原因。--> 

 <message/> 

 </relocation> 

 <!-- 給出該構件在遠程倉庫的狀態。不得在本地項目中設置該元素,因為這是工具自動更新的。有效的值有:none(默認),converted(倉庫管理員從 Maven 1 POM轉換過來),partner(直接從伙伴Maven 2倉庫同步過來),deployed(從Maven 2實例部 署),verified(被核實時正確的和最終的)。--> 

 <status/> 

 </distributionManagement> 

 <!--以值替代名稱,Properties可以在整個POM中使用,也可以作為觸發條件(見settings.xml配置文件里activation元素的說明)。格式是<name>value</name>--> 

 <properties/> 

</project>  

 

使用maven制作jar 以及將jar包發布到maven倉儲中

制作test1

<groupId>com.neusoft.test</groupId>

<artifactId>test1</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>maventest</name>

<description>only a maven test jar</description>

 

將test1引入至test2 maven項目中

<dependencies>

    <dependency>

         <groupId>com.neusoft.test</groupId>

         <artifactId>test1</artifactId>

          <version>0.0.1-SNAPSHOT</version>

    </dependency>

</dependencies>

可以直接引入Test1

 

將自定義jar包發布到本地maven

service打成jar 選擇Add directory entries 添加到maven本地倉儲中
命令:

mvn install:install-file

-DgroupId=包名

-DartifactId=項目名

-Dversion=版本號

-Dpackaging=jar

-Dfile=jar文件所在路徑(我這里使用的是絕對路徑)

 

mvn install:install-file -Dfile=F:\temp\add\aaa.jar -DgroupId=aaa -DartifactId=aaa-jar -Dversion=1.0 -Dpackaging=jar 

 

在執行 mvn site 命令時報錯:

java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent

解決方法,重新配置site plugin 詳細配置見

https://maven.apache.org/plugins/maven-site-plugin/

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-site-plugin</artifactId>

<version>3.8.2</version>

<configuration>

<port>9000</port>

<tempWebappDirectory>${basedir}/target/site/tempdir</tempWebappDirectory>

</configuration>

</plugin>

</plugins>

</build>

創建maven web工程

發現工程報錯,在webapp文件夾下創建一個WEB-INF文件夾,在里面放一個web.xml文件

新建HelloServlet 發現報錯原因為 在進行編譯時 需要使用servlet-api.jar

去maven官網 搜索對應依賴https://mvnrepository.com/

 

加入對應dependency配置

<dependencies>

     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->

    <dependency>

     <groupId>javax.servlet</groupId>

     <artifactId>javax.servlet-api</artifactId>

     <version>3.1.0</version>

     <scope>provided</scope>

    </dependency>

 

</dependencies>

 

使用測試test

在test文件夾中寫測試代碼

使用測試代碼要在測試范圍內引入junit包 到倉庫中搜索

<!-- https://mvnrepository.com/artifact/junit/junit -->

    <dependency>

     <groupId>junit</groupId>

     <artifactId>junit</artifactId>

     <version>4.12</version>

     <scope>test</scope>

    </dependency>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM