Extract logic for duplicate file removal so it can be shared

.. between Maven and Gradle plugins. Also fixed bug in recursive
scanning logic.

Really fixes gh-614