Maven-Maven使用profile实现多套环境变量-《Java笔记》

admin 2025-10-19 01:10:53 编程 来源:ZONE.CI 全球网 0 阅读模式

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)
以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  13