使用 gradle 复制任务从所有依赖项复制 META-INF

Copying META-INF from all dependencies using gradle copy task

使用 Gradle 复制任务时出现问题:

def loopJar(File file) {
    zipTree(file).findAll { that ->
        that
    }.collect { the ->
        if (the.getName().endsWith(".jar") || the.getName().endsWith(".aar")) {
            loopJar(the)
        }
        the;
    }
}

task fatJar(type: Copy) {
    into 'libs'
    include { it.toString().contains("META-INF") }
    from {
        configurations.compile.findAll {
            it.getName() != 'android.jar'
        }.collect {
            loopJar(it);
        }
    }
}

很简单,遍历所有依赖项,如果它们是存档,则遍历它们的 zipTrees 以确保它们本身没有 Jar。

完成此操作后,我想将 META-INF 文件夹中的所有文件复制到我的目录中。

出于某种原因,它正在将所有文件复制到目录中。但是,如果我在每个文件中打印ln,它只会显示我要复制的文件。

      file '/{Dir Structure Here}/build/tmp/expandedArchives/activeandroid-3.1.0-SNAPSHOT.jar_duhgtuy24rfyj4gq0cc29tyjl/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/LICENSE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/maven/com.fasterxml.jackson.core/jackson-annotations/pom.properties'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-annotations-2.4.1.jar_asorliiuo3vke643z99zgpvqb/META-INF/maven/com.fasterxml.jackson.core/jackson-annotations/pom.xml'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/LICENSE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/NOTICE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.properties'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/maven/com.fasterxml.jackson.core/jackson-databind/pom.xml'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-databind-2.4.1.3.jar_av3gri85ykjeek8zq4zffxj7c/META-INF/services/com.fasterxml.jackson.core.ObjectCodec'
file '/{Dir Structure Here}/build/tmp/expandedArchives/support-annotations-23.1.1.jar_59cc6fqykevukbtu3fq75wbi0/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/LICENSE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/MANIFEST.MF'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/NOTICE'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml'
file '/{Dir Structure Here}/build/tmp/expandedArchives/jackson-core-2.4.1.1.jar_cf7fa55yohvqtmkkzf26lmy55/META-INF/services/com.fasterxml.jackson.core.JsonFactory'

这是每个文件的输出。 然而所有文件都进入了 libs 文件夹...什么!?

本脚本的用法: 复制所有 meta-inf 文件(最终只是许可,注意信息)并将它们添加到我的 AAR 文件中它们自己的文件夹中,以允许其他希望包含我的 aar 的人这样做而不会出现文件冲突问题: Android Gradle plugin 0.7.0: "duplicate files during packaging of APK"

def loopJar(files, Map l) {
    files.findAll {
        if (!(it instanceof File))
            return false;
        it.getName() != 'android.jar'
    }.collect { file ->
        List e = new ArrayList();
        zipTree(file).each { the ->
            if (the.getName().endsWith(".jar") || the.getName().endsWith(".aar")) {
                loopJar(the, l)
            }
            if (the.getPath().contains("META-INF"))
                e.add(the);
        }
        if(!e.isEmpty())
        l.put(file.getName(), e);
    }
}

task fatJar << {
    Map l = new HashMap();
    loopJar(configurations.compile, l);
    l.each { key,val ->
        val.each {
            copy {
                from val
                into "licensing/"+key+"/"
            }
        }
    }
}

足够简单的修复: 写成 Java 的方式。