maven模块拆分与应用

pojo拆分
  • 新建模块
  • 实体类domain
  • 配置文件(无)
  • pom文件添加一下内容
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
dao拆分
  • dao接口, mybatisConfig配置文件, jdbc配置文件, spring-mvc.xml, applicationContext.xml

  • pom文件中Tomcat插件删掉

  • 去掉springmvc的包

  • pom中导入pojo

        <!--导入资源pojo-->
        <dependency>
            <groupId>pojo</groupId>
            <artifactId>pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  • 如果编译失败的话,需要把dao模块install到仓库里面,然后再次compile,

​ 因为maven往仓库里找不到jar包,需要pojo编译后jar包才会在仓库出现

  • applicationContext.xml配置文件中去掉开启事务的标签
service拆分
  • service层,
  • applicationContext.xml, 因为service中有bean,所以需要这一层
  • pom中导入dao
        <!--导入dao资源文件-->
        <dependency>
            <groupId>dao</groupId>
            <artifactId>dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  • applicationContext.xml中去掉proerties,datasource,Mybatis整合

  • compile模块成功后要install模块

    controller拆分
    • 保留controller层数据
    • pom文件中导入service的依赖
    • 编译无误后,拉通所有模块用controller层的Tomcat编译一次

聚合

C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images

作用:方便批量进行项目的打包等操作,快速构建maven项目,一次性构建多个项目/模块

没有编程的任何东西,就是工程维护的管理

  • 创建一个空模块,打包类型定义为pom
<packaging>pom</packaging>
  • 定义当前模块进行构建操作时关联的其他模块名称
//../表示上一级目录
<modules>
<module>../ssm_controller</module>
<module>../ssm_service</module>
<module>../ssm_dao</module>
<module>../ssm_pojo</module>
</modules>
  • 测试成功的效果

​ 依赖顺序永远如下图所示
在这里插入图片描述

  • 每一个建立的模块,如果没有写打包方式,默认是jar包方式,

war:web工程

pom:聚合工程

继承

在这里插入图片描述

统一管理子项目的资源

作用:通过继承可以实现在子工程中沿用父工程的配置

与java中的继承相似,在子工程中配置继承关系

  • 在子工程中声明其父工程依赖与对应的位置

​ 父工程的pom文件填写相对路径

在这里插入图片描述

  • 在父工程中定义依赖管理

​ spring环境就是所需要的所有jar包

在这里插入图片描述

添加上其他模块的pom依赖(pojo,dao,service),并且删掉子模块中自己依赖的版本号
在这里插入图片描述

  • 在子工程中定义父工程的依赖关系时,无需声明依赖版本,版本参照父工程中依赖的版本

在这里插入图片描述

聚合与继承的区别

在这里插入图片描述

属性:简化配置过程

在这里插入图片描述

每个项目的父工程必须导入一下三个属性

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

1:解决乱码问题

2:指定编译时用的JDK版本

6类属性

  • 自定义属性
  • 内置属性 projecet
  • 系统属性 mvn help:system
  • 环境变量 mvn help:system
  • setting属性 settings
  • 插件属性 (maven的插件)

在这里插入图片描述

部分代码

//主模块代码  
<groupId>SSM1</groupId>
    <artifactId>SSM1</artifactId>
    <version>1.0-SNAPSHOT</version>
    //子模块代码
           <dependency>
            <groupId>dao</groupId>
            <artifactId>dao</artifactId>
            <version>${version}</version>
        </dependency>


        <dependency>
            <groupId>service</groupId>
            <artifactId>service</artifactId>
            <version>${version}</version>
        </dependency>
版本管理

介绍

在这里插入图片描述

资源配置

在这里插入图片描述

    <!--定义自定义属性-->
    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
        <jdbc.url>jdbc:mysql://localhost:3306/db10</jdbc.url>
    </properties>

在这里插入图片描述

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
#jdbc.url=jdbc:mysql://localhost:3306/db10
jdbc.username=root
jdbc.password=root

参与配置

指定参与文件的目录

在这里插入图片描述

多环境开发配置
多环境兼容

在这里插入图片描述

创建多环境

  • properties:当前环境变量所带的值

在这里插入图片描述

在这里插入图片描述

  • 把properties的内容独立到

在这里插入图片描述

设置默认启动

在这里插入图片描述

加载指定环境

mvn 指令 –P 环境定义id
//范例
mvn install –P pro_env
跳过测试的三种方式

应用场景
在这里插入图片描述

  1. mvn 指令 –D skipTests
  2. mvn clean package -DskipTests=true
  3. 配置文件跳过测试

在这里插入图片描述

3.指定跳过测试

在这里插入图片描述

4.使用界面操作跳过测试

在这里插入图片描述

私服

安装:

  • cmd启动:nexus /run nexus

  • 浏览器访问私服:http://localhost:8081

  • 配置

在这里插入图片描述

私服资源获取

在这里插入图片描述

仓库分类

宿主仓库:存储私服自己的jar包

代理仓库:代理请求转发到中央仓库,进行下载

在这里插入图片描述

本地仓库访问私服
同事分享

maven指令:

输出完整的错误信息:mvn clean -x package

CMD命令:JSP:扫描当前电脑上运行的java代码的pid

  • 定位工作目录: .git
  • pom工作目录: .pom

附完整版代码:

SSM1的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>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--定义该工程用于进行构建管理-->
    <packaging>pom</packaging>

    <!--管理的工程列表-->
    <modules>
        <!--具体的工程名称-->
        <module>../ssm_controller</module>
        <module>../ssm_service</module>
        <module>../ssm_dao</module>
        <module>../ssm_pojo</module>
    </modules>

    <!--创建多环境-->
    <profiles>
        <!--定义具体的环境:生产环境-->
        <profile>
            <!--定义环境对应的唯一名称-->
            <id>pro_env</id>
            <!--定义环境中换用的属性值-->
            <properties>
                <jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
            </properties>
            <!--设置默认启动-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--定义具体的环境:开发环境-->
        <profile>
            <id>dep_env</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db</jdbc.url>
            </properties>
        </profile>
    </profiles>

    <!--定义自定义属性-->
    <properties>
        <spring.version>5.1.9.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
        <!--<jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url>-->
    </properties>

    <!--声明此处进行依赖管理-->
    <dependencyManagement>
        <!--具体的依赖-->
        <dependencies>
            <!--添加自己的工程模块依赖-->
            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_pojo</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_dao</artifactId>
                <version>${version}</version>
            </dependency>

            <dependency>
                <groupId>com.itheima</groupId>
                <artifactId>ssm_service</artifactId>
                <version>${version}</version>
            </dependency>


            <!--spring环境-->
            <!--spring环境-->
            <!--spring环境-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>


            <!--mybatis环境-->
            <!--mybatis环境-->
            <!--mybatis环境-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.3</version>
            </dependency>
            <!--mysql环境-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <!--spring整合jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!--spring整合mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.3</version>
            </dependency>
            <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.16</version>
            </dependency>
            <!--分页插件坐标-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>

            <!--springmvc环境-->
            <!--springmvc环境-->
            <!--springmvc环境-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!--jackson相关坐标3-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.0</version>
            </dependency>
            <!--servlet环境-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>


            <!--其他组件-->
            <!--其他组件-->
            <!--其他组件-->
            <!--junit单元测试-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!--spring整合junit-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <build>
        <pluginManagement>
            <!--设置插件-->
            <plugins>
                <!--具体的插件配置-->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <port>80</port>
                        <path>/</path>
                    </configuration>
                </plugin>
                <!--配置跳过测试-->
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                    <!--<configuration>
                        &lt;!&ndash;设置跳过测试&ndash;&gt;
                        <skipTests>true</skipTests>
                    </configuration>-->
                    <configuration>
                        <includes>
                            <!--包含指定的用例-->
                            <include>**/User*Test.java</include>
                        </includes>
<!--                        <excludes>-->
<!--                            &lt;!&ndash;排除&ndash;&gt;-->
<!--                            <exclude></exclude>-->
<!--                        </excludes>-->
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <!--配置资源文件对应的信息-->
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <!--配置测试资源文件对应的信息-->
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>

    <!--发布配置管理-->
    <distributionManagement>
        <repository>
            <id>heima-release</id>
            <url>http://localhost:8081/repository/heima-release/</url>
        </repository>
        <snapshotRepository>
            <id>heima-snapshots</id>
            <url>http://localhost:8081/repository/heima-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

</project>

controller层代码

<?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">
    <!--定义该工程的父工程-->
    <parent>
      <groupId>com.itheima</groupId>
      <artifactId>ssm</artifactId>
      <version>1.0-SNAPSHOT</version>
      <!--填写父工程的pom文件-->
      <relativePath>../ssm/pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>ssm_controller</artifactId>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>ssm_service</artifactId>
        </dependency>

        <!--springmvc环境-->
        <!--springmvc环境-->
        <!--springmvc环境-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <!--jackson相关坐标3-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <!--servlet环境-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <!--设置插件-->
        <plugins>
            <!--具体的插件配置-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值