依赖spring-boot-starter-parent后maven-resources-plugin的行为备忘录
简而言之
在使用继承spring-boot-starter-parent创建pom的项目中,如果要使用maven-resources-plugin的资源过滤功能,则需要使用@@来包围要过滤的部分,而不是${}。(在Spring Boot的手册中也提到了此事)
重要的事情
我想在使用Spring Boot的Web应用中实现缓存破坏。
试图实施
春天框架中有一个使用VersionResourceResolver来方便地添加指纹的功能,但是这次因为文件名改变会有问题,所以无法使用那个功能。因此,我们决定按照以下步骤来实现:
1. 在Maven构建时使用${maven.build.timestamp}获取日期,并将其设置到属性文件中。
2. 在HTML页面中通过thymeleaf获取该日期,并将其设置为查询字符串。
迷恋之处 zhī chù)
在我尝试以下的情况下,属性文件中没有设置构建日期。
设置应该没有错误……
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<project.build.timestamp>${maven.build.timestamp}</project.build.timestamp>
<maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
...
# Cache busting
app.build.timestamp=${project.build.timestamp}
<link rel="stylesheet" type="text/css" th:href="'/css/common.css?v=' + @{__${@environment.getProperty('app.build.timestamp')}__}" />
<script type="text/javascript" th:src="'/js/commons/common.js?v=' + @{__${@environment.getProperty('app.build.timestamp')}__}"></script>
理由
辛勤搜索后,我发现答案在手册中有写。
http://stackoverflow.com/questions/35417086/maven-resource-filtering-with-spring-boot-could-not-resolve-placeholder
http://docs.spring.io/spring-boot/docs/1.3.0.RELEASE/reference/html/howto-properties-and-configuration.html#howto-use-short-command-line-arguments
在使用spring-boot-starter-parent继承创建pom的情况下,简言之,maven-resources-plugin用于过滤的令牌将从${}更改为@@。
总的来说,这样做是正确的。
# Cache busting
app.build.timestamp=@project.build.timestamp@