目录
需求
根据不同场景、环境,需要引入或者不引入相关的依赖,或者因为外部组件等原因,依赖的版本不一样。
例子:
- A客户需要RabbitMQ,B客户需要Kafka
- A环境的依赖版本是2.13.3,B环境是2.12.7
解决方案
利用maven的profile,来设置不同环境各自的信息,比如可以配置属性值、依赖等等
1. A客户需要RabbitMQ,B客户需要Kafka
pom.xml配置
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>rabbitmq</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>kafka</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
A客户
A客户的profile里引入rabbitmq,比如:
applications.properties
spring.profiles.active=dev,rabbitmq
此时可以在application-rabbitmq.properties里配置上rabbitmq相关的属性
B客户
B客户的profile里引入kafka,比如:
applications.properties
spring.profiles.active=dev,kafka
2.A环境的依赖版本是2.13.3,B环境是2.12.7
不同环境,因为其它依赖或者外部组件等原因,需要使用不同版本的依赖
pom.xml配置
A客户
A客户的profile里引入env1,比如:
applications.properties
spring.profiles.active=dev,env1
此时可以在application-rabbitmq.properties里配置上rabbitmq相关的属性
B客户
B客户的profile里引入env2,比如:
applications.properties
spring.profiles.active=dev,env2