1. 程式人生 > >版本依賴管理之 Maven 學習手冊

版本依賴管理之 Maven 學習手冊

0x01 為什麼使用版本依賴工具Maven?

1.1 倉庫簡介

  • 沒有Maven 時,專案用到的.jar 檔案通常需要拷貝到lib目錄下,專案多了,拷貝的檔案副本就多了,佔用磁碟空間,且難以管理。
  • Maven使用 一個稱之為倉庫的目錄,根據構件的座標統一儲存這些構件的唯一副本,在專案中通過依賴宣告,可以方便的引用構件。

比如javax.servlet-api-4.0.1.jar 實際儲存如圖所示:

image

對應的Maven配置就是:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

對應的本地Maven倉庫地址檔案路徑就是:

C:\Users\fairy\.m2\repository\javax\servlet\servlet-api\2.4

1.2 Maven倉庫分類

image

  • Maven 倉庫分為本地倉庫和遠端倉庫尋找構件時,首先從本地倉庫找,找不到則到遠端倉庫找,再找不到就報錯。
  • 在遠端倉庫中找到了,就下載到本地倉庫再使用
  • 中央倉庫是Maven核心自帶的遠端倉庫

1.2.1 本地倉庫

Maven本地預設倉庫地址為:

${user.home}.m2/repository

更改本地倉庫位置的方法:

1. 全域性修改

修改Maven安裝目錄下的配置檔案

%MAVEN_HOME%/conf/settings.xml

配置如下:

 <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>${user.home}/.m2/repository</localRepository>
  <!-- 指定本地倉庫路徑 -->
2. 針對當前使用者(推薦)

修改使用者目錄下的配置檔案

${user.home}.m2/settings.xml

配置如下:

 <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>${user.home}/.m2/repository</localRepository>
  <!-- 指定本地倉庫路徑 -->

1.2.2 中央倉庫

安裝完Maven,本地倉庫幾乎是空的,這時候需要從遠端倉庫下載所需的構件。
Maven 配置了一個預設的遠端倉庫即中央倉庫。
倉庫地址:

<mirrors>
    <mirror>
      <id>UK</id>
      <name>UK Central</name>
      <url>http://uk.maven.org/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

美國官方中央倉庫地址:

http://repo.maven.apache.org/maven2

英國官方中央倉庫地址:

http://uk.maven.org/maven2

阿里雲中央倉庫地址:

<repository>
          <id>alimaven</id>  
          <name>aliyun maven</name>  
          <url>https://maven.aliyun.com/repository/public</url>
           <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
</repository>

1.3 Maven 座標

Maven的所有構件都是通過座標進行組織和管理。maven的座標通過五個元素進行定義,其中groupId,artifactld,version 是必須的,package 是可選的(預設為jar)
classifier 是不能直接定義的。

  • group :定義當前Maven專案所屬的實際專案,跟Java包名類似,通常與域名反向一一對應。
  • artifactId,定義當前Maven專案的一個模組,預設情況下,Maven 生成的構件其檔名會以artifactId 開頭,如
    javax.servlet-api-4.0.1.jar
  • version: 定義專案版本
  • packaging:定義專案打包方式,如jar,war,zip... 預設為jar
  • scope:依賴範圍,取值有compile,test,provided,預設為compile

1.4 Maven 依賴範圍

執行不同的Maven命令(mvn package,mvn test,mvn install......),會使用不同的classpath,執行classpath。
scope選項的值,決定了該依賴構件會被引入到哪一個classpath中。

  • compile:編譯依賴範圍,預設值。此選項對編譯、除錯、執行的時候都需要該依賴。
  • test:測試依賴範圍。只對測試有效,表明只在測試的時候需要,在編譯和執行時將無法使用該依賴,如Junit。
  • provided:已提供依賴範圍。編譯和測試有效,執行無效。如servlet-api,在專案執行時,tomcat等容易已經提供,無須Maven重複引入

1.5 Maven 依賴傳遞

image

  • 如上圖所示,hibernate-core 依賴hibernate-commons-annotations,而hibernate-commons-annotations又依賴slf4j-api,hibernate對slf4j-api的依賴就是傳遞依賴,我們只需要引入hibernate-core構件的依賴,不用考慮它還有其他的依賴,也不用擔心會引入多餘或衝突的依賴,maven 會自動為我們引入依賴和傳遞其依賴。

1.5 使用Maven好處

  • Maven可以對第三方依賴庫進行統一的版本管理。
  • 使用maven可以統一專案的目錄結構
  • maven 還支援多種外掛
  • 使用maven 可以大大降低專案檔案的大小。

0x02 Maven的下載安裝配置

  1. 官網下載maven
  2. 解壓並配置環境變數:
M2_HOME
C:\Apps\maven\apache-maven-3.6.0

如下所示:
image
3.複製安裝目錄下的settings.xml到使用者目錄下
安裝目錄路徑如下:

%MAVEN_HOME%/conf/settings.xml

比如我的就是:

C:\Apps\maven\apache-maven-3.6.0\conf\setings.xml

使用者目錄路徑如下:

${user.home}.m2/repository

比如我們的就是:

C:\Users\fairy\.m2\settings.xml
  1. 配置settings.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.
-->

<!-- Maven Configuration Guide http://maven.apache.org/settings.html -->
<!--
 | 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.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>/path/to/local/repo</localRepository>
  -->
  <localRepository>${user.home}/.m2/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>
  -->
   <interactiveMode>true</interactiveMode>
   <!-- 如果認為Maven應該嘗試與使用者進行互動以進行輸入,使用true,否則返回false。 預設為true。-->

  <!-- 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>
   | 是否啟用離線模式,預設是false
   | 如果改為true表示啟用離線模式,即只加載本地倉庫Jar包不支援聯網下載
   | 如果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>
    -->
    <!-- 使用下面配置後 org.mortbay.jetty:jetty-maven-plugin:run 可以簡寫成 mvn jetty:run -->
    <pluginGroup>org.mortbay.jetty</pluginGroup>
    <pluginGroup>org.sonatype.plugins</pluginGroup>
    <pluginGroup>org.sonarsource.scanner.maven</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>UK</id>
      <name>UK Central</name>
      <url>http://uk.maven.org/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
     --> 
    <!-- 
    官方倉庫映象:
    美國 http://repo.maven.apache.org/maven2
    英國 http://uk.maven.org/maven2 
    阿里雲
        <mirror>  
          <id>alimaven</id>  
          <name>aliyun maven</name>  
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>;  
          <mirrorOf>central</mirrorOf>          
        </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
     | 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>
    -->

    <!--
     | 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>
    -->

    <profile>
      <id>myHomeProfile</id>
      
      <!-- 自定義多個倉庫映象-->      
      <repositories>
     
         
       <!-- 阿里雲倉庫 -->
       <repository>
          <id>alimaven</id>  
          <name>aliyun maven</name>  
          <url>https://maven.aliyun.com/repository/public</url>
           <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>

       <!-- maven 倉庫-->
       <repository>
          <id>Centeral</id>
          <name>Central Maven2</name>
          <url>http://central.maven.org/maven2</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository> 

       <!-- Maven 英國中心倉庫-->
       <repository>
          <id>UK</id>
          <name>UK Central</name>
          <url>http://uk.maven.org/maven2</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
        <repository>
          <id>Snoatype</id>
          <name>Snoatype</name>
          <url>http://mvnrepository.com/repos/sonatype-releases</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
        <repository>
          <id>SpringPlugin</id>
          <name>Spring Plugins</name>
          <url>http://mvnrepository.com/repos/sonatype-releases</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
        <repository>
          <id>SpringLibs</id>
          <name>Spring Libs</name>
          <url>http://repo.spring.io/libs-milestone/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
         <repository>
          <id>Atlassian</id>
          <name>Atlassian Repository</name>
          <url>https://maven.atlassian.com/content/repositories/atlassian-public/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
       <repository>
          <id>Hortonworks</id>
          <name>Hortonworks Repository</name>
          <url>http://repo.hortonworks.com/content/repositories/releases/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
         <repository>
          <id>Jboss</id>
          <name>JBoss Releases Repository</name>
          <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
        <repository>
          <id>Nexeo</id>
          <name>Nuxeo Releases Repository</name>
          <url>https://maven-eu.nuxeo.org/nexus/content/repositories/public-releases/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
        <repository>
          <id>XWiki</id>
          <name>XWiki Releases Repository</name>
          <url>http://maven.xwiki.org/releases/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
      
       <repository>
          <id>Apache</id>
          <name>Apache Releases Repository</name>
          <url>https://repository.apache.org/content/repositories/releases/</url>
          <snapshots><enabled>false</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
               
     </repositories>
     <!--
    <pluginRepositories>
        <pluginRepository>
            <id>maven.oracle.com</id>
            <url>https://maven.oracle.com</url>
        </pluginRepository>
    </pluginRepositories>
    -->

        <pluginRepositories>
            <pluginRepository>
              <id>nexus-aliyun</id>
              <url>http://maven.aliyun.com/nexus/content/groups/public</url>
              <releases>
                <enabled>true</enabled>
              </releases>
              <snapshots>
                <enabled>true</enabled>
              </snapshots>
            </pluginRepository>
        </pluginRepositories>

    </profile>
    
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
  <activeProfiles>
    <activeProfile>myHomeProfile</activeProfile>
  </activeProfiles>
</settings>

0x03 Eclipse中的Maven使用

STS 是Spring官方團隊出品的,最友好支援Spring的Eclipse衍生版本。

STS 下載地址:https://spring.io/tools
STS 已經繼承了Maven外掛,Maven並不執行任何具體的構件任務,所有這些任務都交給具體的外掛來完成,例如:

  • 編譯原始碼是由maven-compile-plugin完成的。
  • 把Web 應用部署到tomcat 下是由tomcat-maven-plugin完成的,maven Tomcat 外掛現在主要有兩個版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同。

STS 中配置Maven

配置安裝目錄

STS預設安裝了一個版本的Maven,但是一般我們更換我們自己安裝的。
image

配置使用者設定

image

STS 中建立Maven Web專案

1.空白處右鍵 New-------> Other...
image
2.輸入maven關鍵字,找到maven Project
image
3.進入New Maven Project介面
image
4.選擇專案模板
Catalog下拉選擇Internal,Filter 過濾器選擇web,選中,然後Next
image
5.填寫專案資訊,然後點選Finish即可
image
6.選中專案執行安裝依賴

mvn install

當看到Build Success 說明成功
7.執行專案,命令執行後我們的專案就執行到tomcat7中了

tomcat7:run

8.然後開啟瀏覽器訪問網站即可

本篇完~