Whenever i add a property to gradle.properties file the comments in the file get erased. I'd like to preserve all comments. A comment is defined as anything with a #
The task im using to update the gradle.properties file looks like this:
task updateSomeProperty << {
if(project.hasProperty('mynewProperty')) {
File gradleProperties = file("gradle.properties")
Properties props = new Properties()
props.load(new FileInputStream("gradle.properties"))
props.put("moldProperty",project.getProperty('mynewProperty'))
props.store(new FileWriter(gradleProperties),null);
println 'Updated old property to new property.'
}
}
and on the command line i run gradle updateSomeProperty -PmynewProperty=1234
everything works and the property gets injected into the file but the comments in the file get cleared. Notice how i am using null in props.store(new FileWriter(gradleProperties),null); i was thinking if i passed a null comment it would keep the comment but its not working. how do i do this ?
if i cant preserve the comments, is there a way to get all the comments and then i can put them back with the fileWriter command: props.store(new FileWriter(gradleProperties),MY_COMMENTS);
Unfortunately, because you are using a plain java properties instance, according to Properties#store() method, comments parameter is a new comment, furthermore it is just the comment at the first linee of the newly saved properties file. If you want to store previews comments, you should try some other solution. For example, you have to provide your own implementation pf properies and use it to store a new property within exosting properties file. You can find some examp,es on internet, like one here. But sure, it doesn't seem to be straightforward for Gradle script.