Extracting files from dependency regardless of whether it is jar or project dependency


I know similar topics have been asked before regarding how to setup a task (usually something like extractApi) to extract a file from dependency jar/zip.

task extractApi(type: Copy) {
    from {
        configurations.compile.asFileTree.matching {
            include "**/*my*.jar"
        }.collectMany {

    include "**/Api.ts"

    into "${projectDir}/src"

However, when I am doing a composite build, e.g. my jar dependency will be substituted with a project dependency, the same extractApi task does not always work because, as what I suspect, my dependency project is using java-library plugin and assemble task is not executed to produce the jar.

Is it possible to write the extractApi task in a way that it does not care about the format of the dependency artifact? e.g. whether it is jar/zip archive or plain directory?