Maven 多模塊 + SSM框架

TestMultiModuleTest 為父項目,其余為子模塊
test-api 存放bean和service的接口,普通的maven模塊,沒有選擇archetype
test-provider 存放dao和service接口的實現,spring和mybatis的配置文件,普通的maven模塊,沒有選擇archetype。依賴于test-api模塊,需要在該模塊的pom.xml中添加
test-web 存放controller和springMVC的配置文件,選擇archetype為maven-archetype-webapp。依賴于test-api,test-provider模塊,需要在該模塊的pom.xml中添加

項目 TestMultiModuleTest 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.shpun</groupId>
    <artifactId>TestMultiModuleTest</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>test-web</module>
        <module>test-provider</module>
        <module>test-api</module>
    </modules>

    <dependencyManagement>
        <!-- 依賴管理,子模塊根據需要從父項目的獲取 -->
    </dependencyManagement>

</project>

模塊 test-api pom.xml 不變
模塊 test-provider 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">
    <parent>
        <artifactId>TestMultiModuleTest</artifactId>
        <groupId>com.shpun</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-provider</artifactId>

    <dependencies>
        <!-- 模塊test-api依賴 -->
        <dependency>
            <groupId>com.shpun</groupId>
            <artifactId>test-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <!-- 無需添加<version></version>標簽 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency

    </dependencies>

</project>

模塊 test-web 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">
    
    <parent>
        <artifactId>TestMultiModuleTest</artifactId>
        <groupId>com.shpun</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-web</artifactId>
    <packaging>war</packaging>

    <name>test-web Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

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

    <dependencies>

       <!-- 模塊test-api依賴 -->
        <dependency>
            <groupId>com.shpun</groupId>
            <artifactId>test-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
       <!-- 模塊test-provider依賴 -->
        <dependency>
            <groupId>com.shpun</groupId>
            <artifactId>test-provider</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>test-web</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

項目結構

項目列表


項目列表.PNG

模塊test-api


模塊test-api.PNG

模塊test-provider
模塊test-provider.PNG

模塊test-web


模塊test-web.PNG

總結

一開始test-provider和test-web兩個模塊都是webapp,兩個模塊的spring配置文件分開讀取。test-provider讀取spring,mybatis配置文件;test-web讀取springMVC配置文件。但在test-provider中的spring的配置文件一直讀取不到,classpath:和classpath*:都不行。部署的有兩個webapp,分兩個tomcat部署也不行,合并部署也不行。留個坑。

分開讀取配置文件待解決

改成在test-web中的web.xml讀取所有的配置文件classpath:application-.xml。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現,斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,283評論 6 342
  • 這篇很長! maven多模塊開發(fā)是基于網上一篇文章介紹學習而來的,為了防止他的鏈接以后掛了,所以現在復制其內容過來...
    簡單coder閱讀 1,670評論 0 5
  • 簡介 概述 Maven 是一個項目管理和整合工具 Maven 為開發(fā)者提供了一套完整的構建生命周期框架 Maven...
    閩越布衣閱讀 4,544評論 6 39
  • 丙申之冬,校對《條麓堂集》之字誤,歷時彌月,略讀其半。雖囫圇吞棗,過眼云煙,亦始知張四維其人,兼知是書。具條陳之,...
    鳳起公子閱讀 1,321評論 3 5

友情鏈接更多精彩內容