1. 程式人生 > >java專案打包加版本號清理快取二

java專案打包加版本號清理快取二

一篇我們主要介紹了在程式碼中怎麼處理可以做到清理快取的效果,這篇我們說說在打包的時候加版本號,意義上就是給靜態資源加版本號,意思我們每次打包的靜態頁面都是一個全新的頁面,通過這種方式來達到清理快取的目的;為達目標不擇手段嗎!

1.maven專案打包增加版本號,利用maven外掛:com.google.code.maven-replacer-plugin

這種方式在專案打包時執行,自動在靜態檔案後追加版本號;不需要修改任何程式碼;

<properties><!--時間戳格式-->
    <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>  
		    	<groupId>com.google.code.maven-replacer-plugin</groupId>  
		    	<artifactId>replacer</artifactId>  
		    	<version>1.5.3</version>  
			    <executions>  
			     <execution>  
			      <phase>prepare-package</phase>  
			      <goals>  
			       <goal>replace</goal>  
			      </goals>  
			     </execution>  
			    </executions>  
		    	<configuration>  
				    <basedir>${basedir}/src/main/webapp</basedir>
				    <includes>  
				        <include>**/*.html</include> 
				        <include>**/*.jsp</include> 
				    </includes> 
				    <regex>false</regex>  
				    <replacements>  
				         <replacement>  
	                         <token>.css"</token>  
	                         <value>.css?v=${maven.build.timestamp}"</value>  
	                     </replacement>  
	                     <replacement>  
	                         <token>.css'</token>  
	                         <value>.css?v=${maven.build.timestamp}'</value>  
	                     </replacement>  
	                     <replacement>  
	                         <token>.js"</token>  
	                         <value>.js?v=${maven.build.timestamp}"</value>  
	                     </replacement>  
	                     <replacement>  
	                         <token>.js'</token>  
	                         <value>.js?v=${maven.build.timestamp}'</value>  
	                     </replacement>
				    </replacements>  
				</configuration>   
		   </plugin>
        </plugins>
    </build>

2.非maven專案打包使用ant

可使用這個target;如果不知道怎麼使用ant打包build.xml可以看我關於jenkins中關於ant打包的介紹;

<target name="finddir" >
	<echo>檢索檔案,進行替換js版本</echo>
	<tstamp>
		<format property="htmlFiltTime" pattern="yyyyMMddHHmmss" offset="0"  unit="minute"/>
	</tstamp>	
	<replace dir="${appdirHtml}"  includes="**/*.html" encoding="UTF-8">
		<replacefilter token="{version}" value="${htmlFiltTime}"/>
	</replace>
</target>