【问题标题】:How can I concatenate text files in fat jar with gradle如何使用 gradle 连接 fat jar 中的文本文件
【发布时间】:2017-11-07 09:35:36
【问题描述】:

我正在用下面的代码构建一个胖罐子。但是,我在不同的 jar 中有多个同名的属性文件,这些文件被收集到 fat jar 中。

我想解决方案是将具有相同名称的文件连接/合并到一个文件中,但是我该怎么做呢?我发现了这个问题How do I concatenate multiple files in Gradle?

如何访问和合并 it 对象中的属性文件(我们将它们命名为 myproperties.properties)?

task fatJar(type: Jar) {
 def mainClass = "myclass"
 def jarName = "myjarname"
    zip64=true
    manifest {
      attributes(
        'Main-Class': mainClass,
        'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
      )
    }
    baseName = project.name + '-' + jarName + '-all'
    from { 
      configurations.compile.collect {
        it.isDirectory() ? it : zipTree(it) 
      } 
    }
    {
      exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
    }
    with jar
 }

解决方案: 尽管我没有尝试过,但我将给定的答案标记为解决方案。相反,我们通过创建多个 jar 并将它们添加到类路径来解决问题。

【问题讨论】:

    标签: java gradle concatenation executable-jar


    【解决方案1】:

    您可以编写一个自定义的MergeCopy 任务。例如

    public interface FileMerger implements Serializable {
        public void merge(String path, List<File> files, OutputStream out) throws IOException;
    }
    
    public class MergeCopy extends DefaultTask {
        private File outputDir;
        private List<FileTree> fileTrees = []
    
        @TaskInput
        FileMerger merger
    
        @OutputDirectory
        File getOutputDir() {
           return outputDir
        }
    
        @InputFiles
        List<FileTree> getFileTrees() {
           return fileTrees
        }
    
        void from(FileTree fileTree) {
            fileTrees.add(fileTree)
        }
    
        void into(Object into) {
            outputDir = project.file(into)
        }
    
        @TaskAction
        void copyAndMerge() {
           Map<String, List<File>> fileMap = [:]
           FileTree allTree = project.files().asFileTree
           fileTrees.each { FileTree fileTree ->
               allTree = allTree.plus(fileTree)
               fileTree.visit { FileVisitDetails fvd ->
                   String path = fvd.path.path
                   List<File> matches = fileMap[path] ?: []
                   matches << fvd.file
                   fileMap[path] = matches
               }
           }
           Set<String> dupPaths = [] as Set
           Set<String> nonDupPaths = [] as Set
           fileMap.each { String path, List<File> matches ->
               if (matches.size() > 1) {
                  dupPaths << path
               } else {
                  nonDupPaths << path
               }
           }
           FileTree nonDups = allTree.matching {
               exclude dupPaths
           }
           project.copy {
               from nonDups
               into outputDir
           }
           for (String dupPath : dupPaths) {
               List<File> matches = fileMap[dupPath]
               File outFile = new File(outputDir, dupPath)
               outFile.parentFile.mkdirs()
               try (OutputStream out = new FileOutputStream(outFile)) {
                   merger.merge(dupPath, matches, out)
                   out.flush()
               }
           }
        }
    }
    

    你可以这样做

    task mergeCopy(type: MergeCopy) {
        configurations.compile.files.each {
            from it.directory ? fileTree(it) : zipTree(it)
        } 
        into "$buildDir/mergeCopy"
        merger = { String path, List<File> files, OutputStream out ->
           // TODO: implement
        }
    }
    
    task fatJar(type: Jar) {
        dependsOn mergeCopy
        from mergeCopy.outputDir
        ...
    }
    

    【讨论】:

    • 感谢您的建议。我尝试将这些类添加到我的 build.gradle,但我收到一个错误:意外令牌:Action @ line 78,column 25.fileTree.visit((Action super FileVisitDetails> visitor)。我正在使用 Gradle 4.2.1 .
    • 我已经完全重写了解决方案,以使用具有最新检查的任务。再试一次
    • 那么,这行得通吗?请记住,您可以将 FileMergerMergeCopy 源移动到 buildSrc/src/main/groovy 以保持 build.gradle 干净
    • 我不能放心,因为我们一直在努力寻找一种完全不同的方法来解决这个问题。但是,我也没有时间测试您的解决方案。
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2019-06-28
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    相关资源
    最近更新 更多