分模块构建工程

基于上边的三个工程分析,我们将持久层,业务层、控制器和试图表现层可以分为三个不同的模块来处理,创建一个parent工程将通用的pom配置抽取出来,然后聚合多个模块运行。

3.1需求

3.1.1需求描述

将SSM工程拆分为多个模块开发:

Dao模块

Service模块

Web模块

3.1.2理解继承和聚合

通常继承和聚合同时使用。

  1. 何为继承?

继承是为了消除重复,如果将dao、service、web分开创建独立的工程则每个工程的pom.xml文件中的内容存在重复,如设置编译版本、锁定spring的版本的等,可以将这些重复的配置提取出来在父工程的pom.xml中定义。

  1. 何为聚合?

开发通常是分组分模块开发,每个模块开发完成要运行整个工程需要将每个模块聚合在一起运行,比如:dao、service、web三个工程最终会打一个独立的war运行。

3.2案例实现

3.2.1 ssm-parent父模块

3.2.1.1创建父工程

这里选择“跳过骨架选择”

定义坐标:

3.2.1.2定义pom.xml

在父工程的pom.xml中抽取一些重复的配置的,比如:锁定jar包的版本、设置编译版本等。

<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.igeekhome.ssm</groupId>

<artifactId>ssm-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

<properties>

<junit.version>4.12</junit.version>

<servlet-api.version>3.0-alpha-1</servlet-api.version>

<spring.version>4.3.6.RELEASE</spring.version>

<mybatis.version>3.4.2</mybatis.version>

<mybatis-spring.version>1.3.1</mybatis-spring.version>

<mysql.version>5.1.19</mysql.version>

<druid.version>1.0.28</druid.version>

<aspectjweaver.version>1.8.10</aspectjweaver.version>

<slf4j.version>1.7.24</slf4j.version>

<log4j.version>1.2.17</log4j.version>

<jstl.version>1.2</jstl.version>

</properties>

<!-- 添加工程的依赖 -->

<dependencyManagement>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>${junit.version}</version>

<scope>test</scope>

</dependency>

<!-- servlet-api JSP页面编译时需要的包 -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>${servlet-api.version}</version>

<scope>provided</scope>

</dependency>

<!-- Spring 以及 SpringMVC需要引入的包,自动引入需要参照的包 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<!-- 持久层的包 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<!-- Spring 和 Mybatis的整合包 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>${mybatis-spring.version}</version>

</dependency>

<!-- Mysql驱动包 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.version}</version>

</dependency>

<!-- druid数据库连接池 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>${druid.version}</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>${aspectjweaver.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<!-- 打日志的 -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${slf4j.version}</version>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>jcl-over-slf4j</artifactId>

<version>${slf4j.version}</version>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>${log4j.version}</version>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>${jstl.version}</version>

<scope>provided</scope>

</dependency>

</dependencies>

</dependencyManagement>

<build>

<finalName>ssm</finalName>

<resources>

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.xml</include>

<include>**/*.properties</include>

</includes>

</resource>

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.xml</include>

<include>**/*.properties</include>

<include>**/*.ini</include>

</includes>

</resource>

</resources>

<pluginManagement>

<plugins>

<!-- 设置编译版本为1.8 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<!-- Jetty插件,提供一种web容器 -->

<plugin>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-maven-plugin</artifactId>

<version>9.4.2.v20170220</version>

<configuration>

<httpConnector>

<!-- 配置运行的端口号 -->

<port>80</port>

</httpConnector>

<!-- 配置扫描的时间间隔 -->

<scanIntervalSeconds>1</scanIntervalSeconds>

<webApp>

<!-- 配置上下文 -->

<contextPath>/ssm</contextPath>

</webApp>

</configuration>

</plugin>

</plugins>

</pluginManagement>

</build>

</project>

3.2.1.3将父工程发布至仓库

父工程创建完成执行maven-install将父工程发布到仓库方便子工程继承:

3.2.2ssm-dao子模块

3.2.2.1创建dao子模块

选择ssm-parent工程添加模块

这里指定模块名称,选择“跳过骨架选择”,并设置模块名称

3.2.2.2定义pom.xml

dao模块的pom.xml文件中需要继承父模块,添加持久层需要的依赖坐标:

<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>

<parent>

<groupId>com.igeekhome.ssm</groupId>

<artifactId>ssm-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<artifactId>ssm-dao</artifactId>

<packaging>jar</packaging>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<!-- 持久层的包 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

</dependency>

<!-- Spring 和 Mybatis的整合包 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

</dependency>

<!-- Mysql驱动包 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

<!-- druid数据库连接池 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

</dependency>

</dependencies>

</project>

3.2.2.3dao

将ssm工程中的dao接口,mapper配置文件及domain类拷贝到src/main/java中:

3.2.2.4配置文件

拷贝ssm工程中如下配置文件到dao工程:

3.2.2.5单元测试

package com.igeekhome.ssm.tests;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.igeekhome.ssm.dao.EmployeeMapper;

import com.igeekhome.ssm.domain.Employee;

public class EmployeeTest {

@Test

public void testFindEmployeeById(){

//加载配置文件

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "spring-mybatis.xml"});

//获取dao

EmployeeMapper employeeMapper = applicationContext.getBean(EmployeeMapper.class);

//查询数据

Employee employee = employeeMapper.selectByPrimaryKey(7369);

//查看数据

System.out.println(employee.getEname());

//关闭上下文

applicationContext.close();

}

}

3.2.3ssm-service子模块

3.2.3.1创建service子模块

方法同ssm-dao模块创建方法,模块名称为ssm-service。

3.2.3.2定义pom.xml

service模块的pom.xml文件中需要继承父模块,service依赖dao模块:

<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>

<parent>

<groupId>com.igeekhome.ssm</groupId>

<artifactId>ssm-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<artifactId>ssm-service</artifactId>

<packaging>jar</packaging>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>com.igeekhome.ssm</groupId>

<artifactId>ssm-dao</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

</dependencies>

</project>

3.2.3.3service接口

将ssm工程中的service接口拷贝到src/main/java中:

3.2.3.4配置文件

拷贝ssm工程中如下配置文件到service工程:

3.2.3.5单元测试

package com.igeekhome.ssm.tests;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.igeekhome.ssm.domain.Employee;

import com.igeekhome.ssm.service.EmployeeService;

public class EmployeeTest {

@Test

public void testFindEmployeeById(){

//加载配置文件

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "spring-mybatis.xml"});

//获取dao

EmployeeService employeeService = applicationContext.getBean(EmployeeService.class);

//查询数据

Employee employee = employeeService.selectByPrimaryKey(7369);

//查看数据

System.out.println(employee.getEname());

//关闭上下文

applicationContext.close();

}

}

3.2.4ssm-web子模块

3.2.4.1创建web子模块

方法同ssm-dao模块创建方法,模块名称为ssm-web。

3.2.4.2定义pom.xml

<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>

<parent>

<groupId>com.igeekhome.ssm</groupId>

<artifactId>ssm-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<artifactId>ssm-web</artifactId>

<packaging>war</packaging>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<!-- servlet-api JSP页面编译时需要的包 -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<scope>provided</scope>

</dependency>

<!-- Spring 以及 SpringMVC需要引入的包,自动引入需要参照的包 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

</dependency>

<!-- 打日志的 -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>jcl-over-slf4j</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>com.igeekhome.ssm</groupId>

<artifactId>ssm-service</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

</dependencies>

<build>

<finalName>ssm</finalName>

<resources>

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.xml</include>

<include>**/*.properties</include>

</includes>

</resource>

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.xml</include>

<include>**/*.properties</include>

<include>**/*.ini</include>

</includes>

</resource>

</resources>

<plugins>

<!-- 设置编译版本为1.8 -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<!-- Jetty插件,提供一种web容器 -->

<plugin>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-maven-plugin</artifactId>

<configuration>

<httpConnector>

<!-- 配置运行的端口号 -->

<port>80</port>

</httpConnector>

<!-- 配置扫描的时间间隔 -->

<scanIntervalSeconds>1</scanIntervalSeconds>

<webApp>

<!-- 配置上下文 -->

<contextPath>/ssm</contextPath>

</webApp>

</configuration>

</plugin>

</plugins>

</build>

</project>

3.2.4.3Controller

将ssm工程中的controller拷贝到src/main/java中:

3.2.4.4配置文件

将ssm工程中的配置文件拷贝到src/main/resources中

将ssm工程中的WEB-INF中的文件拷贝到项目的webapp中

3.2.5运行调试

方法1:在maven-web工程的pom.xml中配置tomcat插件运行

运行maven-web工程它会从本地仓库下载依赖的jar包,所以当maven-web依赖的jar包内容修改了必须及时发布到本地仓库,比如:maven-web依赖的maven-service修改了,需要及时将maven-service发布到本地仓库。

方法2:在父工程的pom.xml中配置tomcat插件运行,自动聚合并执行

推荐方法2,如果子工程都在本地,采用方法2则不需要子工程修改就立即发布到本地仓库,父工程会自动聚合并使用最新代码执行。

注意:如果子工程和父工程中都配置了tomcat插件,运行的端口和路径以子工程为准。

maven分模块构建项目工程相关推荐

  1. 【Java从0到架构师】Maven - 依赖冲突、分模块构建项目

    SpringBoot - Maven 补充 依赖冲突 解决方案 - dependencyManagement 自定义属性 分模块构建项目 继承 - parent 聚合 - modules 依赖 - d ...

  2. 【SSM整合】SSM详细整合-maven分模块架构

    本文从一个简单的三层案例入手,分模块整合SSM框架,从环境搭建到测试,每一步都比较详细,希望能对你有所帮助. 文章难免存在错误,望大佬及时指教. 文章所有代码在文章末尾给出. 0x01.SSM整合说明 ...

  3. Maven分模块开发,执行指令失败:‘modules.module[5]‘ specifies duplicate child module health_jobs @ line 16, column

    问题描述: Maven工程分模块开发完成 父工程执行install命令后 控制台报出以下错误: 问题分析: 在父工程的pom.xml 文件中,指定了重复了子模块 <module>healt ...

  4. 1、maven笔记(一):构建项目

    1.在E:\下新建mvn目录,由于mvn默认搜索路径为src/main/java/目录下,所以我们在mvn下新建目录/src/main/java/com/zmp/mvn/helloworld/Hell ...

  5. 18、构建Maven的分模块的项目

    1.创建baowei-parent 4.2  pom.xml的配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...

  6. maven 分模块项目 主子pom之间的引入依赖关系

    在项目时用到maven管理项目,在一个就项目的基础上开发新的项目:关于子项目和父项目,子项目与子项目之间的调用问题,发现自己存在不足,以下是自己查询的问题,解决了自己的疑惑. 原文链接:https:/ ...

  7. dao层和service层和control_maven分模块构建SSM普通web项目:service层代码编写

    接着编写service层的代码. 1.service接口及其实现类 新建service包用于放service接口,还有,新建service.impl包放接口的实现类.如下图,BookService和其 ...

  8. Maven分模块管理时com.xx.xx.service等找不到

    记录报错,在创建模块时名字过于随意,遇事后面就稍作修改了下名字,结果出现了报错,没有报红但是却说service的东西找不到.然后发现,service模块下生成了两个imi文件,将其删掉重新编译即可. ...

  9. Maven学习总结(42)——Maven多模块构建中常用的参数

    一.多模块项目中需要使用不同的参数实现不同的构建目的,常用参数: -am --also-make:同时构建所列模块的依赖模块: -amd -also-make-dependents:同时构建依赖于所列 ...

最新文章

  1. opencv 图像访问索引
  2. vuex 的模块化+命名空间
  3. 如何用木板做桥_为这份动手能力点赞!旧木板打磨后做成橱柜,效果可媲美定制的...
  4. linux7简单应用,centos7下openTSDB简单应用
  5. WINDOWS操作系统32位(x86)和64位(x64)的区别
  6. [Python] 创建一个整数列表:range()
  7. 如何在 iPhone 和 iPad 上使用与你共享?
  8. windows | RDPWrap 远程桌面登录增强工具,长期提供多版本 rdpwrap.ini配置文件 [可灵活设置多人同时登录、一键改变配置]
  9. java怎么查看jar包_怎么查看Jar包源码?如何打开Jar文件?
  10. emoji android to iphone,Emoji Android to iphone
  11. SPSS的双变量相关分析
  12. 【SPSS笔记】主效应、交互效应
  13. 清代徽州家政与乡族社会的善治
  14. excel表格斜线_WORD圆角表格,如此惊艳
  15. QGIS官方样式库首现来自中国的贡献——国土空间规划样式库和分区配色表
  16. MySQL-基本概念与查询操作(DESC/SELECT/FROM/WHERE/LIKE)
  17. 机器学习理论: PAC学习
  18. Linux C 以read()读取文件并提取字符串
  19. 作家生涯人物访谈报告知乎_即使您不认为自己是作家,写作也会如何改善您的职业生涯
  20. Study-Python23-023递归:这帮小兔崽子

热门文章

  1. 中国篮球--路在何方!
  2. 防止***根据PHP中的错误信息爆路径
  3. 什么叫做GATWAY,DNS,DHCP?
  4. DHCP服务器控制企业成本
  5. ExtJS实战(4)-struts
  6. 201521460005 实验五
  7. U盘插入电脑后,提示需要格式化U盘如何解决?
  8. MFC的凸包实例【赶紧进来膜拜】
  9. JDK5.0新特性系列---11.5.4线程 同步装置之Exchanger
  10. mysql备份单实例(一)shell