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的聚合与继承

相关文章
相关标签/搜索