02 Maven

lix_uan
• 阅读 358

安装配置

  • 解压

  • 配置环境变量 MAVEN_HOME为安装路径的bin目录

  • 修改本地仓库

    • 修改 conf/settings.xml 中的 localRepository 为一个指定目录
  • 配置阿里云私服

    • 修改 conf/settings.xml 中的 mirrors 标签,为其添加如下子标签
    <mirror>  
        <id>alimaven</id>  
        <name>aliyun maven</name>  
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>          
    </mirror>

分模块开发与设计

  • 将一个项目拆分成若干个子模块

  • 通过指令安装模块到本地仓库(团队内部开发需要发布模块功能到私服中)

    mvn install

依赖管理

  • 依赖具有传递性

    • 直接依赖
    • 间接依赖
    • 特殊优先:当同级配置了相同资源的不同版本,后配置的覆盖先配置的
  • A依赖B,B依赖C,如果A不想将C依赖进来

    • 可选依赖:对外隐藏当前所依赖的资源,隐藏后对应资源将不具有依赖传递性

      <dependency>
          <groupId>cn.lixuan</groupId>
          <artifactId>maven</artifactId>
          <version>1.0-SNAPSHOT</version>
          <!--可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性-->
          <optional>false</optional>
      </dependency>
    • 排除依赖:主动断开依赖的资源,被排除的资源无需指定版本

      <dependency>
          <groupId>cn.lixuan</groupId>
          <artifactId>maven</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <exclusions>
              <exclusion>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
              </exclusion>
              <exclusion>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis</artifactId>
              </exclusion>
          </exclusions>
      </dependency>

聚合与继承

  • 聚合(同步构建)

    <!--父工程设计聚合所包含的子模块的名称-->
    <packaging>pom</packaging>
    
    <modules>
        <module>../maven_ssm</module>
        <module>../maven_pojo</module>
        <module>../maven_dao</module>
    </modules>
  • 继承(简化配置,减少版本冲突)

    <!--在子工程中引用父工程-->
    <parent>
        <groupId>cn.lixuan.maven</groupId>
        <artifactId>Parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <!--指定从当前pom.xml文件出发寻找父工程的pom.xml文件的相对路径-->
        <relativePath>../Parent/pom.xml</relativePath>
    </parent>
    <!--在父工程中管理依赖-->
    <packaging>pom</packaging>
    
    <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>

属性管理

  • 统一管理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>

多环境配置与应用

  • maven提供配置多种环境的设定,帮助开发者使用过程中快速切换环境

    <!--定义多环境-->
    <profiles>
        <!--定义具体的环境:生产环境-->
        <profile>
            <!--定义环境对应的唯一名称-->
            <id>env_dep</id>
            <!--定义环境中专用的属性值-->
            <properties>
                <jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url>
            </properties>
            <!--设置默认启动-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    
        <!--定义具体的环境:开发环境-->
        <profile>
            <id>env_pro</id>
            ……
        </profile>
    </profiles>
  • 使用多环境

    mvn install –P env_pro
  • 跳过测试,快速打包

    mvn install –D skipTests

私服(Nexus)

下载

  • 在maven的settings.xml中 mirrors 标签中配置, 先注释掉aliyun的配置

    <mirror>
        <id>nexus-xuan</id>
        <mirrorOf>*</mirrorOf>
        <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
  • 在nexus中设置允许匿名下载,如果私服中没有对应的jar, 可以配置让私服去阿里云中下载依赖

上传

  • settings.xml中配置本地仓库访问私服的权限

    <server>
      <!--id任意,多个server的id不重复就行,后面会用到-->
      <id>xuan-nexus</id>
      <username>admin</username>
       <!--填写自己nexus设定的登录密码-->
      <password>123456</password>
    </server>
  • 项目的pom.xml中配置当前项目访问私服上传资源的保存位置

    <distributionManagement>
        <repository>
              <!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
            <id>xuan-nexus</id>
              <!--如果jar的版本是release版本,那么就上传到这个仓库,根据自己情况修改-->
            <url>http://localhost:8081/repository/xuan-releases/</url>
        </repository>
        <snapshotRepository>
              <!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
            <id>xuan-nexus</id>
              <!--如果jar的版本是snapshot版本,那么就上传到这个仓库,根据自己情况修改-->
            <url>http://localhost:8081/repository/xuan-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
  • 发布资源到私服

    mvn deploy
点赞
收藏
评论区
推荐文章

暂无数据

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