gradle学习(18)-ant的属性

本文介绍了如何在Gradle项目中集成Ant任务,包括设置和获取Ant属性、路径引用及参考值。通过具体示例展示了如何操作这些配置,并提供运行结果以验证配置的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.设置ant属性


build.xml:


<project>
<target name="hello">
        <echo>buildDir=${buildDir}</echo>
 </target>
</project>

我们要给buildDir属性设值


ant.importBuild ('build.xml'){
        antTargetName ->'a-'+antTargetName
}
task intro << {
        println 'Hello,from gradle'
}
ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name:'buildDir',location:buildDir)


2.获取ant属性值


build.xml中定义属性:


<project>
<property name="antProp" value="a property defined in an Ant build"/>
<target name="hello">
        <echo>buildDir=${buildDir}</echo>
 </target>
</project>

gradle.xml获取属性


ant.importBuild ('build.xml'){
        antTargetName ->'a-'+antTargetName
}
task intro << {
        println 'Hello,from gradle'
}
ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name:'buildDir',location:buildDir)
println ant.antProp


执行gradle命令,输出如下:


qianhuis-Mac-mini:0112 qianhui$ gradle intro
a property defined in an Ant build
:intro
Hello,from gradle

BUILD SUCCESSFUL

Total time: 3.253 secs


3.设置reference的值


build.xml


<project>
<property name="antProp" value="a property defined in an Ant build"/>
<path refid="classpath"/>
<target name="hello">
        <echo>buildDir=${buildDir}</echo>
 </target>
</project>


最后三行是设置代码


ant.importBuild ('build.xml'){
        antTargetName ->'a-'+antTargetName
}
task intro << {
        println 'Hello,from gradle'
}
ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name:'buildDir',location:buildDir)
println ant.antProp
ant.path(id:'classpath',location:'libs')
ant.references.classpath = ant.path(location:'libs')
ant.references['classpath'] = ant.path(location:'libs')


4.获取reference的值


build.xml


<project>
<property name="antProp" value="a property defined in an Ant build"/>
<path refid="classpath"/>
<path id="antPath" location="libs"/>
<target name="hello">
        <echo>buildDir=${buildDir}</echo>
 </target>
</project>



build.gradle


ant.importBuild ('build.xml'){
        antTargetName ->'a-'+antTargetName
}
task intro << {
        println 'Hello,from gradle'
}
ant.buildDir = buildDir
ant.properties.buildDir = buildDir
ant.properties['buildDir'] = buildDir
ant.property(name:'buildDir',location:buildDir)
println ant.antProp
ant.path(id:'classpath',location:'libs')
ant.references.classpath = ant.path(location:'libs')
ant.references['classpath'] = ant.path(location:'libs')

println ant.references.antPath
println ant.references['antPath']

输出如下:


qianhuis-Mac-mini:0112 qianhui$ gradle intro
a property defined in an Ant build
/Users/qianhui/Documents/Developer/gradle_project/0112/libs
/Users/qianhui/Documents/Developer/gradle_project/0112/libs
:intro
Hello,from gradle

BUILD SUCCESSFUL

Total time: 3.001 secs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值