Maven

多环境变量配置

在SpringCloud多模块微服务项目中,在父pom配置profile实现多套环境,同时也要重写build标签,定位要过滤替换环境变量值的配置文件

  1. <profiles>
  2. <profile>
  3. <id>dev</id>
  4. <properties>
  5. <!-- 环境标识,需要与配置文件的名称相对应-->
  6. <profiles.active>dev</profiles.active>
  7. </properties>
  8. <activation>
  9. <!-- 默认环境-->
  10. <activeByDefault>true</activeByDefault>
  11. </activation>
  12. </profile>
  13. <profile>
  14. <id>uat</id>
  15. <properties>
  16. <!-- 环境标识,需要与配置文件的名称相对应-->
  17. <profiles.active>uat</profiles.active>
  18. </properties>
  19. </profile>
  20. <profile>
  21. <id>prod</id>
  22. <properties>
  23. <!-- 环境标识,需要与配置文件的名称相对应-->
  24. <profiles.active>prod</profiles.active>
  25. </properties>
  26. </profile>
  27. </profiles>

配置好该项后可以在IDEA中的Maven中看到对应的环境变量值,选中激活当前的环境变量即可
image.png

在配置文件引用Maven环境变量值

  1. spring:
  2. application:
  3. name: eureka-server
  4. profiles:
  5. active: @profiles.active@
  6. logging:
  7. level:
  8. root: info

配置Maven编译时需要替换环境变量值的过滤的文件类型

  1. <build>
  2. <finalName>${project.name}-${project.version}</finalName>
  3. <resources>
  4. <resource>
  5. <directory>${basedir}/src/main/resources</directory>
  6. <filtering>true</filtering>
  7. <includes>
  8. <include>**/*.xml</include>
  9. <include>**/*.yml</include>
  10. <include>**/*.yaml</include>
  11. <include>**/*.properties</include>
  12. </includes>
  13. </resource>
  14. </resources>
  15. </build>

注意事项

在自定义build标签后,Maven编译时会根据自定义的过滤文件类型,如果不配置忽略过滤的文件,则可能会发生意想不到的后果。

  1. <build>
  2. <finalName>${project.name}-${project.version}</finalName>
  3. <resources>
  4. <resource>
  5. <directory>src/main/resources</directory>
  6. <filtering>true</filtering>
  7. <includes>
  8. <include>**/*.yml</include>
  9. </includes>
  10. </resource>
  11. <resource>
  12. <directory>src/main/resources</directory>
  13. <filtering>false</filtering>
  14. <includes>
  15. <include>**/*.xml</include>
  16. <include>**/*.ttf</include>
  17. <include>**/*.jpg</include>
  18. <include>**/*.png</include>
  19. <include>**/*.ftl</include>
  20. </includes>
  21. </resource>
  22. </resources>
  23. <plugins>
  24. <plugin>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-maven-plugin</artifactId>
  27. </plugin>
  28. </plugins>
  29. </build>

如:

不配置过滤字体文件,编译后的字体文件不能正常使用

双击编译后的字体
image.png
在使用itext的项目中具体报错为:

  1. com.itextpdf.text.DocumentException: Table 'name' does not exist in image/font/simhei.ttf
  2. at com.itextpdf.text.pdf.TrueTypeFont.getBaseFont(TrueTypeFont.java:517)
  3. at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:675)
  4. at com.itextpdf.text.pdf.TrueTypeFontUnicode.process(TrueTypeFontUnicode.java:122)
  5. at com.itextpdf.text.pdf.TrueTypeFontUnicode.<init>(TrueTypeFontUnicode.java:99)
  6. at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:706)
  7. at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:626)
  8. at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:461)
  9. at com.cntaiping.ft.bankinsurance.image.util.LexPDF.insentFont(LexPDF.java:291)
  10. at com.cntaiping.ft.bankinsurance.image.util.LexPDF.getPDFBytes(LexPDF.java:52)
  11. at com.cntaiping.ft.bankinsurance.image.service.impl.ImageBuildServiceImpl.build(ImageBuildServiceImpl.java:75)
  12. at com.cntaiping.ft.bankinsurance.image.service.impl.ImageBuildServiceImpl.build(ImageBuildServiceImpl.java:91)
  13. at com.cntaiping.ft.bankinsurance.image.service.impl.ImageServiceImpl.create(ImageServiceImpl.java:88)
  14. at com.cntaiping.ft.bankinsurance.image.service.impl.ImageServiceImpl$$FastClassBySpringCGLIB$$7c164064.invoke(<generated>)
  15. at org.springwork.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
  16. at org.springwork.aop.work.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687)
  17. at com.cntaiping.ft.bankinsurance.image.service.impl.ImageServiceImpl$$EnhancerBySpringCGLIB$$260adbfe.create(<generated>)
  18. at com.cntaiping.ft.bankinsurance.image.listener.ImageCreateStreamListener.onMessage(ImageCreateStreamListener.java:45)
  19. at com.cntaiping.ft.bankinsurance.image.listener.ImageCreateStreamListener.onMessage(ImageCreateStreamListener.java:20)
  20. at org.springwork.data.redis.stream.StreamPollTask.doLoop(StreamPollTask.java:142)
  21. at org.springwork.data.redis.stream.StreamPollTask.run(StreamPollTask.java:123)
  22. at java.lang.Thread.run(Thread.java:748)