Gradle java 项目在构建期间替换文件中的单行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24231207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle java project replace single line in file during build
提问by Ben
I have a simple Gradle build script to compile and package (similar to the application plugin) my Java application. The only thing I do not accomplish is to replace the current version number in a simple .properties file.
我有一个简单的 Gradle 构建脚本来编译和打包(类似于应用程序插件)我的 Java 应用程序。我唯一没有完成的是在一个简单的 .properties 文件中替换当前版本号。
I have created a file 'src/main/resources/app-info.properties' with a single line 'application.version = @version@'. No I want to replace this version string whenever the file is copied to the build folder (think this happens during the build task).
我创建了一个文件 'src/main/resources/app-info.properties',其中包含一行 'application.version = @version@'。不,每当将文件复制到构建文件夹时,我都想替换此版本字符串(认为这是在构建任务期间发生的)。
I already tried a simple solution with ants ReplaceTokens. This one replaced the version but also broke my .png files in the resources..
我已经用蚂蚁替换令牌尝试了一个简单的解决方案。这个替换了版本,但也破坏了我在资源中的 .png 文件。
So is there a simple solution to just replace tokens in one single file during the build task (or whatever task handles the copy to the build folder)?
那么是否有一种简单的解决方案可以在构建任务(或任何处理复制到构建文件夹的任务)期间仅替换单个文件中的令牌?
Thank you for any help! Ben
感谢您的任何帮助!本
====== Edit based on the comment from Opal =====
====== 根据 Opal 的评论进行编辑 ======
Based on the hint I have added the following:
根据提示,我添加了以下内容:
import org.apache.tools.ant.filters.ReplaceTokens
// ...
build {
from('src/main/resources') {
include '*.properties'
filter(ReplaceTokens, tokens: [version : project.version])
}
}
Which throws this error:
这引发了这个错误:
Could not find method from() for arguments [src/main/resources, build_vbjud9ah7v3pj5e7c5bkm490b$_run_closure6_closure12@43ead1a8] on root project
在根项目中找不到参数 [src/main/resources, build_vbjud9ah7v3pj5e7c5bkm490b$_run_closure6_closure12@43ead1a8] 的方法 from()
Seems like I am on the wrong task?
好像我做错了任务?
====== Edit for completeness adding the solution based on Opals suggest =====
====== 为完整性进行编辑,添加基于 Opals 的解决方案建议 =====
Thanks man, the following is the working solution!
谢谢男人,以下是有效的解决方案!
processResources {
from('src/main/resources') {
include '*.properties'
filter(ReplaceTokens, tokens: [version : project.version])
}
}
采纳答案by Martin Andersson
Books and blogs alike, including the answer from Opal all recommend using a vivid mixture of exclude/include
, from()
and filter()
. And of course, so did I on my first attempt to replace the text {{app javascript library}}
in a index.html
file to the path of a JavaScript library which depended on a simple project property setting.
书籍和博客,包括 Opal 的答案,都建议使用exclude/include
,from()
和filter()
. 当然,这样做我对我的第一次尝试的文本替换{{app javascript library}}
的index.html
文件,其依赖于一个简单的项目属性设置一个JavaScript库的路径。
The problem that hit me was that my 'war' task produced duplicated index.html files in the war archive and getting rid of the problem, using the pattern described previously, resulted in one huge unreadable hack.
困扰我的问题是我的“War”任务在War档案中产生了重复的 index.html 文件,使用前面描述的模式解决了这个问题,导致了一个巨大的无法读取的黑客攻击。
Then I found a really straight forward solution. The following example is from my own build script and you have to customize it a bit to suite your needs:
然后我找到了一个非常直接的解决方案。以下示例来自我自己的构建脚本,您必须对其进行一些自定义以满足您的需求:
war {
eachFile { copyDetails ->
if (copyDetails.path == 'index.html') {
filter { line ->
line.replace('{{app javascript library}}', "lib/someLib.js")
}
}
}
}
回答by Opal
Paste sample code. What You need to do is to includefile for replacement and excludeother files from replacement. Hereis sample usage. Search for ReplaceTokensand You'll see what am I talking about.
粘贴示例代码。您需要做的是包含要替换的文件并从替换中排除其他文件。这是示例用法。搜索ReplaceTokens,你就会明白我在说什么。
You need to add filtering to processResources
task. Sample code:
您需要为processResources
任务添加过滤。示例代码:
processResources {
def profile = project.properties['profile']
def replace_tokens = profile ? filter_tokens[profile] : filter_tokens['default']
exclude '**/log4j-test.xml'
from('src/main/resources') {
exclude '**/*.ttf'
filter(ReplaceTokens, tokens: replace_tokens)
}
from('src/main/resources') {
include '**/*.ttf'
}
}
Above ttf
(binary) files are excluded from filtering but copied. replace_tokens
is a filter taken from map defined in other part of the script.
以上ttf
(二进制)文件被排除在过滤之外但被复制。replace_tokens
是从脚本其他部分定义的地图中获取的过滤器。