Maven項目管理工具

一.Maven的簡介

1.1 什麼是maven

 mavenapache下的一個開源項目,是純java開發,而且只是用來管理java項目的html

1.2 Maven好處

1. jar包的統一管理,能夠節省空間java

2.一鍵構建編碼,編譯,測試(junit),運行,打包,部署spring

3.能夠跨平臺apache

4.運用於大型項目提升效率tomcat

1.3 依賴管理

 

 

2.Maven的安裝配置

2.1 下載安裝

2.2 Maven環境變量配置

一、 要配置jdk,  maven3.3.9這個版本所需的jdk版本必需要1.7以上框架

二、 最終要運行的是maven軟件中bin目錄的mvn命令ssh

因此要配置maven的環境變量maven

在系統變量添加測試

環境變量的名稱:MAVEN_HOME編碼

變量值:就是maven軟甲解壓的目錄F:\class32\apache-maven-3.3.9

 3、把MAVEN_HOME添加到path

4、驗證maven是否配置成功:

打開dos窗口 輸入: mvn –v

2.3 Maven倉庫

三種倉庫

1、本地倉庫 本身維護

本地倉庫的配置只須要修改settings.xml文件就能夠

 

2、遠程倉庫(私服) 公司維護

3、中央倉庫 maven團隊維護

三種倉庫的關係以下:

 

三.入門程序

3.1 Maven的目錄結構

 

3.2 Maven的經常使用命令

1.mvn clean   清理編譯的文件

2.mvn compile 編譯了主目錄的文件

3.mvn test  編譯並運行了test目錄的代碼

4.mvn package 打包

5.Install 就是把項目發佈到本地倉庫

6.tomcatrun  一鍵啓動

3.3 Maven的生命週期(瞭解)

Compile   test  package  install  deploy(發佈到私服)

三種生命週期

Clean生命週期

 Clean

Default生命週期

Compile   test  package  install  deploy

Site生命週期

 Site

四.Maven整合ssh框架

4.1 依賴傳遞

只添加了一個struts2-core依賴,發現項目中出現了不少jar,這種狀況 依賴傳遞

4.2 依賴版本衝突的解決

1) 第一聲明優先原則

<!-- spring-beans-4.2.4 -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.2.4.RELEASE</version>
      </dependency>
  
  
<!-- spring-beans-3.0.5 -->
      <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-spring-plugin</artifactId>
          <version>2.3.24</version>
      </dependency>

2) 路徑近者優先原則

<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>4.2.4.RELEASE</version>
      </dependency>

3) 排除原則

<dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-spring-plugin</artifactId>
          <version>2.3.24</version>
          <exclusions>
            <exclusion>
              <groupId>org.springframework</groupId>
              <artifactId>spring-beans</artifactId>
            </exclusion>
          </exclusions>
      </dependency>

4)版本鎖定原則

<properties>
        <spring.version>4.2.4.RELEASE</spring.version>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <struts.version>2.3.24</struts.version>
    </properties>

    <!-- 鎖定版本,struts2-2.3.2四、spring4.2.四、hibernate5.0.7 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
</dependencies>
</dependencyManagement>

 

五.分模塊開發

5.1 依賴範圍對依賴傳遞形成的影響(瞭解)

父工程來管理   聚合

六.私服 nexus

安裝nexus

啓動服務

啓動失敗的解決方法:

 

登陸nexus

用戶名/密碼  admin/admin123

倉庫類型

Virtual   虛擬倉庫

Proxy  代理倉庫

Hosted  宿主倉庫  本地倉庫

Group

需求:

dao放到私服上,而後service從私服上下載

Virtual   虛擬倉庫

Proxy  代理倉庫

Hosted  宿主倉庫  本地倉庫

Group

需求 :將ssh_dao的這個工程打成jar包,並放入到私服上去.

6.1上傳dao

第一步: 須要在客戶端即部署dao工程的電腦上配置 maven環境,並修改 settings.xml 文件,配置鏈接私服的用戶和密碼 。

此用戶名和密碼用於私服校驗,由於私服須要知道上傳都 的帳號和密碼 是否和私服中的帳號和密碼一致

<server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>

 

第二步: 配置項目pom.xml

配置私服倉庫的地址,本公司的本身的jar包會上傳到私服的宿主倉庫,根據工程的版本號決定上傳到哪一個宿主倉庫,若是版本爲release則上傳到私服的release倉庫,若是版本爲snapshot則上傳到私服的snapshot倉庫。

 

<distributionManagement>
      <repository>
          <id>releases</id>
    <url>http://localhost:8081/nexus/content/repositories/releases/</url>
      </repository> 
      <snapshotRepository>
          <id>snapshots</id>
    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
      </snapshotRepository> 
  </distributionManagement>

注意:pom.xml這裏<id> settings.xml 配置 <id> 對應!

第三步:執行deploy命令發佈到私服

6.2下載dao

第一步 修改settings.xml

<profile>   
    <!--profile的id-->
    <id>dev</id>   
    <repositories>   
      <repository>  
        <!--倉庫id,repositories能夠配置多個倉庫,保證id不重複-->
        <id>nexus</id>   
        <!--倉庫地址,即nexus倉庫組的地址-->
        <url>http://localhost:8081/nexus/content/groups/public/</url>   
        <!--是否下載releases構件-->
        <releases>   
          <enabled>true</enabled>   
        </releases>   
        <!--是否下載snapshots構件-->
        <snapshots>   
          <enabled>true</enabled>   
        </snapshots>   
      </repository>   
    </repositories>  
     <pluginRepositories>  
        <!-- 插件倉庫,maven的運行依賴插件,也須要從私服下載插件 -->
        <pluginRepository>  
            <!-- 插件倉庫的id不容許重複,若是重複後邊配置會覆蓋前邊 -->
            <id>public</id>  
            <name>Public Repositories</name>  
            <url>http://localhost:8081/nexus/content/groups/public/</url>  
        </pluginRepository>  
    </pluginRepositories>  
  </profile>

 

<activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>

第二步 刪除本地倉庫中的dao

第三步 update service工程,出現如下信息說明已經成功

 

 

文章推薦:

1.maven的聚合與繼承

相關文章
相關標籤/搜索