Gradle 任务替换 .java 文件中的字符串 [英] Gradle task replace string in .java file

查看:36
本文介绍了Gradle 任务替换 .java 文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译代码之前替换我的 Config.java 文件中的几行.我能找到的只是在复制文件时通过过滤器解析文件.一旦我必须复制它,我就必须将它保存在某个地方 - 这就是我寻求解决方案的原因:复制到临时位置,同时替换行> 删除原始文件 > 将复制的文件复制回原始位置 > 删除临时文件.有没有更好的解决方案?

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?

推荐答案

也许你应该试试 ant 的 替换正则表达式:

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')
    }
}

此任务将用 bbb 替换所有出现的 aaa.无论如何,这只是一个例子,您可以根据自己的目的进行修改或使用ant尝试一些类似的解决方案.

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.

这篇关于Gradle 任务替换 .java 文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆