引言
在开发中遇到这样的情况:某些jar包只需要在开发环境使用,其它环境不使用,比如Swagger包。这时候就需要我们根据不同环境打包不同的jar包。
方法
查阅了Maven的文档,发现使用Maven打包的时候可以利用profile配置区分环境。
解决
在pom.xml加入profile配置
<profiles>
<profile>
<id>dev</id>
<dependencies>
<dependency>
<groupId>com.moda</groupId>
<artifactId>moda-swagger-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
打包的使用命令
mvn clean package -P dev
由于activeByDefault指定为默认,也可以不加 -P dev效果是一样
mvn clean package
其它环境,注意在dev前面加入“!”,表示禁止id=dev的配置
mvn clean package -P !dev
总结
通过profile可以指定多个环境,比如dev、test、uat、prod,每个环境可以定制化的配置,达到在不改动代码的情况下可以很方便的切换环境。