title: How can I modify Java source code in my Gradle project prior to compilation without modifying the files on disk?
body: <p>I have a Gradle project. In my source code I have some some annotations (among other things) which take the same string. However, this same string exists in a config file, and I would like to only reference that file, so I only have to change this string in one place in order to change all instances of it. Because of this, I would have to read the string from file, meaning the string isn't compile-time constant. Thus I can't use the string read from the file as an annotation argument.</p>
  
  <pre><code>// I have this.
  @Annotation("some_string")
  Object a;
  
  @Annotation("some_string")
  Object b;
  
  
  // This doesn't work
  String ss = // read a file, fetch the string from the file.
  
  @Annotation(ss)
  Object a;
  
  @Annotation(ss)
  Object b;
  </code></pre>
  
  <p>I was wondering if there was a way within Gradle to modify the source code prior to compilation in a way that allows me to have a single variable control all the annotation arguments.</p>
  
  <p>I.e., in the source code I could have <code>@Annotation("%%some_string%%")</code> and replace all occurrences of <code>%%some_string%%</code> with the variable in the Gradle task. It would be easy enough to simply modify each source file with something like</p>
  
  <pre><code>def annotation_variable = // read a file, fetch the string from the file.
  for ( f in srcFileTree ) {
      def text = file(f).text
      file(f).text = text.replaceAll("%%some_string%%", annotation_variable)
  }
  </code></pre>
  
  <p>However, that would permanently modify the source file. So, I'm looking for a way to modify the file in the pipeline, without actually modifying the file on disk. Is there anyway to do this?</p>