maven分模块构建项目工程

                         分模块构建工程

基于上边的三个工程分析,我们将持久层,业务层、控制器和试图表现层可以分为三个不同的模块来处理,创建一个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插件,运行的端口和路径以子工程为准。

猜你喜欢

转载自blog.csdn.net/qq_15204179/article/details/83785604
今日推荐