如何强制maven检查xml文件是否包含某些内容?

时间:2016-08-01 11:19:53

标签: xml maven svn maven-plugin

我使用SVN externalproeprties设置版本作者和版本,但这是一个包含数百个文件的大项目,有时开发人员忘记添加以下谎言:

<?xml version="1.0" encoding="UTF-8"?>
<!-- $Rev:: 1161                 $:  Revision of last commit.-->
<!-- $Author:: Xelian  $:  Author of last commit.-->
<!-- $Date:: 2016-07-11 13:13:20#$:  Date of last commit.-->

是否有一种简单的方法可以检查来自给定目录的文件是否包含<!-- $Rev::<!-- $Author::<!-- $Date::以及Maven构建是否失败?

我查找了enforcer插件,但它没有提供这样的规则。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用checkstyle。

Checkstyle有规则验证文件是否包含特定标头: http://checkstyle.sourceforge.net/config_header.html

maven有一个插件: https://maven.apache.org/plugins/maven-checkstyle-plugin/

答案 1 :(得分:0)

所以我自己找到了答案:

<properties>
    <groovyVersion>2.4.7</groovyVersion>
    <gmavenplus-plugin.version>1.5</gmavenplus-plugin.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>${groovyVersion}</version>
    </dependency>
</dependencies>

<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus-plugin.version}</version>
<executions>
    <execution>
        <phase>generate-sources</phase>
        <goals>
            <goal>execute</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <scripts>
        <script>file:/${pom.basedir}/scripts/Script.groovy</script>
    </scripts>
</configuration>

然后我用Groovy创建一个脚本:

import groovy.io.FileType

def list = []
def dirToCheck = new File(project.basedir,"dirToCheck")

dirToCheck.eachFile(FileType.FILES) { currentFile ->
    String fileContent = currentFile.getText("UTF-8");
    if(!fileContent.toLowerCase().startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"".toLowerCase())){
        throw new IllegalArgumentException("\n File:"+ currentFile.absolutePath+" does not starts with xml declaration");
    }
}

然后执行

mvn generate-sources

如果出现错误,Build将失败。