[英]How can I concatenate text files in fat jar with gradle
I'm building a fat jar with the code below. 我用下面的代码构建一个胖子罐。 However, I have multiple property files with the same name in different jars, which are collected into the fat jar. 但是,我在不同的jar中有多个具有相同名称的属性文件,这些文件被收集到胖jar中。
I guess the solution is to concatenate/merge the files with the same name into one file, but how do I do it? 我猜解决方案是将具有相同名称的文件串联/合并到一个文件中,但是我该怎么做? I found this question How do I concatenate multiple files in Gradle? 我发现了这个问题, 如何在Gradle中连接多个文件? . 。
How can I access and merge the property files (let's name them myproperties.properties) in the it
objects? 如何在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
}
Solution: I marked the given answer as the solution although I haven't tried it. 解决方案:尽管我没有尝试过,但仍将给出的答案标记为解决方案。 Instead we solved the problem by creating multiple jars and adding those to the classpath. 相反,我们通过创建多个jar并将其添加到类路径来解决该问题。
Possibly you could write a custom MergeCopy
task. 可能您可以编写自定义MergeCopy
任务。 Eg 例如
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()
}
}
}
}
You could then do 然后你可以做
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
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.