31 Maven

lix_uan
• 阅读 967

Maven本地仓库的配置

  • 配置环境变量

  • 查看是否配置成功

31 Maven

  • Maven默认的本地仓库 C:\Users\Administrator\.m2\repository

  • 更改 conf\settings.xml

    //更改仓库地址
    <localRepository>修改后的仓库地址</localRepository>
    //配置阿里云镜像
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>

在IDEA中使用Maven

31 Maven

在pom.xml中导入依赖

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>
</dependencies>

Maven生命周期

31 Maven

Maven的核心概念

约定的目录结构

  • 约定 > 配置 > 编码,意思是能基于约定的就是不进行配置 31 Maven

坐标

<groupId>cn.lixuan</groupId>    //公司域名倒序 + 当前项目名
<artifactId>test</artifactId>    //当前项目的模块名
<version>1.0-SNAPSHOT</version>    //当前模块的版本

依赖管理

  • A jar包需要用到B jar包中的类时,我们就说A对B有依赖

  • 直接依赖和间接依赖

  • 依赖的范围

    • compile:主程序、测试程序和在服务器运行时都需要用到
    • test:仅测试程序的时候需要,例如 junit
    • provided:服务器不需要,例如 servlet-api在服务器上运行时,Servlet容器会提供相关API,所以部署的时候不需要
  • 依赖的作用范围可以概括为下图

    31 Maven

依赖的传递性

  • 存在间接依赖情况时,只有当间接依赖的jar包依赖范围为compile时才可以访问

解决jar包冲突

  • 路径最短者优先
  • 路径相同时先声明者优先(dependency标签配置的先后顺序)

依赖排除

  • 为了确保程序正确,可以将有可能重复的间接依赖排除

    <dependency>
        <!--依赖排除-->
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.2</version>
    </dependency>
    

统一管理jar包版本

<properties>
    <spring.version>4.0.0.RELEASE</spring.version>
</properties>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>

继承

在子工程中引用父工程

<!--继承-->
<parent>
    <groupId>cn.lixuan.maven</groupId>
    <artifactId>Parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--指定从当前pom.xml文件出发寻找父工程的pom.xml文件的相对路径-->
    <relativePath>../Parent/pom.xml</relativePath>
</parent>

在父工程中管理依赖

<!--依赖管理-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

子项目中重新指定需要的依赖,删除范围和版本号

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
</dependency>

聚合

  • 将工程拆分为多个模块后,需要逐个操控,而使用聚合后可以批量操作

配置聚合

  • 在总的聚合工程中配置

    <!--聚合-->
    <modules>
        <module>../MakeFriend</module>
        <module>../OurFriends</module>
        <module>../HelloFriend</module>
        <module>../Hello</module>
    </modules>
点赞
收藏
评论区
推荐文章

暂无数据

lix_uan
lix_uan
Lv1
学无止境,即刻前行
文章
7
粉丝
5
获赞
0