[英]How to read a property from an ant build file to a gradle build script?
I am looking for a possibly clean and concise way of extracting a property from ant's build.xml
file. 我正在寻找一种可能简洁明了的方法来从ant的
build.xml
文件中提取属性。
I know I could use ant.importBuild 'build.xml'
but this would import all the targets, possibly causing name clashes eg with the java plugin, which is a known issue . 我知道我可以使用
ant.importBuild 'build.xml'
但这会导入所有目标,可能会导致名称冲突,例如与Java插件冲突,这是一个已知问题 。
I am using gradle 1.6. 我正在使用gradle 1.6。
One way is to manually parse the XML file, for example using Groovy's XmlSlurper
class. 一种方法是手动解析XML文件,例如使用Groovy的
XmlSlurper
类。 However, this won't replace any property references occurring in the property value. 但是,这不会替换属性值中出现的任何属性引用。 Another way is to configure an
org.apache.tools.ant.Project
object (similar to how ant.importBuild
does it), and get the property value from there. 另一种方法是配置
org.apache.tools.ant.Project
对象(类似于ant.importBuild
工作方式),然后从那里获取属性值。 Something like: 就像是:
import org.apache.tools.ant.Project
import org.apache.tools.ant.ProjectHelper
task printPropertyValue {
doLast {
def antProject = new Project()
ProjectHelper.configureProject(antProject, file("build.xml"))
def value = antProject.getProperty("some.property")
while (value.contains('${')) {
value = antProject.replaceProperties(value)
}
println value
}
}
There may be a better way to recursively replace property references, but I couldn't find one. 递归替换属性引用可能有更好的方法,但是我找不到。
Another potential solution is to use an external properties file that's read both by Ant and Gradle. 另一个可能的解决方案是使用由Ant和Gradle读取的外部属性文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.