如果想檢查java代碼的質(zhì)量,那么使用checkstyle plugin就可以.但是怎么檢查其他文本文件的內(nèi)容呢?可以使用antrun這個(gè)插件.
比如,我們的maven工程中維護(hù)了數(shù)據(jù)庫(kù)歷史上所有的DDL語(yǔ)句,這樣可以在測(cè)試數(shù)據(jù)庫(kù)和生產(chǎn)數(shù)據(jù)庫(kù)利用增量DDL來(lái)同步數(shù)據(jù)庫(kù)結(jié)構(gòu),保證開(kāi)發(fā)、測(cè)試和生產(chǎn)數(shù)據(jù)庫(kù)的schema是一致的.但是從一些可視化客戶端工具中產(chǎn)生的DDL語(yǔ)句,往往先把表drop掉,這種語(yǔ)句在生產(chǎn)環(huán)境上執(zhí)行比較危險(xiǎn).這種情況下可以告訴同事,要求上傳DDL語(yǔ)句時(shí)仔細(xì)檢查.但是可以更自動(dòng)一些,需要檢查SQL文件,如果發(fā)現(xiàn)drop table語(yǔ)句,及時(shí)發(fā)出警報(bào).
在maven工程中,利用antrun插件,可以實(shí)現(xiàn)這個(gè)功能
在build中加入如下plugin
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
<configuration>
<target>
<fail message="found drop statement">
<condition>
<resourcecount count="0" when="gt">
<fileset dir="scheme">
<contains text="DROP TABLE" ignorewhitespace="yes"
casesensitive="no" />
</fileset>
</resourcecount>
</condition>
</fail>
</target>
</configuration>
</plugin>
執(zhí)行的步驟
1 fileset指定schema文件夾下的DDL sql文件
2 contains查找包含了"DROP TABLE"的文件,查找過(guò)程中忽略大小寫(xiě),忽略空格
3 resourcecount得到上一步中符合條件的文件數(shù)量
4 如果符合條件的文件數(shù)量比0大,則fail本次構(gòu)建