Building and Testing with Gradle笔记3——Ant and Gradle

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cwt8805/article/details/53156429

Hello Ant

可以将Ant理解为Java世界的make,正如使用make需要编写Makefile。Ant也有类似Makefile的构建文件,只不过是使用xml来描述的。创建一个文件build.xml,内容如下:

<project>
    <target name="helloViaAttribute">
        <echo message="hello from Ant"/>
    </target>
</project>

然后进入该目录执行ant helloViaAttribute。即可输出hello from AnthelloViaAttribute是build文件中定义的一个target,类似于Gradle中的task,或者make中的目标。echo是ant中的一个内置task。事实上Gradle是Ant的一个超集,可以在Gradle中直接使用Ant的内置task。如下:

task hello << {
    String greeting = "hello from Ant"
    ant.echo(message: greeting)
}

Ant中的property类似于Gradle中的变量。如下分别是Ant和Gradle的构建文件:

<project>
    <property name="buildoutput" location="output"/>
    <property name="appversion" value="2.1"/>
</project>
def helloName = "Fird Birfle"
int personAge = 43

task hello << {
    println "Hello ${helloName}. I believe you are ${personAge} years of age."
}

Gradle中导入Ant的自定义Task

task checkTheStyle << {
    //Load the custom task
    ant.taskdef(resource: 'checkstyletask.properties') {
        classpath {
            fileset(dir: 'libs/checkstyle', includes: '*.jar')
        }
    }

    //Use the custom task
    ant.checkstyle(config: 'src/tools/sun_checks.xml') {
        fileset(dir: 'src')
    }
}

ant.taskdef方法加载jar文件中的一个属性文件,内容如下:

checkstyle=com.puppycrawl.tools.checkstyle.CheckStyleTask

前面的checkstyle是Ant中自定义的一个Task名称,后面则是具体的实现。这个地方和Gradle中自定义Task的方法类似。
接下来ant.checkstyle就开始直接使用这个自定的Task了。由于Ant缺乏对dependencies的管理,需要将自己下载jar包放入到libs里面。正如前面的checkstyle.jar。
使用Gradle的依赖管理方法,还可以这样加载和执行Ant的自定义Task。

configurations {
    myPmd
}

dependencies {
    myPmd group: 'pmd', name: 'pmd', version: '4.2.5'
}

repositories {
    mavenCentral()
}

task checkThePMD << {
    ant.taskdef(name: 'myPmdTask', classname: 'net.sourceforge.pmd.ant.PMDTask',
        classpath: configurations.myPmd.asPath)

    ant.myPmdTask(shortFilenames: 'true', failonruleviolation: 'true',
        rulesetfiles: file('src/tools/pmd-basic-rules.xml').toURI().toString()) {
            formatter(type: 'text', toConsole: 'true')
            fileset(dir: 'src/main/java')
    }
}

复杂的Ant配置

如下build.xml将samples目录中所有txt文件压缩成一个zip文件

<project>
    <target name="zipsourceInAnt">
        <zip destfile='samples-from-ant.zip'>
            <fileset dir= 'samples'>
                <include name='**.txt'/>
            </fileset>
        </zip>
    </target>
</project>

对应的Gradle写法如下:

task zipsourceInGradle << {
    ant.zip(destfile: 'samples-from-gradle.zip') {
        fileset(dir: 'samples') {
            include(name: '**.txt')
        }
    }
}

导入整个Ant建构文件

在build.gradle文件头部,添加如下一行,将整个ant建构文件导入Gradle

ant.importBuild 'build.xml'

然后build.xml文件中所有定义的target都可以当作Gradle中的task使用,如,gradle helloViaAttribute

Ant Target和Gradle相互依赖

没啥好说的,直接看代码:

<project>
    <target name="antStandAloneHello">
        <echo message="A standalone hello from an Ant target"/>
    </target>

    <target name="antHello" depends="beforeTheAntTask">
        <echo message="A dependent hello from the Ant target"/>
    </target>
</project>
ant.importBuild 'build.xml'

defaultTasks = ['antStandAloneHello', 'afterTheAntTask']

task beforeTheAntTask << {
    println "A Gradle task that precedes the Ant target"
}

task afterTheAntTask(dependsOn: "antHello") << {
    println "A Gradle task that precedes the Ant target"
}

直接执行gradle,默认执行defaultTasks列表中的task,结果输出:

:antStandAloneHello
[ant:echo] A standalone hello from an Ant target

:beforeTheAntTask
A Gradle task that precedes the Ant target

:antHello
[ant:echo] A dependent hello from the Ant target

:afterTheAntTask
A Gradle task that precedes the Ant target

使用AntBuilder

扫描目录文件

task echoDirListViaAntBuilder() {
    description = 'Uses the built-in AntBuilder instance to echo and list files'

    //Docs: http://ant.apache.org/manual/Types/fileset.html
    //Echo the Gradle project name via the ant echo plugin
    ant.echo(message: project.name)
    ant.echo(path)
    ant.echo("${projectDir}/samples")

    //Gather list of files in a subdirectory
    ant.fileScanner{
        fileset(dir:"samples")
    }.each{
        //Print each file to screen with the CWD (projectDir) path removed.
        println it.toString() - "${projectDir}"
    }
}

注意,Groovy中的字符串可以相减。

使用正则表达式

task echoSystemPropertiesWithRegEx() {
    description = "Uses Groovy's regex matching to find a match in System properties"

    def charsToFind = 'sun'
    println "SYSTEM PROPERTIES"
    System.properties.each{
        ant.echo("Does '${it}' contain '${charsToFind}': " + (it ==~ ".*${charsToFind}.*"))
    }
}

猜你喜欢

转载自blog.csdn.net/cwt8805/article/details/53156429
今日推荐