2017-11-07 1 views
0

私は下のコードで太った瓶を構築しています。しかし、私は、別のジャーに同じ名前の複数のプロパティファイルを持っており、それらはfat jarファイルにまとめられています。どのように私は脂肪の瓶のテキストファイルをグラデルと連結することができます

解決方法は、同じ名前のファイルを1つのファイルに連結/マージすることですが、どうすればよいですか?私はこの質問を見つけた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ファイルを作成してクラスパスに追加することで、この問題を解決しました。

答えて

0

おそらく、カスタム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 
    ... 
} 
+0

感謝を行うことができます。私はbuild.gradleにクラスを追加しようとしましたが、エラーが発生しました:予期しないトークン:アクション@行78、列25. fileTree.visit((Action <?super FileVisitDetails> visitor)。私はGradle 4.2.1を使用しています。 – Torsten

+0

最新のチェックでタスクを使用するためのソリューションを完全に書き直しました。もう一度やり直してください –

+0

これはうまくいきましたか? 'FileMerger'と' MergeCopy'ソースを 'buildSrc/src/main/groovy'を使って 'build.gradle'をきれいにしておく –

関連する問題