refactor: replace packaging jar method using "net.lingala.zip4j"

This commit is contained in:
2023-09-06 02:25:54 +08:00
parent 7e419b758f
commit a2ca412ec9
3 changed files with 8 additions and 23 deletions

View File

@@ -35,4 +35,7 @@ libraries:
version: 4.11.0
com.squareup:
javapoet:
version: 1.13.0
version: 1.13.0
net.lingala.zip4j:
zip4j:
version: 2.11.5

View File

@@ -27,6 +27,7 @@ dependencies {
implementation(com.charleskorn.kaml.kaml)
implementation(com.squareup.okhttp3.okhttp)
implementation(com.squareup.javapoet)
implementation(net.lingala.zip4j.zip4j)
}
gradlePlugin {

View File

@@ -25,11 +25,10 @@ import com.highcapable.sweetdependency.plugin.SweetDependencyExtension
import com.highcapable.sweetdependency.utils.code.entity.MavenPomData
import com.highcapable.sweetdependency.utils.debug.SError
import com.highcapable.sweetdependency.utils.deleteEmptyRecursively
import com.highcapable.sweetdependency.utils.parseFileSeparator
import com.highcapable.sweetdependency.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
@@ -168,26 +167,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 })
}
}