Groovy: Remplacer une séquence de caractères dans une String et charger une Properties depuis une file
게시: 17 July 2019
Voici un bout de code fonctionnel en groovy,
pour changer toutes les occurrences d’une séquence de caractères dans une String
et charger un fichier .properties dans un objet properties.
Exécuter une commande dans une String.
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
아키텍처 « 독립 플러그인 + 소비자 루트 » : 왜 내 Gradle 빌드가 중복되는가
14 May 2026
14 May 2026