Groovy: 替换字符串中的字符序列并从文件加载 Properties
发布于 17 July 2019
这是一段可运行的 Groovy 代码,
要替换字符串中所有字符序列的出现
并将一个 .properties 文件加载到 properties 对象中。
在字符串中执行命令。
replaceTextinFile.groovy
#!/usr/bin/env groovy
println System.getProperty("user.home")
static String getSeparator_() {
System.getProperty("file.separator")
}
String separator = separator_
String path = System.getProperty("user.home")
.concat("${separator}.gradle" +
separator +
"gradle.properties")
File propertiesFile = new File(path)
assert propertiesFile.exists() && !propertiesFile.directory
Properties gradleProperties = new Properties()
propertiesFile.withInputStream { InputStream it ->
gradleProperties.load(it)
}
String user_name = gradleProperties.getProperty("user_name")
String text
String resultText
text = new File("${System.getProperty("user.home")}" +
separator +
"src" +
separator +
"cheroliv.github.io" +
separator +
"src" +
separator +
"main" +
separator +
"resources" +
separator +
"eco_space.txt").text
String to = separator +
"media" +
separator +
"${user_name}" +
separator +
"320" +
separator +
"videoCours" +
separator
String from = "${System.getProperty("user.home")}" +
separator +
"p2p" +
separator
resultText = text.replace(to, from)
println resultText
//execute command line in a string
println "rsync -avr ${resultText} ${to}".execute().text
相关文章
HyperFrames-Gradle: 将 AsciiDoc 转换为 MP4 视频
31 May 2026
31 May 2026
L'Architecture « 插件独立+消费者根 » : 为什么我的Gradle构建会重复?
14 May 2026
14 May 2026
知识图谱作为推荐引擎:我是如何杀死按时间顺序的「相关文章」的
12 May 2026
12 May 2026