Maven项目管理工具

一.Maven的简介

1.1 什么是maven

 mavenapache下的一个开源项目,是纯java开发,并且只是用来管理java项目的

1.2 Maven好处

1. jar包的统一管理,可以节省空间

2.一键构建编码,编译,测试(junit),运行,打包,部署

3.可以跨平台

4.运用于大型项目提高效率

1.3 依赖管理

 

 

2.Maven的安装配置

2.1 下载安装

2.2 Maven环境变量配置

1、 要配置jdk,  maven3.3.9这个版本所需的jdk版本必须要1.7以上

2、 最终要运行的是maven软件中bin目录的mvn命令

所以要配置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.24、spring4.2.4、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的聚合与继承

posted @ 2018-05-19 11:56  薛定谔病态猫  阅读(2111)  评论(0编辑  收藏  举报