天天看點

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

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

1.1 倉庫簡介

  • 沒有Maven 時,項目用到的.jar 檔案通常需要拷貝到lib目錄下,項目多了,拷貝的檔案副本就多了,占用磁盤空間,且難以管理。
  • Maven使用 一個稱之為倉庫的目錄,根據構件的坐标統一存儲這些構件的唯一副本,在項目中通過依賴聲明,可以友善的引用構件。

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

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

對應的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倉庫分類

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四
  • 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           

共享統用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 依賴傳遞

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四
  • 如上圖所示,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           

如下所示:

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

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>myShareProfile</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>
       
       <!--Spring 插件類庫-->
        <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>
       
        <!--Spring 類庫-->
        <repository>
          <id>SpringLibs</id>
          <name>Spring Libs</name>
          <url>http://repo.spring.io/libs-milestone/</url>
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
       </repository>
       
         <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </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>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>nexus-aliyun</id>
                  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                  <releases>
                    <enabled>true</enabled>
                  </releases>
                  <snapshots>
                    <enabled>true</enabled>
                  </snapshots>
                </pluginRepository>
          
                <pluginRepository>
                    <id>spring-snapshots</id>
                    <name>Spring Snapshots</name>
                    <url>https://repo.spring.io/libs-snapshot-local</url>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
        
                <pluginRepository>
                    <id>spring-milestones</id>
                    <name>Spring Milestones</name>
                    <url>https://repo.spring.io/libs-milestone-local</url>
                    <snapshots>
                        <enabled>false</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>myShareProfile</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,但是一般我們更換我們自己安裝的。

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四
配置使用者設定
版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

STS 中建立Maven Web項目

1.空白處右鍵 New-------> Other...

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

2.輸入maven關鍵字,找到maven Project

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

3.進入New Maven Project界面

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

4.選擇項目模闆

Catalog下拉選擇Internal,Filter 過濾器選擇web,選中,然後Next

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

5.填寫項目資訊,然後點選Finish即可

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

6.修改JDK改變目錄結構

完成後我們看到的目錄結構會是這個樣子的

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

是以我們需要修改下JDK,選中項目右鍵----Build Path----->Configure Build Path...

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

切換到library頁籤,選中圖中所示的,點選Edit...

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

選擇工作空間的JDK1.8

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

然後可以發現我們的項目結構已經發生改變了。

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

7.修改項目配置

切換到Navigator視圖

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

然後可以看到

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

将原來的1.5 改成1.8

org.eclipse.jdt.core.prefs修改前

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.5           

org.eclipse.jdt.core.prefs修改後

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8           

8.pom.xml 修改如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xingyun</groupId>
  <artifactId>helloworld_web_maven_sample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>helloworld_web_maven_sample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
        <servlet.version>3.1.0</servlet.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>helloapp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <!-- server、username、password對應maven的setting下的配置 -->
                    <server>tomcat</server>
                    <username>admin</username>
                    <password>admin</password>
                    <path>/${project.build.finalName}</path>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>           

9.選中項目執行安裝依賴

mvn install           

當看到Build Success 說明成功

9.運作項目,指令執行後我們的項目就運作到tomcat7中了

選中項目右鍵-----Run As------>maven build...

tomcat7:run           
版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

10.然後打開浏覽器通路網站即可

http://localhost:8080/helloapp/
版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

附錄一

感謝你耐心閱讀本篇,最後送上一個神器指令,這個指令可幫助我們解決Maven依賴沖突.

使用場景,

比如A-1.2.jar 包依賴于 B-0.1.jar ,

然而項目中已經引入了一個較高版本B-1.1.jar,

我們想要的結果是隻有A-1.2.jar 和B-1.1.jar

首先通過這個Maven照妖鏡指令查找Maven依賴樹

mvn dependency:tree           

當執行成功後可以看到這個

版本依賴管理之 Maven 修煉手冊0x01 為什麼使用版本依賴工具Maven?0x02 Maven的下載下傳安裝配置0x03 Eclipse中的Maven使用附錄一附錄二附錄三附錄四

其中,org.apache.ws.xmlschema 是 group id,

xmlschema-core: 是artifactId

jar: 指的壓縮類型

2.2.1 指的是版本号

<dependency>
            <groupId>xml-resolver</groupId>
            <artifactId>xml-resolver</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.ws.xmlschema</groupId>
                    <artifactId>xmlschema-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>           

exclusions的意思是移除自身傳遞的依賴jar包

附錄二

再學一招,除了剛才的方法外,我們還可以指定全局統一版本的方法

這個配置的作用不會下載下傳依賴,僅僅用來統一版本号

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>antlr</groupId>
                <artifactId>antlr</artifactId>
                <version>2.7.7</version>
            </dependency>
            </dependencies>
        </dependencyManagement>           

附錄三

配置項目本地私有包

<dependency>
            <groupId>com.xingyun</groupId>
            <artifactId>test</artifactId>
            <version>1.0</version>
            <scope>system</scope>
                    <systemPath>${project.basedir}/src/main/resources/lib/test-1.0.jar</systemPath>
</dependency>           

注:

${project.basedir}是Maven内置屬性,

内置屬性(Maven預定義,使用者可以直接使用)
  • ${basedir} 表示項目根目錄,即包含pom.xml檔案的目錄;
  • ${version} 表示項目版本;
  • ${project.basedir} 同${basedir};
  • ${project.baseUri} 表示項目檔案位址;
  • ${maven.build.timestamp} 表示項目構件開始時間;
  • ${maven.build.timestamp.format} 表示屬性${maven.build.timestamp}的展示格式,預設值為yyyyMMdd-HHmm,可自定義其格式,其類型可參考java.text.SimpleDateFormat。用法如下:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>           

附錄四

Maven 生命周期

validate,

initialize,

generate-sources,

process-sources,

generate-resources,

process-resources,

compile,

process-classes,

generate-test-sources,

process-test-sources,

generate-test-resources,

process-test-resources,

test-compile,

process-test-classes,

test,

prepare-package,

package,

pre-integration-test,

integration-test,

post-integration-test,

verify,

install,

deploy,

pre-clean,

clean,

post-clean,

pre-site,

site,

post-site,

site-deploy.           

本篇完~

繼續閱讀