Maven工程的多模块项目、项目与项目之间的引用

     一个大项目需要一个团队来完成,然后一个大型项目就拆分成几块来同时开发,节省时间,提高效率.


大致分为以下几个模块(仅是自身经历):
|—依赖管理工程模块:一般现在开发都是以maven来管理jar包,方便.所以整个工程的依赖统一放在一个单独工程中,一般叫做父工程xxx-parent.
     |– 注意事项:父工程打包方式设置成pom
                       创建父工程时,不要勾选maven模板,他就是一个管理依赖的,不处理业务.如下图
这里写图片描述


|—pom文件格式如下:

<?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.qiushiju</groupId>
    <artifactId>xxx-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 集中定义依赖版本号 -->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.6.4</slf4j.version>
        <jackson.version>2.4.2</jackson.version>
        <druid.version>1.0.9</druid.version>
        <httpclient.version>4.3.5</httpclient.version>
        <jstl.version>1.2</jstl.version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>
        <commons-lang3.version>3.3.2</commons-lang3.version>
        <commons-io.version>1.3.2</commons-io.version>
        <commons-net.version>3.3</commons-net.version>
        <pagehelper.version>3.4.2</pagehelper.version>
        <jsqlparser.version>0.9.1</jsqlparser.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <jedis.version>2.7.2</jedis.version>
        <solrj.version>4.10.3</solrj.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- 时间操作组件 -->
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>${joda-time.version}</version>
            </dependency>
            <!-- Apache工具组件 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>${commons-net.version}</version>
            </dependency>
            <!-- Jackson Json处理工具包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <!-- httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- 日志处理 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.miemiedev</groupId>
                <artifactId>mybatis-paginator</artifactId>
                <version>${mybatis.paginator.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- JSP相关 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jsp-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <!-- 文件上传组件 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons-fileupload.version}</version>
            </dependency>
            <!-- Redis客户端 -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>${jedis.version}</version>
            </dependency>
            <!-- solr客户端 -->
            <dependency>
                <groupId>org.apache.solr</groupId>
                <artifactId>solr-solrj</artifactId>
                <version>${solrj.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
      <pluginManagement>
            <plugins>
                <!--&lt;!&ndash; 配置Tomcat插件 &ndash;&gt;-->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

        |—pom文件中的< properties>< /properties>标签内是下面< dependencies>< /dependencies>标签中的版本号,把它们提取出来,便于方便管理.
                |—把这个工程编译安装进本地仓库,这样后面其他的工程才能使用IDEA中是这样操作
这里写图片描述
                |—其他模块怎么使用父工程的依赖,下面几个模块介绍


|—公共工程模块(common):这个工程内放置的是整个项目要用用的公共类,例如日期格式类(DataUtil.java)、分页结果类(PafeReult.java)、封装JSON结果类(JsonResult.java)等等….
                |—创建公共工程(xxx-common),使用maven创建一个java工程,因为它只提供几个类,不做web项目.如图
这里写图片描述
                |—打包方式为jar包.因为它其中的类需要被别的工程使用.可以在pom.xml文件中写,指定打包方式

<packaging>jar</packaging>


                |—继承父工程(xxx-parent).在pom.xml文件中写入如下代码

<parent>
        <groupId>com.qiushiju</groupId>
        <artifactId>xxx-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>


                |—写入本工程实际需要的依赖(从父工程中copy,不需版本信息)比如

<dependencies>
.
.
.
 <!-- 日志处理 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        .
        .
        .
        </dependencies>


                |—把xxx-common工程也编译打包安装进本地仓库,方便别人调用,打包方式参考上面


|—开发工程模块(比如后台管理系统或者前台管理系统xxx-manager):这个模块一般都是MVC架构,所以需要三个子模块来分开开发以提升效率.三个子模块分别是
xxx-manager-pojo、xxx-manager-controller、xxx-manager-service、xxx-manager-dao。如图项目结构
这里写图片描述

                |—先说xxx-manager的pom.xml,①它需要继承xxx-parent使用它的依赖,②还有需要加入xxx-common的依赖,要使用其中的工具类
部分代码:

 <packaging>pom</packaging>

 <parent>
        <groupId>com.qiushiju</groupId>
        <artifactId>xxx-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
 </parent>
    <!-- 依赖管理 -->
<dependencies>
            <!-- 加入common依赖 -->
        <dependency>
            <groupId>com.qiushiju</groupId>
            <artifactId>xxx-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  ...
  </dependencies>
   <modules>

        <module>xxx-manager-pojo</module>
        <module>xxx-manager-mapper</module>
        <module>xxx-manager-service</module>
        <module>xxx-manager-controller</module>

    </modules>


                |—①从上面代码看到xxx-manager的打包方式要为pom.因为它的几个子模块要使用它的依赖.
                ②没有别的依赖,因为依赖在四个子模块的各自pom.xml文件中写着,每个模块写各自所需的依赖
                ③< module>标签,创建一个子模块就会自动出来这一标签~

                |—现在来说在xxx-manager的基础上创建子项目,方法如下:xxx-manager项目上右键-new-module
这里写图片描述


                |—同样的步骤创建其他子模块.每个子类创建成功后,在自己的pom.xml文件中都会自动有< parent>标签来继承父类,

<!-- 继承父类 -->
    <parent>
        <artifactId>xxx-manager</artifactId>
        <groupId>com.qiushiju</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
     <dependencies>
     <!-- controller需要service的依赖, -->
     <!-- 在xxx-manager-service的pom文件中,也需要dao层的依赖 -->
        <dependency>
            <groupId>com.qiushiju</groupId>
            <artifactId>xxx-manager-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>
        ..
        </dependencies>

                |—特殊的,①pojo,service,dao模块需要打包方式为jar,因为controller需要使用它们的类.而controller模块是需要创建为webapp,
                ②dao层使用mybatis,因此会有Mapper.xml文件.因此打包时会出错,需要在xxx-manager-dao的pom文件中加入配置代码,代码如下

 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

                |—ssm的配置文件写在xxx-manager-controller的src-main-resource目录下与java同级.静态文件放在wabapp下
这里写图片描述
                |—如果新建的工程没有这三个文件夹,就自己创建.然后mark这个文件夹为相应的资源文件
这里写图片描述



注意:不管哪一个模块修改,都需要重新编译,打包,安装进仓库,才能时时更新,别的工程才能调用.



后话:自己项目中的总结,可能有疏漏,有好的意见或建议欢迎指正

对于Spring Boot多模块项目的打包,可以按照以下步骤进行操作。 首先,你需要将项目源码导入到本地进行打包验证。这些源码包括单工程构建打包和多模块打包的过程,可以放心使用。 在项目中,包含了一个父工程、多个子模块和一个工具模块。每个子模块都有启动类,而工具模块没有。因此,这三者的pom文件可能会有所不同。根据项目的需求,你需要修改这些pom文件。之后,你可以使用IDEA的打包工具一键打包整个项目。 接下来,你需要修改每个子模块中有启动类的pom文件。在这段配置中,你需要加入以下内容: <build> <plugins> <plugin> <!--该插件主要用途:构建可执行的JAR--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> </build> 以上配置的作用包括将java目录中的*.xml文件进行打包,并设置自己目录下的配置文件。 通过按照以上步骤进行操作,你就可以成功打包Spring Boot多模块项目了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot多模块打包源码](https://download.csdn.net/download/zhangcongyi420/88153258)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot多模块项目打包教程,超详细图文详解!](https://blog.csdn.net/weixin_43314519/article/details/115152850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值