gradle学习(18)-ant的属性
最后更新于:2022-04-01 06:51:59
## 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
~~~