Jar task creating archive with *.class twice

Continuing the discussion from Jar file adding duplicate files in services folder:

Continuing the discussion from Custom jar task producing archive with every *.class twice:

I added this block to my build.gradle to avoid duplicate insertion of class files into my jar when using the same output directory for both classes and resources. Does this seem like a reasonable way to proceed?

tasks.withType(Jar) {
  def seen = [:]
  eachFile {
    if (seen.containsKey(it.path)) {
      it.exclude()
    } else {
      seen[it.path] = true
    }
  }
}

If you don’t mind using an incubating feature, it looks like this works too.

tasks.withType(Jar) {
  eachFile {
    it.duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  }
}

The Gradle shadow plugin supports merging service files.