此处仅讨论单体多模块的父子工程的打包配置, 可以推及springcloud多模块的打包, 一样一样的…
关于maven基础, 可以参考此笔记中的两篇文档:
https://www.yuque.com/fvy7xd/xinzhang/uy0tm7
https://www.yuque.com/fvy7xd/xinzhang/ll0ppb

classpath

参考: https://blog.csdn.net/wppwpp1/article/details/106628669
https://blog.csdn.net/qq_35772435/article/details/105251885
顾名思义, classpath指的是项目编译后的class文件的路径
因为编译后的文件路径与源程序是对应的, 因此classpath 等价于 main/java + main/resources + 第三方jar包的根目录

  1. 引用classpath路径下的文件,只需在文件名前加classpath, 比如指定log配置文件

    1. server:
    2. port: 60001
    3. logging:
    4. config: classpath:logback-spring.xml
  2. lib和classes同属classpath,两者的访问优先级为: lib>classes。

  3. classpath 和 classpath* 区别:
  • classpath:只会到你的class路径中查找找文件;
  • classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。

打jar包

项目结构如图
image.png

父工程pom配置

  • 依赖版本管理以及插件版本管理
  • jdk编译版本以及编译格式
  • 项目版本 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <project xmlns=”http://maven.apache.org/POM/4.0.0

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    top.xinzhang0618 oa pom 1.0.0-SNAPSHOT

    oa-core oa-schema oa-web oa-biz 1.8 1.8 1.8 UTF-8 1.0.0-SNAPSHOT
  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>top.xinzhang0618</groupId>
  5. <artifactId>oa-biz</artifactId>
  6. <version>${project.version}</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>top.xinzhang0618</groupId>
  10. <artifactId>oa-core</artifactId>
  11. <version>${project.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>top.xinzhang0618</groupId>
  15. <artifactId>oa-schema</artifactId>
  16. <version>${project.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>top.xinzhang0618</groupId>
  20. <artifactId>oa-web</artifactId>
  21. <version>${project.version}</version>
  22. </dependency>
  23. <!--springboot, 此处import引入依赖版本管理-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-dependencies</artifactId>
  27. <version>2.1.3.RELEASE</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. <!--fastjson-->
  32. <dependency>
  33. <groupId>com.alibaba</groupId>
  34. <artifactId>fastjson</artifactId>
  35. <version>1.2.33</version>
  36. </dependency>
  37. <!--jwt-->
  38. <dependency>
  39. <groupId>com.auth0</groupId>
  40. <artifactId>java-jwt</artifactId>
  41. <version>3.10.3</version>
  42. </dependency>
  43. <!--mybatisPlus-->
  44. <dependency>
  45. <groupId>com.baomidou</groupId>
  46. <artifactId>mybatis-plus-boot-starter</artifactId>
  47. <version>3.3.2</version>
  48. </dependency>
  49. <!--mysql-->
  50. <dependency>
  51. <groupId>mysql</groupId>
  52. <artifactId>mysql-connector-java</artifactId>
  53. <version>6.0.6</version>
  54. </dependency>
  55. <!--druid-->
  56. <dependency>
  57. <groupId>com.alibaba</groupId>
  58. <artifactId>druid-spring-boot-starter</artifactId>
  59. <version>1.1.23</version>
  60. </dependency>
  61. <!--oss-->
  62. <dependency>
  63. <groupId>com.aliyun.oss</groupId>
  64. <artifactId>aliyun-sdk-oss</artifactId>
  65. <version>3.10.2</version>
  66. </dependency>
  67. <!--httpclient-->
  68. <dependency>
  69. <groupId>org.apache.httpcomponents</groupId>
  70. <artifactId>httpclient</artifactId>
  71. <version>4.5.12</version>
  72. </dependency>
  73. <!-- transmittable-thread-local -->
  74. <dependency>
  75. <groupId>com.alibaba</groupId>
  76. <artifactId>transmittable-thread-local</artifactId>
  77. <version>2.11.5</version>
  78. </dependency>
  79. </dependencies>
  80. </dependencyManagement>
  81. <build>
  82. <pluginManagement>
  83. <plugins>
  84. <plugin>
  85. <groupId>org.apache.maven.plugins</groupId>
  86. <artifactId>maven-jar-plugin</artifactId>
  87. <version>3.1.1</version>
  88. </plugin>
  89. <plugin>
  90. <groupId>org.apache.maven.plugins</groupId>
  91. <artifactId>maven-resources-plugin</artifactId>
  92. <version>3.1.0</version>
  93. </plugin>
  94. <plugin>
  95. <groupId>org.apache.maven.plugins</groupId>
  96. <artifactId>maven-dependency-plugin</artifactId>
  97. <version>3.1.1</version>
  98. </plugin>
  99. <plugin>
  100. <groupId>org.apache.maven.plugins</groupId>
  101. <artifactId>maven-compiler-plugin</artifactId>
  102. <version>3.8.0</version>
  103. </plugin>
  104. <plugin>
  105. <groupId>org.springframework.boot</groupId>
  106. <artifactId>spring-boot-maven-plugin</artifactId>
  107. <version>2.1.3.RELEASE</version>
  108. </plugin>
  109. </plugins>
  110. </pluginManagement>
  111. </build>

  1. <a name="1Ttu0"></a>
  2. ### web(springboot模块)打包配置
  3. - springboot打包插件
  4. 参考: [https://blog.csdn.net/taiyangdao/article/details/75303181](https://blog.csdn.net/taiyangdao/article/details/75303181)<br />注意需加上repackage的插件目标, repackage会将web依赖的模块都打进xxx-web.jar里
  5. - 资源过滤
  6. 通过filter过滤后的资源文件, 会视为系统的配置文件, 其中的${}变量, 编译时会被赋值; 而不想要excel等文件的${}被赋值, 在filter中需排除这类文件
  7. ```xml
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <project xmlns="http://maven.apache.org/POM/4.0.0"
  10. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  12. <parent>
  13. <artifactId>oa</artifactId>
  14. <groupId>top.xinzhang0618</groupId>
  15. <version>1.0.0-SNAPSHOT</version>
  16. </parent>
  17. <modelVersion>4.0.0</modelVersion>
  18. <artifactId>oa-web</artifactId>
  19. <dependencies>
  20. <dependency>
  21. <groupId>top.xinzhang0618</groupId>
  22. <artifactId>oa-biz</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>top.xinzhang0618</groupId>
  26. <artifactId>oa-core</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>top.xinzhang0618</groupId>
  30. <artifactId>oa-schema</artifactId>
  31. </dependency>
  32. <!--springbootWeb-->
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-web</artifactId>
  36. </dependency>
  37. <!--springbootTest-->
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <resources>
  46. <resource>
  47. <directory>src/main/resources</directory>
  48. <includes>
  49. <include>excel/**</include>
  50. <include>es/**</include>
  51. </includes>
  52. <filtering>false</filtering>
  53. </resource>
  54. <resource>
  55. <directory>src/main/resources</directory>
  56. <filtering>true</filtering>
  57. </resource>
  58. </resources>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. <executions>
  64. <execution>
  65. <goals>
  66. <goal>repackage</goal>
  67. </goals>
  68. </execution>
  69. </executions>
  70. </plugin>
  71. </plugins>
  72. </build>
  73. </project>

其他补充

  1. 打完jar后启动命令: java -jar -Dspring.profiles.active=dev oa-web-1.0.0-SNAPSHOT.jar
  2. 依赖模块无需配置打包插件等
  3. 其他模块若有资源文件, 一样需要放到resources文件夹下供web模块引用, 比如base.xml的引用, 见下

image.png
image.png

打包结果分析

image.png
我们解压oa-web-1.0.0-SNAPSHOT.jar,所有的jar包在BOOT-INF/lib下, 其中也能找到依赖的模块的jar包, 打完包后jar大小为36.7M
image.png
image.png


配置分离

打zip包

参考: https://blog.csdn.net/qq_38279833/article/details/105513529

  1. 父工程加个输出目录的配置, 注意basedir是web工程的根目录, ${basedir}/../target是父工程下的target目录

    1. <properties>
    2. <dist.path>${basedir}/../target</dist.path>
    3. <java.version>1.8</java.version>
    4. <maven.compiler.source>1.8</maven.compiler.source>
    5. <maven.compiler.target>1.8</maven.compiler.target>
    6. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    7. <project.version>1.0.0-SNAPSHOT</project.version>
    8. </properties>
  2. web工程变更如下:

  • 更改资源文件的输出路径
  • springboot打包插件打包方式为zip包, 并指定jar输出目录
  • 增加dependency插件, 将web工程依赖的包复制进指定jar目录 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <project xmlns=”http://maven.apache.org/POM/4.0.0

    1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    1. <artifactId>oa</artifactId>
    2. <groupId>top.xinzhang0618</groupId>
    3. <version>1.0.0-SNAPSHOT</version>

    4.0.0

    oa-web

    top.xinzhang0618 oa-biz top.xinzhang0618 oa-core top.xinzhang0618 oa-schema org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test src/main/resources ${dist.path} false excel/ es/ src/main/resources ${dist.path} true org.springframework.boot spring-boot-maven-plugin ZIP non-exists non-exists ${dist.path}/lib repackage org.apache.maven.plugins maven-dependency-plugin copy package copy-dependencies ${dist.path}/lib

  1. 3. 其他工程不需要变更, 依旧不需要打包插件
  2. 3. 项目启动需要指定lib目录, 启动命令为: java -jar -Dspring.profiles.active=dev -Dloader.path=. ./lib/oa-web-1.0.0-SNAPSHOT.jar
  3. 注意, 项目打包后, idea无法通过debug按钮直接启动, 因为需要指定lib目录, 简单做法, Clean清除打的包, debug启动(执行的是build)
  4. 打包结果如下, lib目录下的oa-web-1.0.0-SNAPSHOT.jar不包含其他模块的依赖, 打完包后target文件夹46.5M, 比打jar包大了10M.........<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/281275/1610691765477-736e593a-2c46-431a-ae7d-4b06dc39da11.png#crop=0&crop=0&crop=1&crop=1&height=437&id=OTWdb&margin=%5Bobject%20Object%5D&name=image.png&originHeight=437&originWidth=348&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16516&status=done&style=none&title=&width=348)
  5. ---
  6. <a name="YNoXP"></a>
  7. ### Nacos
  8. 官方文档: [https://nacos.io/zh-cn/docs/quick-start-spring-boot.html](https://nacos.io/zh-cn/docs/quick-start-spring-boot.html)<br />主要的使用官方文档上都有, 这里简略写一点
  9. 1. 安装及启动nacos
  10. 1. 加依赖
  11. ```xml
  12. <!--nacos-->
  13. <dependency>
  14. <groupId>com.alibaba.boot</groupId>
  15. <artifactId>nacos-config-spring-boot-starter</artifactId>
  16. <version>0.2.7</version>
  17. </dependency>
  1. 代码配置(启动类加@NacosPropertySource注解, 配置文件加上nacos配置) ```java @SpringBootApplication(scanBasePackages = “top.xinzhang0618.oa”) @NacosPropertySource(dataId = “oa-test.yml”, autoRefreshed = true,type = ConfigType.YAML) public class WebApplication {

    public static void main(String[] args) {

    1. SpringApplication.run(WebApplication.class, args);

    }

}

  1. ```yaml
  2. spring:
  3. application:
  4. name: oa
  5. nacos:
  6. config:
  7. server-addr: http://39.106.55.179:40000
  8. namespace: c3545dbe-b516-49a4-9f86-da97dd019ce7
  1. 在配置中心添加相关配置

image.png
image.png

支持配置的动态刷新, 使用示例

  1. @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
  2. private boolean useLocalCache;

配置绑定变量

这里拿打jar包的配置做如下变更
通过maven的profile实现配置绑定变量, 以在不同的环境使用不同的轻量化的配置文件

  1. 父工程添加profile

    1. <profiles>
    2. <profile>
    3. <id>oa-prod</id>
    4. <build>
    5. <filters>
    6. <filter>${basedir}/../profiles/oa-prod.properties</filter>
    7. </filters>
    8. </build>
    9. </profile>
    10. </profiles>
  2. web工程建立application-prod.yml, 参数使用变量 ```yaml spring: datasource: druid: url: jdbc:mysql://${database.host}:${database.port}/oa_dev?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&useSSL=false username: ${database.user} password: ${database.password} redis: host: ${redis.host} port: ${redis.port} database: ${redis.database} password: ${redis.password}

  1. 3. 在工程目录下建profiles/oa-prod.properties配置文件
  2. ```yaml
  3. #database
  4. database.host=39.106.55.179
  5. database.port=3306
  6. database.user=root
  7. database.password=xxx
  8. #redis
  9. redis.host=39.106.55.179
  10. redis.port=6379
  11. redis.password=xxx
  12. redis.database=1

如上配置完成后, 在主目录下, 打包命令如下:
$mvn clean package -Dmaven.test.skip=true -P oa-prod -f pom.xml

其中mvn参数: (参数不懂的 mvn -h 获取帮助)
-D 表示定义的系统属性, maven或者java的
-P 表示指定的profile
-f 表示指定的pom文件

将打出的oa-web-1.0.0-SNAPSHOT.jar包解压后能看到相应的prod.yml配置文件中的变量已经被赋值了
image.png

最后, 启动命令不变: $java -jar -Dspring.profiles.active=prod oa-web-1.0.0-SNAPSHOT.jar

抽取公共pom文件

如下, 可以像springcloudDependences一样, 抽取公共依赖, install和deploy到仓库后, 其他模块就可以import使用

xz-demo-dependencies-1.0.0-SNAPSHOT.pom

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.pingan.lcloud</groupId>
  6. <artifactId>xz-demo-dependencies</artifactId>
  7. <version>1.0.0-SNAPSHOT</version>
  8. <packaging>pom</packaging>
  9. <properties>
  10. <fastjson.version>1.2.33</fastjson.version>
  11. <lombok.version>1.18.22</lombok.version>
  12. <commons-lang3.version>3.12.0</commons-lang3.version>
  13. <hutool-all.version>5.7.21</hutool-all.version>
  14. </properties>
  15. <dependencyManagement>
  16. <dependencies>
  17. <!--fastjson-->
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>fastjson</artifactId>
  21. </dependency>
  22. <!--lombok-->
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. </dependency>
  27. <!--commons-lang3-->
  28. <dependency>
  29. <groupId>org.apache.commons</groupId>
  30. <artifactId>commons-lang3</artifactId>
  31. </dependency>
  32. <!--hutool-all-->
  33. <dependency>
  34. <groupId>cn.hutool</groupId>
  35. <artifactId>hutool-all</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-dependencies</artifactId>
  40. <version>Greenwich.SR3</version>
  41. <type>pom</type>
  42. <scope>import</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-dependencies</artifactId>
  47. <version>2.1.3.RELEASE</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <pluginManagement>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. <configuration>
  60. <!--devTools-->
  61. <fork>true</fork>
  62. </configuration>
  63. <executions>
  64. <execution>
  65. <goals>
  66. <goal>repackage</goal>
  67. </goals>
  68. </execution>
  69. </executions>
  70. </plugin>
  71. </plugins>
  72. </pluginManagement>
  73. </build>
  74. </project>

安装到仓库

  1. mvn -f xz-demo-dependencies-1.0.0-SNAPSHOT.pom install
  2. mvn -f xz-demo-dependencies-1.0.0-SNAPSHOT.pom deploy

其他模块使用

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>com.pingan.lcloud</groupId>
  5. <artifactId>xz-demo-dependencies</artifactId>
  6. <version>1.0.0-SNAPSHOT</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>