理一理Maven的phase, goal, surefire, failsafe
一直在用Maven,可是今天在引用了一個插件,想要執行插件的goal的時候卻完全懵了。執行mvn test
的時候分不清surefire
和failsafe
的作用。
這可怎麼行,至少要知道大概的原理纔行。
首先phase是類似於Spring,Tomcat的lifecycle一樣的各個階段,這些phase是有順序的。對Maven來說,主要的phase如下:
validate
- validate the project is correct and all necessary information is availablecompile
- compile the source code of the projecttest
- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployedpackage
- take the compiled code and package it in its distributable format, such as a JAR.verify
- run any checks on results of integration tests to ensure quality criteria are metinstall
- install the package into the local repository, for use as a dependency in other projects locallydeploy
- done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
在執行phase的時候,會執行本身和它之前的phase
其次是goal。
我們現在有了生命週期,但是在每個build階段我們具體應該做什麼呢?這就是goal發揮作用了。而聰明的你肯定知道具體行爲肯定是一個實現,沒錯,這就是爲什麼goal會是plugin的一個屬性。當然一個plugin可以有多個goal。一個goal可以綁定到多個phase上去執行。
具體要執行某個插件的goal方法是: mvn groupId:artifactId:version:goal
最後來說一說maven-surefire-plugin
和maven-failsafe-plugin
插件吧。
從功能來說,一個是fail fast,一個是fail safe。從生命週期階段上來說maven-sure-fire
是test
階段的,maven-failsafe-plugin
是屬於integration-test
階段,這個階段在package
和verify
之間,用來運行集成測試。