在Spring Boot开发中,经过Maven构建项目依赖是一件比较舒心的事,能够为咱们省去处理冲突等大部分问题,将更多的精力用于业务功能上。近期在项目中,因为项目集成了其余外部系统资源文件,须要根据不一样的环境实现加载不一样的JS资源文件,处理这个问题首先想到的即是经过判断当前环境实现动态加载JS,可是,在应用打包的时候已经明确了部署环境,对于静态资源的引用也是肯定的,为什么不在执行打包的过程当中指定引用的资源路径?没错,尤为在经过Spring Boot的Maven打包时,能够经过简单的几行代码来实现实现。下面给出一个简单的例子,好比,在html页面咱们须要动态的引入一个JS,JS的路径分别为http://www.cnblogs.com/funnyboy0128/test.js和http://www.cnblogs.com/funnyboy0128-test/test.js,假设分别对应到生产环境和开发环境,则处理以下:javascript
一、pom文件以下:html
<build>
<plugins>
<plugin>java
<groupId>org.springframework.boot</groupId>spring
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- mavne打包动态修改替换占位符 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>maven
<profiles>
<profile>
<id>dev</id>
<properties>
<funnyboy.env>http://www.cnblogs.com/funnyboy0128-test</funnyboy.env>
</properties>
</profile>spring-boot
<profile>
<id>pro</id>
<properties>
<funnyboy.env>http://www.cnblogs.com/funnyboy0128</funnyboy.env>
</properties>
</profile>测试
</profiles>ui
二、资源 文件,例如xx.html中引用JS以下:spa
<script type="text/javascript" src="@funnyboy.env@/test.js"></script>htm
三、打完包测试一下:
mvn package -Pdev后查看应用包的xx.html,内容以下:<script type="text/javascript" src="http://www.cnblogs.com/funnyboy0128-test/test.js"></script>
mvn package -Ppro后查看应用包的xx.html,内容以下:<script type="text/javascript" src="http://www.cnblogs.com/funnyboy0128/test.js"></script>
通过测试OK。