簡體   English   中英

使用Gradle Copy任務從所有依賴項復制META-INF

[英]Copying META-INF from all dependencies using gradle copy task

使用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);
        }
    }
}

非常簡單,遍歷所有依賴項(如果它們是歸檔文件),遍歷其zipTree,以確保它們本身沒有Jar。

完成此操作后,我想將META-INF文件夾中的所有文件復制到我的目錄中。

由於某種原因,它將所有文件復制到目錄中。 但是,如果我在每個文件中都使用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插件0.7.0:“ 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方式編寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM