默認情況下, 我們修改 class 或者 修改模板文件(templates目錄 下面的文件) 等動態資源, 都不會立即自動生效。 在IDEA中, 我通過Ctrl + F9 , 仍然是無效。 當然, 靜態資源的修改是可以立即更新的, 但是也是需要Ctrl + F9 編譯一次, 另外前端瀏覽器需要F5刷新一遍。
有沒有好的辦法呢? 有!其實 boot 已經 提供了 devtools 這么一個工具。 在 pom.xml 的dependencies標簽配置下面的內容后, 就可以了!
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
對於 devtools , 我們還可以給設置optional 參數, 據說是可以提供編譯速度:
<optional>true</optional>
有人說,還需要配置一個 fork:
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!--fork : 如果沒有該項配置,肯呢個devtools不會起作用,即應用不會restart -->
- <fork>true</fork>
- </configuration>
- </plugin>
- </plugins>
我測試不是這樣的, fork 是 true false 都不要緊。 fork的作用好像不是這個。 我看了官方文檔是:
Flagging the dependency as optional is a best practice that prevents devtools from being transitively applied to other modules using your project
也就是說 避免 devtools 傳遞性的被應用到項目中其他的 module, 好像也不要緊,它僅僅是要給最佳實踐。
devtools 還有一個作用是把 spring.thymeleaf.cache 之類的緩存功能給關閉了。
這樣, 我們就可以隨意修改java 源碼和 動態資源文件了, 我測試過,新增刪除java方法或者java 文件,或者 動態資源文件, 都是可以生效的。
但是, 在IDEA中, 我們需要 按Ctrl + F9 , 有沒有辦法自動編譯呢? IDEA 默認不就是自動編譯的嗎? 為什么需要 Ctrl + F9? 有沒有辦法可以不 按Ctrl + F9呢, 當然也是可以的:
IDEA 配置
- CTRL + SHIFT + A 查找 勾選 make project automatically 選項
- ctrl+shift+alt+/ 查找Registry 勾選 compiler.automake.allow.when.app.running 選項
另外我觀察到, 修改java 文件, 按Ctrl + F9 就相當於重啟了一遍(觀察控制台日志可知)。而模板文件不是這樣的,它沒有什么日志打印出來, 貌似是僅僅替換了那個文件。
而boot 為DevTools 也是提供了一些配置的。 這些配置可以控制 哪些修改 是否導致restart :
# DEVTOOLS (DevToolsProperties)
spring.devtools.livereload.enabled=true # Enable a livereload.com compatible server. spring.devtools.livereload.port=35729 # Server port. spring.devtools.restart.additional-exclude= # Additional patterns that should be excluded from triggering a full restart. spring.devtools.restart.additional-paths= # Additional paths to watch for changes. spring.devtools.restart.enabled=true # Enable automatic restart. spring.devtools.restart.exclude=META-INF/maven/**,META-INF/resources/**,resources/**,static/**,public/**,templates/**,**/*Test.class,**/*Tests.class,git.properties # Patterns that should be excluded from triggering a full restart. spring.devtools.restart.poll-interval=1000 # Amount of time (in milliseconds) to wait between polling for classpath changes. spring.devtools.restart.quiet-period=400 # Amount of quiet time (in milliseconds) required without any classpath changes before a restart is triggered. spring.devtools.restart.trigger-file= # Name of a specific file that when changed will trigger the restart check. If not specified any classpath file change will trigger the restart. # REMOTE DEVTOOLS (RemoteDevToolsProperties) spring.devtools.remote.context-path=/.~~spring-boot!~ # Context path used to handle the remote connection. spring.devtools.remote.debug.enabled=true # Enable remote debug support. spring.devtools.remote.debug.local-port=8000 # Local remote debug server port. spring.devtools.remote.proxy.host= # The host of the proxy to use to connect to the remote application. spring.devtools.remote.proxy.port= # The port of the proxy to use to connect to the remote application. spring.devtools.remote.restart.enabled=true # Enable remote restart. spring.devtools.remote.secret= # A shared secret required to establish a connection (required to enable remote support). spring.devtools.remote.secret-header-name=X-AUTH-TOKEN # HTTP header used to transfer the shared secret.
默認改變 /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates 等目錄文件,會重新重啟項目?? ,當然我們編輯靜態文件不想重啟項目可以配置
spring.devtools.restart.exclude=static/**,public/**
我們可以通過:
spring.devtools.restart.enabled=false
禁止devtools 進行重啟,但是,這樣之后, 我們對java 文件的修改就不會生效,這時 Ctrl + F9 也沒用, 因為class 文件不會替換。 所以,看來重啟還是不可避免的。—— 不知道為什么devtools沒有提供類似 jrebel 的class熱加載的功能,通過重啟來實現熱加載實在是感覺有些low。
另外,我們可以設置 spring.devtools.restart.trigger-file= 屬性。 它可以減少我們的重啟次數(過多的重啟也確實很煩哦),但是,這樣之后,我們得記住那個特定的文件,當我們需要重啟的時候,我們得手動修改它,或者插件修改。 手動修改的話,我試過,感覺還不如不用devtools 得了, 每次我想重啟直接點擊重啟按鈕不是一樣的操作嗎?(當然,devtools 多了個reload的功能) , 使用插件的話, 沒找到什么好的插件。應該就是指 livereload ,jrebel之類的了吧。也感覺用起來不流暢。
總之,devtools 還是挺強大的,它提供了很多的配置。但仍有不足,期待能夠集成jrebel變得更好。
參考:
https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/#using-boot-devtools-restart
http://blog.csdn.net/isea533/article/details/70495714 這個完全就是上面官方文檔的翻譯。
http://www.logicbig.com/tutorials/spring-framework/spring-boot/trigger-file/