Gradle 从 Gradle 中调用 Ant


当前第2页 返回上一页

build.gradle

task check << {
    ant.taskdef(name: 'pmd', classname: 'net.sourceforge.pmd.ant.PMDTask', classpath: configurations.pmd.asPath)
    ant.pmd(shortFilenames: 'true', failonruleviolation: 'true', rulesetfiles: file('pmd-rules.xml').toURI().toString()) {
        formatter(type: 'text', toConsole: 'true')
        fileset(dir: 'src')
    }
}  

导入 Ant 构建

你可以使用 ant.importBuild()方法来向 Gradle 项目导入一个 Ant 构建。当您导入一个 Ant 构建时,每个 Ant 目标被视为一个 Gradle 任务。这意味着你可以用与 Gradle 任务完全相机的方式操纵和执行 Ant 目标。

导入 Ant 构建

build.gradle

ant.importBuild 'build.xml'
build.xml
<project>
    <target name="hello">
        <echo>Hello, from Ant</echo>
    </target>
</project>  

gradle hello 的输出结果

> gradle hello
:hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs  

您可以添加一个依赖于 Ant 目标的任务:

依赖于 Ant 目标的任务

build.gradle

ant.importBuild 'build.xml'
task intro(dependsOn: hello) << {
    println 'Hello, from Gradle'
}  

gradle intro 的输出结果

> gradle intro
:hello
[ant:echo] Hello, from Ant
:intro
Hello, from Gradle
BUILD SUCCESSFUL
Total time: 1 secs  

或者,您可以将行为添加到 Ant 目标中:

将行为添加到 Ant 目标

build.gradle

ant.importBuild 'build.xml'
hello << {
    println 'Hello, from Gradle'
}  

gradle hello 的输出结果

> gradle hello
:hello
[ant:echo] Hello, from Ant
Hello, from Gradle
BUILD SUCCESSFUL
Total time: 1 secs  

它也可以用于一个依赖于 Gradle 任务的 Ant 目标:

依赖于 Gradle 任务的 Ant 目标

build.gradle

ant.importBuild 'build.xml'
task intro << {
    println 'Hello, from Gradle'
}
build.xml
<project>
    <target name="hello" depends="intro">
        <echo>Hello, from Ant</echo>
    </target>
</project>  

gradle hello 的输出结果

> gradle hello
:intro
Hello, from Gradle
:hello
[ant:echo] Hello, from Ant
BUILD SUCCESSFUL
Total time: 1 secs  

Ant 属性和引用

有几种方法来设置 Ant 属性,以便使该属性被 Ant 任务使用。你可以直接在 AntBuilder 实例上设置属性。Ant 属性也可以从一个你可以修改的 Map 中获得。您还可以使用 Ant property 任务。下面是一些如何做到这一点的例子。

Ant 属性设置

build.gradle

ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name: 'buildDir', location: buildDir)
build.xml
<echo>buildDir = ${buildDir}</echo>  

许多 Ant 任务在执行时会设置一些属性。有几种方法来获取这些属性值。你可以直接从AntBuilder 实例获得属性。Ant 属性也可作为一个 Map。下面是一些例子。

获取 Ant 属性

build.xml

<property name="antProp" value="a property defined in an Ant build"/>
build.gradle
println ant.antProp
println ant.properties.antProp
println ant.properties['antProp']  

有几种方法可以设置 Ant 引用:

Ant 引用设置

build.gradle

ant.path(id: 'classpath', location: 'libs')
ant.references.classpath = ant.path(location: 'libs')
ant.references['classpath'] = ant.path(location: 'libs')
build.xml
<path refid="classpath"/>  

有几种方法可以获取 Ant 引用:

获取 Ant 引用

build.xml

<path id="antPath" location="libs"/>
build.gradle
println ant.references.antPath
println ant.references['antPath']  

标签:Gradle

返回前面的内容

相关阅读 >>

Gradle 教程 - 杂七杂八

Gradle war 插件

Gradle checkstyle 插件

Gradle scala 插件

Gradle 构建基础

Gradle java 构建入门

Gradle jetty 插件

Gradle jdepend 插件

Gradle osgi 插件

Gradle 简介

更多相关阅读请进入《Gradle》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...