1. 程式人生 > >maven分模組構建專案工程

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>

        <!-- Sprin