SpringBoot 多模块项目搭建+H2测试+profile

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请附上博主博文网址,并标注作者。违者必究 https://blog.csdn.net/HuHui_/article/details/83028195

前言

现在SpringBoot越来越流行,其配置少的特点,让我们不论是做传统Web还是微服务都青睐于SpringBoot

这里阐述一下 SpringBoot 多模块用maven搭建+H2测试+maven profile

转载请注明作者感谢~

重点

  1. POM.XML的构建
  2. 多模块依赖关系
  3. H2引入及测试
  4. maven profile 引入根据不同环境打包不同配置
  5. 生成出来应该是一个JAR包,把依赖的包打进去。可以指定生成jar包的filename
  6. 关注POM文件里面写的

Core - Code

  • 我们要划分好自己需要的模块和职责
    1. nile-cmszbs-szcst-be(root)
    2. nile-cmszbs-szcst-common(通用工具)
    3. nile-cmszbs-szcst-cs (servier+dao+domain)
    4. nile-cmszbs-szcst-rs(controller)
<?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.richstonedt.nile.cmszbs.szcst</groupId>
    <artifactId>nile-cmszbs-szcst-be</artifactId>
    <packaging>pom</packaging>
    <version>0.1.0-SNAPSHOT</version>
    <modules>
        <module>nile-cmszbs-szcst-rs</module>
        <module>nile-cmszbs-szcst-cs</module>
        <module>nile-cmszbs-szcst-common</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>
    
    <description>
        root的pom.xml 其他多个子模块公用的包可以在这里引入 子模块引用这些JAR包则不需要填写VERSION
    </description>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <mybatis.spring.boot.starter.version>1.3.0</mybatis.spring.boot.starter.version>
        <commons.lang3.version>3.6</commons.lang3.version>
        <swagger2.version>2.7.0</swagger2.version>
        <mybatis.generator>1.3.7</mybatis.generator>
        <postgresql.version>9.4.1212</postgresql.version>
        <h2.version>1.4.197</h2.version>
        <pagehelper.version>1.2.7</pagehelper.version>
        <quartz.version>2.2.3</quartz.version>
        <fastjson.version>1.2.39</fastjson.version>
        <poi.version>3.17</poi.version>
        <logback.version>1.1.11</logback.version>
        <druid.version>1.1.10</druid.version>
    </properties>


    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 开发环境 -->
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>

    <!-- 统一依赖管理放root的pom.xml 子模块引用这些JAR包则不需要填写VERSION-->
    <dependencyManagement>
        <dependencies>
            <!--log -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>

            <!--mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.starter.version}</version>
            </dependency>

            <!--H2-->
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>${h2.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- fast json -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!--PG SQL-->
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>${postgresql.version}</version>
            </dependency>

            <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!-- apache poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>

            <!-- apache common -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons.lang3.version}</version>
            </dependency>

            <!-- pagehelper -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>

            <!-- Swagger -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger2.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger2.version}</version>
            </dependency>

            <!-- spring quartz -->
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>${quartz.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <distributionManagement>
        <repository>
            <id>nexus_production</id>
            <url>nexus_production URL</url>
        </repository>
        <snapshotRepository>
            <id>nexus_snapshots</id>
            <url>nexus_snapshots URL</url>
        </snapshotRepository>
    </distributionManagement>


</project>
<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>nile-cmszbs-szcst-be</artifactId>
        <groupId>com.richstonedt.nile.cmszbs.szcst</groupId>
        <version>0.1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <description>
        common包,通用的工具类等
    </description>

    <artifactId>nile-cmszbs-szcst-common</artifactId>
    <name>nile-cmszbs-szcst-common</name>
    <version>${project.parent.version}</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!-- apache poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

</project>
<?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>
        <artifactId>nile-cmszbs-szcst-be</artifactId>
        <groupId>com.richstonedt.nile.cmszbs.szcst</groupId>
        <version>0.1.0-SNAPSHOT</version>
    </parent>

    <artifactId>nile-cmszbs-szcst-cs</artifactId>
    <name>nile-cmszbs-szcst-cs</name>
    <version>${project.parent.version}</version>
    <packaging>jar</packaging>

    <description>
        CS包,service+dao+domain
    </description>

    <dependencies>
        <dependency>
            <groupId>com.richstonedt.nile.cmszbs.szcst</groupId>
            <artifactId>nile-cmszbs-szcst-common</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <!--spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!--log -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <!--PG SQL-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <!--H2-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <!-- pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis.generator}</version>
                <configuration>
                    <!-- 配置文件 -->
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <!-- 允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!--JDBC postgresql-->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>${postgresql.version}</version>
                    </dependency>
                    <!-- mybatis-generator-core -->
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>${mybatis.generator}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <finalName>nile-cmszbs-szcst-cs</finalName>
    </build>

</project>
<?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>nile-cmszbs-szcst-be</artifactId>
        <groupId>com.richstonedt.nile.cmszbs.szcst</groupId>
        <version>0.1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nile-cmszbs-szcst-rs</artifactId>
    <version>${project.parent.version}</version>
    <packaging>jar</packaging>

    <description>
        controller,也是构建包的POM
    </description>

    <dependencies>
        <!-- models -->
        <dependency>
            <groupId>com.richstonedt.nile.cmszbs.szcst</groupId>
            <artifactId>nile-cmszbs-szcst-cs</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.richstonedt.nile.cmszbs.szcst</groupId>
            <artifactId>nile-cmszbs-szcst-common</artifactId>
            <version>${project.parent.version}</version>
        </dependency>


        <!--spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>

        <!-- H2 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- fast json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <!-- spring quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>
    </dependencies>
    
    <!-- 根据公司规范 be结尾 并添加版本号 -->
    <build>
        <finalName>nile-cmszbs-szcst-be-${project.parent.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

总结

搭建项目需要跟公司规范挂钩,例如是否有用到NEXUS私有仓库,是否需要源码包,环境的版本等

文章内容并没有困难点,在于自身总结,做一个思考。JAR包版本的选择等。集成一些工具后如何配置好让其生效,并且考虑JAR冲突的因素。

一个宗旨,基础东西,总结! 困难的技术点,总结!困难的使用工具,总结!架构学些,总结!

转载请注明作者感谢~ 自己总结的东西才是自己的

猜你喜欢

转载自blog.csdn.net/HuHui_/article/details/83028195
今日推荐