在springmvc开发的时候,我们常常利用maven命令:package -P dev 来打包不同环境的配置文件,例如logback.xml,开发时日志级别为debug,生产为info
<!-- logback.xml -->
<root level="${maven.log.levle}">
</root>
<!--pom.xml -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<maven.log.level>debug</maven.log.level>
</properties>
</profile>
<profile>
<id>prd</id>
<properties>
<maven.log.level>info</maven.log.level>
</properties>
</profile>
</profiles>
但是在spring boot 打包时,你会发现${maven.log.levle}这个变量没有被替换,why?在spring-boot-starter-parent的pom中,maven-resources-plugin被重新定义了,为了避免与${}冲突,只能使用@
<artifactId>spring-boot-starter-parent</artifactId>
<properties>
<resource.delimiter>@</resource.delimiter>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>${resource.delimiter}</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
因此只需要这样修改即可,如果变量中本来就需要输出@,可以用\@进行转义
<!-- logback.xml -->
<root level="@maven.log.levle@">
</root>
spring boot关于maven的说明:
https://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#howto-automatic-expansion-maven
maven-resources-plugin插件说明:
http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html