From 6297cb9919b4c76bd601103ef11f54664a4499d3 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 6 Sep 2023 02:25:52 +0800 Subject: [PATCH] refactor: replace packaging jar method using "net.lingala.zip4j" --- .../sweet-dependency-config.yaml | 5 +++- sweetproperty-gradle-plugin/build.gradle.kts | 1 + .../sweetproperty/utils/code/CodeCompiler.kt | 25 +++---------------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/gradle/sweet-dependency/sweet-dependency-config.yaml b/gradle/sweet-dependency/sweet-dependency-config.yaml index 3b2167b..c67d0e1 100644 --- a/gradle/sweet-dependency/sweet-dependency-config.yaml +++ b/gradle/sweet-dependency/sweet-dependency-config.yaml @@ -22,4 +22,7 @@ libraries: kotlinpoet: version: 1.14.2 javapoet: - version: 1.13.0 \ No newline at end of file + version: 1.13.0 + net.lingala.zip4j: + zip4j: + version: 2.11.5 \ No newline at end of file diff --git a/sweetproperty-gradle-plugin/build.gradle.kts b/sweetproperty-gradle-plugin/build.gradle.kts index 62891a1..3e210b5 100644 --- a/sweetproperty-gradle-plugin/build.gradle.kts +++ b/sweetproperty-gradle-plugin/build.gradle.kts @@ -23,6 +23,7 @@ kotlin { dependencies { implementation(com.squareup.kotlinpoet) implementation(com.squareup.javapoet) + implementation(net.lingala.zip4j.zip4j) } gradlePlugin { diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/utils/code/CodeCompiler.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/utils/code/CodeCompiler.kt index 09b71ab..3adb194 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/utils/code/CodeCompiler.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/utils/code/CodeCompiler.kt @@ -24,11 +24,10 @@ package com.highcapable.sweetproperty.utils.code import com.highcapable.sweetproperty.utils.code.entity.MavenPomData import com.highcapable.sweetproperty.utils.debug.SError import com.highcapable.sweetproperty.utils.deleteEmptyRecursively -import com.highcapable.sweetproperty.utils.parseFileSeparator import com.highcapable.sweetproperty.utils.toFile +import net.lingala.zip4j.ZipFile +import net.lingala.zip4j.model.ZipParameters import java.io.File -import java.util.jar.JarEntry -import java.util.jar.JarOutputStream import javax.tools.DiagnosticCollector import javax.tools.JavaFileObject import javax.tools.StandardLocation @@ -167,26 +166,8 @@ internal object CodeCompiler { */ private fun packageToJar(buildDir: File, outputDir: File, pomData: MavenPomData, isSourcesJar: Boolean) { if (buildDir.exists().not()) SError.make("Jar file output path not found: ${buildDir.absolutePath}") - /** - * 添加文件到 JAR - * @param jos 当前输出流 - * @param parentPath 父级路径 - */ - fun File.addToJar(jos: JarOutputStream, parentPath: String = "") { - val currentPath = "$parentPath$name".replace("${buildDir.name}|", "").replace("|", "/").parseFileSeparator() - if (isFile) { - if (name.startsWith(".")) return - jos.putNextEntry(JarEntry(currentPath)) - inputStream().use { fis -> - val buffer = ByteArray(4096) - var bytesRead: Int - while (fis.read(buffer).also { bytesRead = it } != -1) jos.write(buffer, 0, bytesRead) - } - jos.closeEntry() - } else listFiles()?.forEach { it.addToJar(jos, parentPath = "$currentPath|") } - } val jarFile = "${outputDir.absolutePath}/${pomData.artifactId}-${pomData.version}${if (isSourcesJar) "-sources" else ""}.jar".toFile() if (jarFile.exists()) jarFile.delete() - jarFile.outputStream().use { fos -> JarOutputStream(fos).use { jos -> buildDir.addToJar(jos) } } + ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false }) } } \ No newline at end of file