Excluding module from configuration seems to remove from inherited configurations too

This is as designed. Exclude rules defined on a configuration are inherited by child configurations and compileOnly.extendsFrom compile. This limits the ability to globally exclude things in parent configurations that need to be included in child configurations. Alternatively, you can still do module level excludes.