星期日, 8月 09, 2015

[Java] Granule Asset 函式庫測試

記錄一下使用Granule來編譯js/css檔的過程,雖然專案有點舊了,但試用起來效果還不錯。
主要是看上有JSP Tag很好整合。

功能如下

  1. Combine and compresses JS and CSS using different methods: on fly or in build process, CSS and JS fast compression or more sophisticated Google Closure compression, or just simple file combining.
  2. No-lock in solution. The tag just put around existing scripts. The tag can be turn on/off on the different levels: page and application.
  3. Debug and production modes.
  4. Calculate dependencies using Closure Library package/namespace system.
  5. Can automatically choose optimization methods.
  6. Multiple combinations of JS/CSS even with different compression methods on one page.
  7. Support JSP includes.
  8. Several types of cache, memory and file.
  9. Automatically regenerates the bundle if you modify an included file.
  10. Proxy-friendly GZip support.
  11. Rewrites relative URLs in your CSS files.
  12. JSP, JSF, Grails integration.
  13. Multiple loggers support (SLF4J, Log4J, Apache Logger)
  14. Can be setup to preserve license headers of JS libraries.
  15. JDK1.5 and higher even for Google Closure Compiler.

安裝


  1. 將Granule的jar檔放入你專案的library
  2. 在要套用的jsp網頁上面放置以下tag
    1. <%@ taglib uri="http://granule.com/tags" prefix="g" %>
  3. 配置web.xml檔

網頁配置

透過g:compress tag可以將範圍內所包含的js或inline js打包成一包。

執行結果



其他功能

  1. 加入設定檔

看了一下TagCacheFactory原始碼有三種設定檔,可以讓我們切換專案模式。
備註: 如果更新properties要重開tomcat才有效

public final static String PRODUCTION_PATH = "/WEB-INF/granule.properties";
public final static String DEBUG_PATH = "/WEB-INF/granule.debug.properties";
public final static String DEVELOPER_PATH = "/WEB-INF/granule.developer.properties";

   2. 啟用檔案快取

# cache - type of cache
# values: 
#   memory - all combined and compressed allocated in the server memory 
cache=disk
#cache=memory

之後會在專案路徑\build\web\granulecache\之下產生實體檔案,如果你的js有修改,檔案的修改日期也會變更的



沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails