springboot父子工程搭建

springboot工程搭建

  • 层级目录图
  • 搭建父工程parent
    • 需要注意的
    • 父工程pom
  • 搭建api工程
    • 搭建api子父工程
    • 搭建api子工程
  • 搭建网关gateway工程
  • 搭建service工程
    • 搭建service子父工程
    • 搭建子工程pdx-goods-service
      • pdx-goods-service.pom
      • bootstrap.yml配置文件
    • 搭建子工程pdx-file-service
      • pdx-file-service.pom
      • bootstrap.yml配置文件
  • 搭建公共工程util
    • 搭建common子父工程
    • 搭建service-dependency子父工程
  • 搭建web工程
  • 错误
    • 打包报错repackage failed

  父工程及子父工程打包pom,只做版本控制
  二级父工程可按照功能分类,给每类工程创建子父工程
  1. ???–api:存储所有数据库表对应的bean和feign接口
  2. ???–gateway:存储所有微服务网关
  3. ???–service:存储所有微服务工程
  4. ???–util:存储公共工程
  5. ???–web:存储所有和页面渲染有关的工程

层级目录图

springboot父子工程搭建_第1张图片

搭建父工程parent

创建一个默认的spring框架的项目,推荐使用idea的自动构建
springboot父子工程搭建_第2张图片

springboot父子工程搭建_第3张图片

springboot父子工程搭建_第4张图片

springboot父子工程搭建_第5张图片

需要注意的

  1. 用到了Alibaba的依赖,需要springboot版本介于>=2.2.0,<2.3.0
  2. 删除不用的文件及文件夹springboot父子工程搭建_第6张图片
  3. 父工程及子父工程都不需要写代码,只做版本管控制,src文件夹删掉

父工程pom

顶级父工程的pom,包含最顶级的依赖,很少,比如lombok、test


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <modules>
        <module>pdx-servicemodule>
        <module>pdx-apimodule>
        <module>pdx-webmodule>
        <module>pdx-gatewaymodule>
        <module>pdx-utilmodule>
    modules>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.10.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.pdxgroupId>
    <artifactId>pdxartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>pdx-parentname>
    <description>pdx父工程(派大星)description>
    
    <packaging>pompackaging>
    <properties>
        <java.version>1.8java.version>
        <spring-clould-alibaba.version>2.2.1.RELEASEspring-clould-alibaba.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.60version>
        dependency>
    dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring-clould-alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>


project>

抽取不常用的,但是组件中大部分需要依赖的,可以新拉一个文件,我放到pom.bak中

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-mongodbartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-quartzartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.2.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.kafkagroupId>
            <artifactId>spring-kafkaartifactId>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.kafkagroupId>
            <artifactId>spring-kafka-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

搭建api工程

搭建api子父工程

新建module
springboot父子工程搭建_第7张图片
springboot父子工程搭建_第8张图片
pom


<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>pdxartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>pdx-goods-apimodule>
    modules>
    <artifactId>pdx-apiartifactId>
    <description>存储所有数据库表对应的bean和feign接口的子父工程description>

    <dependencies>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.2version>
            
            <scope>providedscope>
        dependency>
    dependencies>
project>

搭建api子工程

pom文件


<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>pdxartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>pdx-goods-apimodule>
    modules>
    <artifactId>pdx-apiartifactId>
    <description>存储所有数据库表对应的bean和feign接口的子父工程description>

    <dependencies>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.2version>
            
            <scope>providedscope>
        dependency>
    dependencies>
project>

搭建网关gateway工程

pom


<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>pdxartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <artifactId>pdx-gatewayartifactId>
    <description>存储所有微服务网关的子父工程description>


project>

搭建service工程

搭建service子父工程

pom


<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>pdxartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>pdx-goods-servicemodule>
        <module>pdx-file-servicemodule>
    modules>
    <artifactId>pdx-serviceartifactId>
    <description>存储所有微服务工程的子父工程description>

    <dependencies>
        
        <dependency>
            <groupId>com.pdxgroupId>
            <artifactId>pdx-commonartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>com.pdxgroupId>
            <artifactId>pdx-service-dependencyartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>
project>

搭建子工程pdx-goods-service

pdx-goods-service.pom


<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>pdx-serviceartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>pdx-goods-serviceartifactId>
    <description>实现goods的crud,对应数据库pdx_goodsdescription>

    <dependencies>
        
        <dependency>
            <groupId>com.pdxgroupId>
            <artifactId>pdx-goods-apiartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    dependencies>
project>

bootstrap.yml配置文件

server:
  port: 8082
spring:
  application:
    name: pdx-goods
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://???/shop_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: localhost:8848
      discovery:
        server-addr: localhost:8848 #Nacos的注册地址

# ====================MybatisPlus====================
mybatis-plus:
  mapper-locations: mapper/*.xml #mapper-locations:复杂的操作可能需要自己写SQL,SQL可以写到xml文件中,这里指定和Dao对应的xml文件,此时我们需要在resources中创建一个mapper目录

  type-aliases-package: com.sb.mall.*.model #type-aliases-package:指定JavaBean的别名包,和MyBatis用法一样
  configuration:
    map-underscore-to-camel-case: true #map-underscore-to-camel-case:开启驼峰功能,数据库表列名如果有_,可以自动按驼峰命名规则转换
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #log-impl:日志开启,方便测试

# ====================日志配置====================
logging:
  pattern:
    console: "%msg%n"

搭建子工程pdx-file-service

pdx-file-service.pom


<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>pdx-serviceartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>pdx-file-serviceartifactId>
    <description>文件上传下载description>

    <dependencies>
        
        <dependency>
            <groupId>com.cephgroupId>
            <artifactId>radosartifactId>
            <version>0.6.0version>
        dependency>
        
        <dependency>
            <groupId>com.cephgroupId>
            <artifactId>libcephfsartifactId>
            <version>0.80.5version>
        dependency>
        
        <dependency>
            <groupId>org.javaswiftgroupId>
            <artifactId>jossartifactId>
            <version>0.10.2version>
        dependency>
    dependencies>

project>

bootstrap.yml配置文件

server:
  port: 8082
spring:
  application:
    name: pdx-file
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: localhost:8848
      discovery:
        server-addr: localhost:8848 #Nacos的注册地址
# ====================ceph文件系统====================
ceph:
  username: cephtester:subtester #Ceph配置 主用户名:子用户名
  password: gupao #秘钥
  authUrl: http://192.168.100.131:7480/auth/1.0 #接口访问路径
  defaultContainerName: user_datainfo #默认容器名字

# ====================图片路径====================
cephurl: http://localhost:8082/file/download/

# ====================日志配置====================
logging:
  pattern:
    console: "%msg%n"

搭建公共工程util

需要用到的工具包也可以单独放到一个工程中,每次要用,直接依赖即可
pom


<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>pdxartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>pdx-service-dependencymodule>
        <module>pdx-commonmodule>
    modules>
    <artifactId>pdx-utilartifactId>
    <description>存储公共工程的子父工程description>


project>

搭建common子父工程

pom


<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>pdx-utilartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>pdx-commonartifactId>
    <description>公共工具包工程:需要用到的工具包单独放到一个工程中,每次要用,直接依赖即可description>

    
    <dependencies>
        <dependency>
            <groupId>cglibgroupId>
            <artifactId>cglib-nodepartifactId>
            <version>3.3.0version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>commons-beanutilsgroupId>
            <artifactId>commons-beanutilsartifactId>
            <version>1.8.3version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
        dependency>
    dependencies>
project>

搭建service-dependency子父工程

pom


<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>pdx-utilartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>pdx-service-dependencyartifactId>
    <description>所有service(微服务)工程公共依赖包、所有微服务工程公共配置类description>


    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            
            <version>2.2.1.RELEASEversion>
        dependency>
    dependencies>
project>

搭建web工程

pom


<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>pdxartifactId>
        <groupId>com.pdxgroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <packaging>pompackaging>
    <artifactId>pdx-webartifactId>
    <description>存储所有和页面渲染有关工程的子父工程description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>
project>

错误

打包报错repackage failed

Execution default of goal org.springframework.boot:spring-boot-maven-plugin: XXXX .RELEASE:repackage

只有服务模块生产可执行JAR包,其他几个模块是不需要可执行的,而我的根POM里,spring boot 默认帮我加入了如下build策略

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombokgroupId>
                        <artifactId>lombokartifactId>
                    exclude>
                excludes>
            configuration>
        plugin>
    plugins>
build>

解决方案比较简单,就是把这个spirng boot针对所有模块的build部分移除出来,放到你自己需要打包的那个子模块里,我这里是service模块,其他模块不需要生产可执行的jar.

你可能感兴趣的:(项目,spring,spring,boot,java)