Maven高级

目录

分模块开发

分模块开发实现

创建新模块

建立依赖关系

将项目安装本地仓库

依赖管理

依赖传递与冲突问题

可选依赖和排除依赖

可选依赖

聚合和继承

聚合

继承

属性

配置文件加载属性

版本管理

多环境配置与应用

多环境开发

跳过测试

IDEA工具实现跳过测试

方式二:配置插件实现跳过测试

方式三:命令行跳过测试


分模块开发

         项目中的每一层都可以单独维护,也可以很方便的被别人使用。关于分模块开发的意义

分模块开发实现

创建新模块

建立依赖关系

<dependency>
    <groupId>com.itheima</groupId>
    <artifactId>maven_03_pojo</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

将项目安装本地仓库

 

依赖管理

依赖指当前项目运行所需的jar,一个项目可以设置多个依赖。

<!--设置当前项目所依赖的所有jar-->
<dependencies>
    <!--设置具体的依赖-->
    <dependency>
        <!--依赖所属群组id-->
        <groupId>org.springframework</groupId>
        <!--依赖所属项目id-->
        <artifactId>spring-webmvc</artifactId>
        <!--依赖版本号-->
        <version>5.2.10.RELEASE</version>
    </dependency>
</dependencies>

依赖传递
可选依赖
排除依赖

依赖传递与冲突问题
 

        有两个maven_03_pojo的依赖被加载到Dependencies

        把maven_02_ssm项目中pom.xml关于maven_03_pojo的依赖注释或删除
掉,可以被正常使用,这个特性其实就是
依赖传递

 

        A代表自己的项目;B,C,D,E,F,G代表的是项目所依赖的jar包;D1和D2 E1和E2代表是相同
jar包的不同版本

        依赖传递有直接依赖间接依赖

相对于A来说,A直接依赖B和C,间接依赖了D1,E1,G,F,D2和E2

因为有依赖传递的存在,就会导致jar包在依赖的过程中出现冲突问题,依赖冲突

特殊优先:当同级配置了相同资源的不同版本,后配置的覆盖先配置的

路径优先:当依赖中出现相同的资源时,层级越深,优先级越低,层级越浅,优先级越高

声明优先:当资源在相同层级被依赖时,配置顺序靠前的覆盖配置顺序靠后的

        最终的结果都会在Maven的Dependencies面板中展示出来,展示的是哪个版本,也就是说它选择的就是哪个版本

 全面的查看Maven中各个坐标的依赖关系,可以点击Maven面板中的show Dependencies

可选依赖和排除依赖

可选依赖

        可选依赖是在B上设置<optional>,A不知道有C的存在

<dependency>
    <groupId>com.itheima</groupId>
    <artifactId>maven_03_pojo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递-->
    <optional>true</optional>
</dependency>

排除依赖

        排除依赖是在A上设置<exclusions>,A知道有C的存在,主动将其排除掉

    <!--依赖dao运行-->
    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>maven_04_dao</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!--排除依赖是隐藏当前资源对应的依赖关系-->
      <exclusions>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

聚合和继承

聚合

所谓聚合:将多个模块组织成一个整体,同时进行项目构建的过程称为聚合

聚合工程:通常是一个不具有业务功能的"空"工程(有且仅有一个pom文件)

 将项目的打包方式改为pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <packaging>pom</packaging>

</project>

jar:默认情况,说明该项目为java项目
war:说明该项目为web项目
pom:说明该项目为聚合或继承(后面会讲)项目

pom.xml添加所要管理的项目

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <packaging>pom</packaging>

    <!--设置管理的模块名称-->
    <modules>
        <module>../maven_02_ssm</module>
        <module>../maven_03_pojo</module>
        <module>../maven_04_dao</module>
    </modules>

</project>

 使用聚合统一管理项目


        聚合工程主要是用来管理项目,聚合工程管理的项目在进行运行的时候,会按照项目与项目之间的依赖关系来自动决定执行的顺序和配置的顺序无关

继承

重复配置的问题

spring-webmvc、spring-jdbc在三个项目模块中都有出现,这样就出现了重复的内容

所谓继承:子工程可以继承父工程中的配置信息,常见于依赖关系的继承

子项目:

  <!--配置当前工程继承自parent工程-->
  <parent>
    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <relativePath>../maven_01_parent/pom.xml</relativePath>
  </parent>

父工程:

?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <packaging>pom</packaging>

    <!--设置管理的模块名称-->
    <modules>
        <module>../maven_02_ssm</module>
        <module>../maven_03_pojo</module>
        <module>../maven_04_dao</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</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-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
</project>

 优化子项目依赖版本问题

        如果把所有用到的jar包都管理在父项目的pom.xml,看上去更简单些,但是这样就会导致有很多项目引入了过多自己不需要的jar包

        导致ssm_order项目中多引入了springtest的jar包,如果这样的jar包过多的话,对于ssm_order来说也是一种"负担"

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

<dependencyManagement>标签不真正引入jar包,而是配置可供子项目选择的jar包依赖

子项目要想使用它所提供的这些jar包,需要自己添加依赖,并且不需要指定<version>

在maven_02_ssm的pom.xml添加junit的依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

        这里就不需要添加版本了,这样做的好处就是当父工程dependencyManagement标签中的版
本发生变化后,子项目中的依赖版本也会跟着发生变化

属性

<!--定义属性-->
    <properties>
        <spring.version>5.2.10.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
        <!--<jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url>-->
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</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-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

配置文件加载属性

父工程定义属性

<properties>
    <jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
</properties>

 jdbc.properties文件中引用属性

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=root
jdbc.password=root

设置maven过滤文件范围

  <build>
        <resources>
            <!--设置资源目录,并设置能够解析${}-->
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>

 

打包的过程中如果报如下错误:

        原因就是Maven发现你的项目为web项目,就会去找web项目的入口web.xml[配置文件配置的方式,发现没有找到,就会报错

在Maven中的属性分为

 具体查看这些属性:在cmd命令行中输入mvn help:system

版本管理

SNAPSHOT(快照版本)

项目开发过程中临时输出的版本,称为快照版本

RELEASE(发布版本)

项目开发到一定阶段里程碑后,向团队外部发布较为稳定的版本,这种版本所对应的构件文件
是稳定的

多环境配置与应用

多环境开发

        不同环境的配置是不相同的,如不可能让三个环境都用一个数据库,所以就会有三个数据库的url配置

        maven提供配置多种环境的设定,帮助开发者在使用过程中快速切换环境。具体实现步骤:

 父工程配置多个环境,并指定默认激活环境

 <!--配置多环境-->
    <profiles>
        <!--开发环境-->
        <profile>
            <id>env_dep</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
            </properties>
            <!--设定是否为默认启动环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--生产环境-->
        <profile>
            <id>env_pro</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db</jdbc.url>
            </properties>
        </profile>
        <!--测试环境-->
        <profile>
            <id>env_test</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.3.3.3:3306/ssm_db</jdbc.url>
            </properties>
        </profile>
    </profiles>

 执行安装查看env_dep环境是否生效

 

命令行实现环境切换

 

跳过测试

IDEA工具实现跳过测试

Toggle翻译为切换的意思,也就是说在测试与不测试之间进行切换。

        如果我们想更精细的控制哪些跳过哪些不跳过,就需要使用配置插件的方式。

方式二:配置插件实现跳过测试

    <build>
        <resources>
            <!--设置资源目录,并设置能够解析${}-->
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <!--排除掉不参与测试的内容-->
                    <excludes>
                        <exclude>**/BookServiceTest.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

skipTests:如果为true,则跳过所有测试,如果为false,则不跳过测试
excludes:哪些测试类不参与测试,即排除,针对skipTests为false来设置的
includes: 哪些测试类要参与测试,即包含,针对skipTests为true来设置的

方式三:命令行跳过测试

mvn -D skipTests



        执行的项目构建指令必须包含测试生命周期,否则无效果。例如执行compile生命周期,不经过test生命周期。
        该命令可以不借助IDEA,直接使用cmd命令行进行跳过测试,需要注意的是cmd要在pom.xml所在目录下进行执行


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值