Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task replace string in .java file

Tags:

java

gradle

task

I want to replace few lines in my Config.java file before the code gets compiled. All I was able to find is to parse file through filter during copying it. As soon as I have to copy it I had to save it somewhere - thats why I went for solution: copy to temp location while replacing lines > delete original file > copy duplicated file back to original place > delete temp file. Is there better solution?

like image 337
Srneczek Avatar asked Nov 01 '15 16:11

Srneczek


People also ask

How do I pass arguments to Gradle task?

If the task you want to pass parameters to is of type JavaExec and you are using Gradle 5, for example the application plugin's run task, then you can pass your parameters through the --args=... command line option. For example gradle run --args="foo --bar=true" .

What is task type in Gradle?

Gradle supports two types of task. One such type is the simple task, where you define the task with an action closure. We have seen these in Build Script Basics. For this type of task, the action closure determines the behaviour of the task. This type of task is good for implementing one-off tasks in your build script.

How do I skip a task in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

What is .Gradle file?

gradle files are the main script files for automating the tasks in an android project and are used by Gradle for generating the APK from the source files.


1 Answers

May be you should try something like ant's replaceregexp:

task myCopy << {     ant.replaceregexp(match:'aaa', replace:'bbb', flags:'g', byline:true) {         fileset(dir: 'src/main/java/android/app/cfg', includes: 'TestingConfigCopy.java')     } } 

This task will replace all occurances of aaa with bbb. Anyway, it's just an example, you can modify it under your purposes or try some similar solution with ant.

like image 92
Stanislav Avatar answered Sep 20 '22 08:09

Stanislav