理一理Maven的phase, goal, surefire, failsafe

理一理Maven的phase, goal, surefire, failsafe

一直在用Maven,可是今天在引用了一個插件,想要執行插件的goal的時候卻完全懵了。執行mvn test的時候分不清surefirefailsafe的作用。

這可怎麼行,至少要知道大概的原理纔行。

首先phase是類似於Spring,Tomcat的lifecycle一樣的各個階段,這些phase是有順序的。對Maven來說,主要的phase如下:

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - 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 met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - 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-pluginmaven-failsafe-plugin插件吧。

從功能來說,一個是fail fast,一個是fail safe。從生命週期階段上來說maven-sure-firetest階段的,maven-failsafe-plugin是屬於integration-test階段,這個階段在packageverify之間,用來運行集成測試。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章