springboot多模块项目maven实战解说

最近工作中,常常都是以模块的形式,来分配任务的,和以往个人的项目不同,个人的项目不需要分模块设计,多模块设计也是考虑到很多的问题,比如负载均衡,项目的方便的管理等,下面来讲解一下项目的整体maven架构问题,理解整体,可以让我们更理解整体是到底怎么设计的。

maven项目的结构

下面是maven项目的标准结构,和在idea中的情形。
src文件夹下是Java代码,target下是编译后的文件,一般说来,web项目中可能出现配置缺少或者一些资源文件不生效,那就要看看是不是包含到这个文件夹了,这也是一种排错经验吧,pom.xml是maven的整个结构的骨架,就是配置文件。
在这里插入图片描述在这里插入图片描述

maven的pom.xml里的详细配置

在这里插入图片描述如上图 所示的箭头,在pom里的配置,对应每一个模块,画圈圈的地方,就是一个总项目。

    <groupId>org.potato</groupId>//代表项目坐标
    <artifactId>cocplatform</artifactId>//代表项目坐标
    <packaging>pom</packaging>//打包方式是值在这个pom里只负责把这些模块打包编译,其实做完这些就没这个pom什么事了
    <version>0.1</version>//版本号

建议去看《maven实战》这本书。写不下去,看来,总结点东西还正是累。干脆直接放完整个代码吧。

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

    <!--spring boot 依赖管理,maven继承的方式-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <groupId>org.potato</groupId>
    <artifactId>cocplatform</artifactId>
    <packaging>pom</packaging>
    <version>0.1</version>

    <name>cocplatform</name>
    <description>this is a platform for playing coc</description>

    <modules>
        <module>cocplatform-chat</module>
        <module>core</module>
        <module>investigator-manager</module>
        <module>cocplatform-combine</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <mysql-version>8.0.15</mysql-version>
    </properties>

    <!--应该使用这样方式管理依赖比较好,不容易造成依赖冲突,以后多模块好管理-->
    <!--对后面的模块的依赖只是提供管理,用不用这个依赖还要后面模块的引用,这里只是提供了统一的版本依赖管理-->
    <dependencyManagement>
        <dependencies>

            <!--springboot 依赖管理 start,从外部导入,也可以采用继承的方式-->
            <!--<dependency>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-starter-parent</artifactId>-->
                <!--<version>2.1.3.RELEASE</version>-->
                <!--<type>pom</type>-->
                <!--<scope>import</scope>-->
            <!--</dependency>-->
            <!--springboot 依赖管理 end-->


            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-version}</version>
            </dependency>

            <!--swagger ui -start-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.6.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.6.0</version>
            </dependency>
            <!--end-->
        </dependencies>
    </dependencyManagement>


    <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>build</id>
            <properties>
                <profileActive>build</profileActive>
            </properties>
        </profile>
    </profiles>



    <build>
        <plugins>
            <!--<plugin>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
            <!--</plugin>-->
            <plugin>
                <!--需要加下面这个插件才能使用多环境在maven中配置激活-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
             <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>
                 </configuration>
             </plugin>
        </plugins>
    </build>

</project>

上面是一个总的pom文件,各个模块通过继承这个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>
    <parent>
        <groupId>org.potato</groupId>
        <artifactId>cocplatform</artifactId>
        <version>0.1</version>
    </parent>
    <artifactId>cocplatform-combine</artifactId>
    <version>0.1</version>
    <name>cocplatform-combine</name>
    <packaging>war</packaging>
    <description>combine all function in coc</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>coc</finalName>
        <!--需要加上这个资源过滤才能激活相应的开发环境配置文件,起到替代占位符的作用-->
        <!--资源过滤-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--需要true才能替换占位符-->
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.txt</include>
                    <include>*.xlsx</include>
                    <include>application.yml</include>
                    <include>application-${profileActive}.yml</include>
                    <include>*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!--<plugin>此种方式是打jar的,由于本项目是war,所以不使用这个插件-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
            <!--</plugin>-->
            <plugin>
                <!--需要加下面这个插件才能使用多环境在maven中配置激活-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <!--打包成war包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <packagingExcludes>
                        <!--WEB-INF/lib/}-5.0-SNAPSHOT.jar,-->
                    </packagingExcludes>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <!--打包时跳过测试的部分-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <!--使用tomcat插件实现自动部署-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://47.102.223.47:8080/manager/text</url> <!-- tomcat管理路径,即ip端口等信息 -->
                    <!--<server>develope</server> &lt;!&ndash; 与settings.xml文件中Server的id相同 &ndash;&gt;-->
                    <path>/coc</path> <!-- 应用的部署位置 -->
                    <username>coc</username>
                    <password>coc</password>
                    <update>true</update>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

可以发现模块下的依赖不需要版本号了,这是因为总的pom已经进行了依赖管理,如果模块不引用的话,该模块是没有该依赖的,总的pom只是提供了选择和帮你统一了每个模块引用相同的依赖而已。模块可以继承总的pom,来达到继承的作用。
值得一提的是,springboot也已经提供了日常所需的web项目依赖管理,所以有些项目不写版本号,也是这个道理啦。
maven还提供了一个插件来做自动部署,如上所写的,只要做相应的配置就可以了,很方便的。之前我也是不知道这个,现在用这个插件来自动部署,很简便就是啦。
投偷点懒,大概意思就是啦,要是有人看到到这,可以留言,会详细告诉你这个构建的过程的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值