我是Gradle的新手。我正在使用Eclipse Buildship 2.1.1。我想在源代码中包含的属性文件中包含构建时间戳。我在build.gradle中做了类似的事情,
war {
def propertyFile = file
"src/main/java/com/about/About.properties"
def props = new Properties()
propertyFile.withReader { props.load(it) }
println "Before setProperty:"
println props.getProperty('releaseDate')
props.setProperty('releaseDate', new Date().format('yyyy-MM-dd-HH:mm:ss'))
// propertyFile.withWriter { props.store(it) }
println "After setProperty:"
println props.getProperty('releaseDate')
buildDir = new File(rootProject.projectDir, "gradleBuild/" + project.name)
}
这会正确打印以下详细信息,
Before setProperty:
2017-11-22T19:28
After setProperty:
2017-11-24-10:53:57
但这并没有反映在属性文件本身中。如果删除propertyFile.withWriter { props.store(it) }
的评论,则会抛出错误,
No signature of method: java.util.Properties.store() is applicable for argument types: (java.io.BufferedWriter) values: [java.io.BufferedWriter@9a24ce]
Possible solutions: store(java.io.OutputStream, java.lang.String), store(java.io.Writer, java.lang.String), sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), size()
这种做法是对的还是我应该以其他方式做到这一点?
答案 0 :(得分:1)
您必须将属性写回文件才能更新文件。行propertyFile.withWriter { props.store(it) }
抛出错误,因为store
方法需要2个参数。第二个参数是在文件顶部添加的注释,但如果您不需要,则可以是null
:
propertyFile.withWriter { props.store(it, null) }