@@ -34,81 +34,75 @@ tasks.register<ShadowJar>("shadowChatFormatter") {
3434 .files
3535 }
3636
37- val merger = JarMerger (outputFile, shadowJarFiles)
38-
3937 this .doLast {
40- merger.merge( )
38+ mergeJars(outputFile, shadowJarFiles )
4139 }
4240}
4341
44- class JarMerger (
45- private val outputFile : File ,
46- private val inputFiles : List <File >
47- ) : Serializable {
4842
49- fun merge ( ) {
50- val outputDir = this . outputFile.parentFile
51- ? : throw IllegalStateException (" Cannot find output directory" )
43+ fun mergeJars ( outputFile : File , inputFiles : List < File > ) {
44+ val outputDir = outputFile.parentFile
45+ ? : throw IllegalStateException (" Cannot find output directory" )
5246
53- if (! outputDir.exists() && ! outputDir.mkdirs()) {
54- throw IllegalStateException (" Failed to create directory: ${outputDir.absolutePath} " )
55- }
47+ if (! outputDir.exists() && ! outputDir.mkdirs()) {
48+ throw IllegalStateException (" Failed to create directory: ${outputDir.absolutePath} " )
49+ }
5650
57- if (this . outputFile.exists() && ! this . outputFile.delete()) {
58- throw IllegalStateException (" Cannot delete existing file: ${this . outputFile.absolutePath} " )
59- }
51+ if (outputFile.exists() && ! outputFile.delete()) {
52+ throw IllegalStateException (" Cannot delete existing file: ${outputFile.absolutePath} " )
53+ }
6054
61- if (! this . outputFile.createNewFile()) {
62- throw IllegalStateException (" Cannot create output file: ${this . outputFile.absolutePath} " )
63- }
55+ if (! outputFile.createNewFile()) {
56+ throw IllegalStateException (" Cannot create output file: ${outputFile.absolutePath} " )
57+ }
6458
65- JarOutputStream (FileOutputStream (this . outputFile)).use { outputJar ->
66- val processedEntries = mutableSetOf<String >()
59+ JarOutputStream (FileOutputStream (outputFile)).use { outputJar ->
60+ val processedEntries = mutableSetOf<String >()
6761
68- for (jarFile in this .inputFiles) {
69- this .processJarFile(jarFile, outputJar, processedEntries)
70- }
62+ for (jarFile in inputFiles) {
63+ processJarFile(jarFile, outputJar, processedEntries)
7164 }
7265 }
66+ }
7367
74- private fun processJarFile (
75- jarFile : File ,
76- outputJar : JarOutputStream ,
77- processedEntries : MutableSet <String >
78- ) {
79- JarFile (jarFile).use { sourceJar ->
80- for (entry in sourceJar.entries()) {
81- if (entry.isDirectory || processedEntries.contains(entry.name)) {
82- continue
83- }
8468
85- try {
86- this .copyJarEntry(sourceJar, entry, outputJar)
87- processedEntries.add(entry.name)
88- } catch (exception: IOException ) {
89- if (exception.message?.contains(" duplicate entry:" ) != true ) {
90- throw exception
91- }
69+ fun processJarFile (
70+ jarFile : File ,
71+ outputJar : JarOutputStream ,
72+ processedEntries : MutableSet <String >
73+ ) {
74+ JarFile (jarFile).use { sourceJar ->
75+ for (entry in sourceJar.entries()) {
76+ if (entry.isDirectory || processedEntries.contains(entry.name)) {
77+ continue
78+ }
79+
80+ try {
81+ copyJarEntry(sourceJar, entry, outputJar)
82+ processedEntries.add(entry.name)
83+ } catch (exception: IOException ) {
84+ if (exception.message?.contains(" duplicate entry:" ) != true ) {
85+ throw exception
9286 }
9387 }
9488 }
9589 }
90+ }
9691
97- private fun copyJarEntry (
98- sourceJar : JarFile ,
99- entry : JarEntry ,
100- outputJar : JarOutputStream
101- ) {
102- val entryBytes = sourceJar.getInputStream(entry).use { it.readBytes() }
103- val newEntry = JarEntry (entry.name).apply {
104- this .time = System .currentTimeMillis()
105- this .size = entryBytes.size.toLong()
106- }
107-
108- outputJar.putNextEntry(newEntry)
109- outputJar.write(entryBytes)
110- outputJar.closeEntry()
92+ fun copyJarEntry (
93+ sourceJar : JarFile ,
94+ entry : JarEntry ,
95+ outputJar : JarOutputStream
96+ ) {
97+ val entryBytes = sourceJar.getInputStream(entry).use { it.readBytes() }
98+ val newEntry = JarEntry (entry.name).apply {
99+ this .time = System .currentTimeMillis()
100+ this .size = entryBytes.size.toLong()
111101 }
102+
103+ outputJar.putNextEntry(newEntry)
104+ outputJar.write(entryBytes)
105+ outputJar.closeEntry()
112106}
113107
114108runPaper {
0 commit comments