父工程及子父工程打包pom,只做版本控制
二级父工程可按照功能分类,给每类工程创建子父工程
- ???–api:存储所有数据库表对应的bean和feign接口
- ???–gateway:存储所有微服务网关
- ???–service:存储所有微服务工程
- ???–util:存储公共工程
- ???–web:存储所有和页面渲染有关的工程
层级目录图
搭建父工程parent
创建一个默认的spring框架的项目,推荐使用idea的自动构建
需要注意的
- 用到了Alibaba的依赖,需要springboot版本介于>=2.2.0,<2.3.0
- 删除不用的文件及文件夹
- 父工程及子父工程都不需要写代码,只做版本管控制,src文件夹删掉
父工程pom
顶级父工程的pom,包含最顶级的依赖,很少,比如lombok、test
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modules>
<module>pdx-service</module>
<module>pdx-api</module>
<module>pdx-web</module>
<module>pdx-gateway</module>
<module>pdx-util</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.pdx</groupId>
<artifactId>pdx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pdx-parent</name>
<description>pdx父工程(派大星)</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-clould-alibaba.version>2.2.1.RELEASE</spring-clould-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-clould-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
抽取不常用的,但是组件中大部分需要依赖的,可以新拉一个文件,我放到pom.bak中
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
搭建api工程
搭建api子父工程
新建module 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">
<parent>
<artifactId>pdx</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>pdx-goods-api</module>
</modules>
<artifactId>pdx-api</artifactId>
<description>存储所有数据库表对应的bean和feign接口的子父工程</description>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
搭建api子工程
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">
<parent>
<artifactId>pdx</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>pdx-goods-api</module>
</modules>
<artifactId>pdx-api</artifactId>
<description>存储所有数据库表对应的bean和feign接口的子父工程</description>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
搭建网关gateway工程
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">
<parent>
<artifactId>pdx</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>pdx-gateway</artifactId>
<description>存储所有微服务网关的子父工程</description>
</project>
搭建service工程
搭建service子父工程
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">
<parent>
<artifactId>pdx</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>pdx-goods-service</module>
<module>pdx-file-service</module>
</modules>
<artifactId>pdx-service</artifactId>
<description>存储所有微服务工程的子父工程</description>
<dependencies>
<dependency>
<groupId>com.pdx</groupId>
<artifactId>pdx-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.pdx</groupId>
<artifactId>pdx-service-dependency</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
搭建子工程pdx-goods-service
pdx-goods-service.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">
<parent>
<artifactId>pdx-service</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pdx-goods-service</artifactId>
<description>实现goods的crud,对应数据库pdx_goods</description>
<dependencies>
<dependency>
<groupId>com.pdx</groupId>
<artifactId>pdx-goods-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</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
<?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>pdx-service</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pdx-file-service</artifactId>
<description>文件上传下载</description>
<dependencies>
<dependency>
<groupId>com.ceph</groupId>
<artifactId>rados</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>com.ceph</groupId>
<artifactId>libcephfs</artifactId>
<version>0.80.5</version>
</dependency>
<dependency>
<groupId>org.javaswift</groupId>
<artifactId>joss</artifactId>
<version>0.10.2</version>
</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
<?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>pdx</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>pdx-service-dependency</module>
<module>pdx-common</module>
</modules>
<artifactId>pdx-util</artifactId>
<description>存储公共工程的子父工程</description>
</project>
搭建common子父工程
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">
<parent>
<artifactId>pdx-util</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pdx-common</artifactId>
<description>公共工具包工程:需要用到的工具包单独放到一个工程中,每次要用,直接依赖即可</description>
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
搭建service-dependency子父工程
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">
<parent>
<artifactId>pdx-util</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pdx-service-dependency</artifactId>
<description>所有service(微服务)工程公共依赖包、所有微服务工程公共配置类</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
</project>
搭建web工程
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">
<parent>
<artifactId>pdx</artifactId>
<groupId>com.pdx</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>pdx-web</artifactId>
<description>存储所有和页面渲染有关工程的子父工程</description>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
解决方案比较简单,就是把这个spirng boot针对所有模块的build部分移除出来,放到你自己需要打包的那个子模块里,我这里是service模块,其他模块不需要生产可执行的jar.
|