Gradle java project replaces one line in a file at build time

I have a simple Gradle build script to compile and package (like an application plugin) my Java application. The only thing I do not do is replace the current version number in a simple .properties file.

I created the file 'src / main / resources / app-info.properties' with one line 'application.version = @version @'. No. I want to replace this version line whenever a file is copied to the build folder (think this happens during build).

I have already tried a simple solution with ReplaceTokens ants. This replaced the version, but also broke my .png files in resources.

So, is there a simple solution to just replace the tokens in one file during the build task (or does any task process the copy in the build folder)?

Thanks for any help! Ben

====== Edit based on a comment from Opal =====

Based on the hint, I added the following:

import org.apache.tools.ant.filters.ReplaceTokens // ... build { from('src/main/resources') { include '*.properties' filter(ReplaceTokens, tokens: [version : project.version]) } } 

What causes this error:

Could not find method from () for arguments [src / main / resources, build_vbjud9ah7v3pj5e7c5bkm490b $ _run_closure6_closure12 @ 43ead1a8] in the root project

Does it seem like I'm in the wrong task?

====== Editing the completeness of adding an Opals-based solution offers =====

Thanks to the man, the following working solution!

 processResources { from('src/main/resources') { include '*.properties' filter(ReplaceTokens, tokens: [version : project.version]) } } 
+6
source share
2 answers

Books and blogs, including an answer from Opal, recommend a vibrant mix of exclude/include , from() and filter() . And, of course, on the first attempt, I replaced the text {{app javascript library}} in the index.html file with the JavaScript library path, which depended on a simple project property setting.

The problem that struck me was that my β€œmilitary” task produced duplicate index.html files in the military archive and got rid of this problem using the template described earlier, as a result there was one huge unreadable hack.

Then I found a really direct solution. The following example is from my own build script, and you should tweak it a bit to suit your needs:

 war { eachFile { copyDetails -> if (copyDetails.path == 'index.html') { filter { line -> line.replace('{{app javascript library}}', "lib/someLib.js") } } } } 
+7
source

Insert sample code. What you need to do is include the replacement file and exclude other files from it. An example is used here . Find ReplaceTokens and you'll see what I'm talking about.

You need to add filtering to the processResources task. Code example:

 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 are copied. replace_tokens is a filter taken from a map defined in another part of the script.

+3
source

Source: https://habr.com/ru/post/970869/


All Articles