【问题标题】:Copying META-INF from all dependencies using gradle copy task使用 gradle 复制任务从所有依赖项中复制 META-INF
【发布时间】:2016-03-08 09:16:03
【问题描述】:

使用 Gradle Copy 任务的问题:

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 文件夹中的所有文件复制到我的目录中。

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

      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"

【问题讨论】:

    标签: java gradle copy meta-inf


    【解决方案1】:
    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的方式写。

    【讨论】:

    • 有时复制 DSL 并不明显——反正对我来说。
    • 我不会称之为简单修复:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 2016-04-21
    相关资源
    最近更新 更多