Wondering how to overwrite duplicate files while b...
# community-support
s
Wondering how to overwrite duplicate files while bunding the war. Not finding any overwrite duplicate strategy in documentation. If i use EXCLUDE strategy, its keeping only the first file and ignoring my 2nd file file. If i use INCLUDE strategy, its keeping both files in war but i need to keep only the 2nd file. and rest of the options(FAIL, WARN, INHERIT) will not help in this case. Hope am not the first one facing this challenge.
m
It might help to step back and ask why you ended up with duplicate JARs in the first place. It's hard to develop a strategy without more context. Here's my example: I've run into this problem before for
installDist
tasks in a Gradle multi-project build. The sub-projects were hierarchically organized, but JAR names default to project names. E.g.,
:thing1:api
and
:thing2:api
both produce a JAR named
api.jar
. In my case, the "strategy" was to add this to my convention plugin (i.e.,
buildSrc/src/main/kotlin/buildlogic.java-conventions.gradle.kts
):
Copy code
tasks.withType<Jar> {
    archiveBaseName = "${rootProject.name}-${project.path.substring(1).replace(':', '-')}"
}
That ensured that each sub-project has a unique JAR name. But it's hard to say what the best solution is in your case without more context as to the source of the duplicates.
s
Not sure what is the hesitation to add OVERWRITE duplicate strategy. i guess this will help a lot. FYI, below is my scenario: I have file called "default.html" in webapp folder, this file have basic static content where my project is getting used. this default.html file will serve only in local/developer environments. in addition I have an angular project and when i build the the same, in dist folder "default.html" file is generating. When am packing the war, plugin automatically copying the webapp folder default.html file to to the root war folder and when am trying to copy dist folder content to the root of the war, i wanted to overwrite the previously copied html file from webapp folder.
v
Not sure what is the hesitation to add OVERWRITE duplicate strategy. i guess this will help a lot.
Just a wild guess, but probably for archive tasks this is checked on-the-fly, so the contents of the first file are already written to the archive and overwrite would no longer work. But anyway, I've yet to see any valid use-case for a valid duplicate strategy besides
FAIL
. All cases I had so far where Gradle complained about missing duplicates strategy setting one would just have been symptom treatment. And it is especially problematic because you can only set it for all files, not for single files, so if you set it now due to this file, future problems like this will resolved the same way which might not be correct. Instead fix where the problem is coming from and make sure there are no duplicates, for example by proper excludes.