How to overwrite classes in an existing jar?

I have some curious project inheritance, where we use a Jar from some other project, but where we do overwrite some classes with the same name and package path as those in the jar. So the build task should be: - compile sources - jar sources into existing jar.

I have tried with overriding task jar like this:

task jar(type: Jar, overwrite: true) {
 from zipTree(lib_xxx.xxx_bof_dep90)
   from sourceSets.main.output.classesDir

This works as the jar task puts the compiled classes into the existing jar - but it leaves existing classes in that jar. So in effect, in the new jar I have duplicate classes.

How can I tell Gradle to overwrite existing classes with the ones just compiled?

Overwriting archive files isn’t currently supported. You either have to go through a temporary directory, or use the Ant Jar task.

“go through a temporary directory”’?

Do I have to - inflate the existing jar into that temporary dir,

  • copy the build classes into that dir, overwritting existing classes and

  • then do the jar task with “from” argument of that temporary directory?

Yes. Or you patch the Jar with Ant’s Jar task.

ok, thanks for your support!