Maven入门到掌握(一套打通任督二脉)

1:分模块开发

  • 意义:

  • 步骤

    • 创建新模块

    • 在各个模块中导入所需要的那个模块的依赖,例

      a<!--    依赖pojo模块-->
      
          <dependency>
            <groupId>com.ysj</groupId>
            <artifactId>maven_03_pojo</artifactId>
              <version>1.0-SNAPSHOT</version>
          </dependency>
      
    • 将要导入的模块安装(install)到本地仓库,否则编译都过不了

在这里插入图片描述

2:依赖管理

  • 依赖传递

    • 依赖具有传递性
      • 直接依赖
      • 间接依赖
    • 依赖注入冲突问题
      • 当同一个坐标配置了多个版本的依赖,后面会覆盖前面的
        在这里插入图片描述
  • 可选依赖(不透明性):隐藏当前工程所依赖的资源,隐藏后对应的资源不具有传递性

        <dependency>
            <groupId>com.ysj</groupId>
            <artifactId>maven_03_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
    <!--        隐藏依赖配置-->
            <optional>true</optional>
    
  • 排除依赖(不需要):隐藏当前以来对应的资源关系,无需指定版本

    <!--      依赖dao模块-->
          <dependency>
              <groupId>com.ysj </groupId>
              <artifactId>maven_04_dao</artifactId>
              <version>1.0-SNAPSHOT</version>
              <exclusions>
    <!--              隐藏当前以来对应的资源关系,无需指定版本-->
                  <exclusion>
                      <groupId>mysql</groupId>
                      <artifactId>mysql:mysql-connector-java</artifactId>
                  </exclusion>
              </exclusions>
          </dependency>
    

3:继承与聚合

3.1:聚合

  • 定义:将多个模块组织成一个整体,同时进行项目构建

  • 目的:为了让统一管理各个模块,当其中有模块更新时,让多个模块同时更新

  • 聚合工程

    • 新建一个模块,该模块只需一个pom.xml文件

      • 其打包方式为pom

        <groupId>com.ysj</groupId>
        <artifactId>maven_01_parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        
    • pom.xml中进行模块管理

      <modules>
          <module>../maven_03_pojo</module>
          <module>../maven_04_dao</module>
          <module>../../../../springmvc_08_ssm</module>
      </modules>
      
    • 无需管上边儿module的书写顺序,实际聚合中,程序会根据依赖关系逐步构建

3.2:继承

  • 类似java

  • 聚合与继承一般在一个模块中

  • 作用

    • 简化配置
    • 减少版本冲突
  • <!--    配置当前工程继承自parent工程-->
        <parent>
            <groupId>com.ysj</groupId>
            <version>1.0-SNAPSHOT</version>
            <artifactId>maven_01_parent</artifactId>
            <!--填写父工程的pom文件-->
            <relativePath>../maven_01_parent/pom.xml</relativePath>
        </parent>
    
  • 在父模块中定义依赖管理

    • <!--定义依赖管理-->
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>junit</groupId>
                      <artifactId>junit</artifactId>
                      <version>4.12</version>
                      <scope>test</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      
    • 此时子模块中可自行选择是否使用该依赖,若使用,则不用指定版本号,默认使用父模块的版本

3.3:继承和聚合的区别

在这里插入图片描述

4:属性

4.1:属性

  • 定义属性
<!--定义属性:变量名为spring.version,版本号为5.2.10.RELEASE-->
<properties>
<spring.version>5.2.10.RELEASE</spring.version>
</properties>
  • 使用属性(使用${})

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    

4.2:配置文件加载属性

        <build>
            <resources>
                <resource>
<!--${project.basedir}表示当前项目目录,因为子项目继承了parent,所以子项目也有该功能-->
                    <directory>${project.basedir}/src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>

  • 打包时要配置maven打war包,此时要忽略web.xml的检查

    • 在web项目下新建个空web.xml文件

    • 在web项目下的pom.xml中定义插件

      </plugin>
      
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.6.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
        </plugin>
      

4.3:版本管理

在这里插入图片描述

6:多环境开发

6.1:配置多环境

  • 通过id进行区分
<!--    配置多环境-->
        <profiles>
<!--        开发环境-->
            <profile>
                <id>env_dep</id>
                <properties>
                    <jdbc.url>jdbc:mysql://127.0.0.1:3306/mybatis</jdbc.url>
                </properties>
                <!--设置默认开发环境-->
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
            </profile>
<!--            生产环境-->
            <profile>
                <id>env_pro</id>
                <properties>
                    <jdbc.url>jdbc:mysql://127.1.1.1:3306/mybatis</jdbc.url>
                </properties>
            </profile>
            <!--            测试环境-->
            <profile>
                <id>env_test</id>
                <properties>
                    <jdbc.url>jdbc:mysql://127.2.2.2:3306/mybatis</jdbc.url>
                </properties>
            </profile>


        </profiles>

6.2:设置默认开发环境

  • 打包时用命令

    mvn -install -P 环境名
    

7:跳过测试

  • 当上线前有些功能还未开发完全,跳过测试可直接打包

  • 方法

    • 点这个闪电:但是这个的弊端是所有模块均跳过测试

    在这里插入图片描述

    • 配置插件跳过

    • <!--测试插件-->
         <plugins>
             <plugin>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>2.22.2</version>
                 <configuration>
                  <!--跳过测试选项-->
                     <skipTests>false</skipTests>
                     <!--排除掉不参与测试的内容-->
                 <excludes>
                     <exclude>**/BookServiceTest.java</exclude>
                 </excludes>
                 </configuration>
      
             </plugin>
         </plugins>
      
    • 指令跳过

      mvn package -D skipTest
      
           <excludes>
               <exclude>**/BookServiceTest.java</exclude>
           </excludes>
           </configuration>

       </plugin>
   </plugins>
```
  • 指令跳过

    mvn package -D skipTest
    

    关于私服的学习,后续会更新。

创作不易,六个三连+关注吧
  • 48
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 74
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 74
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Duck&踏风彡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值