mirror of
https://github.com/HighCapable/SweetProperty.git
synced 2025-09-05 10:15:36 +08:00
refactor: replace packaging jar method using "net.lingala.zip4j"
This commit is contained in:
@@ -22,4 +22,7 @@ libraries:
|
|||||||
kotlinpoet:
|
kotlinpoet:
|
||||||
version: 1.14.2
|
version: 1.14.2
|
||||||
javapoet:
|
javapoet:
|
||||||
version: 1.13.0
|
version: 1.13.0
|
||||||
|
net.lingala.zip4j:
|
||||||
|
zip4j:
|
||||||
|
version: 2.11.5
|
@@ -23,6 +23,7 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation(com.squareup.kotlinpoet)
|
implementation(com.squareup.kotlinpoet)
|
||||||
implementation(com.squareup.javapoet)
|
implementation(com.squareup.javapoet)
|
||||||
|
implementation(net.lingala.zip4j.zip4j)
|
||||||
}
|
}
|
||||||
|
|
||||||
gradlePlugin {
|
gradlePlugin {
|
||||||
|
@@ -24,11 +24,10 @@ package com.highcapable.sweetproperty.utils.code
|
|||||||
import com.highcapable.sweetproperty.utils.code.entity.MavenPomData
|
import com.highcapable.sweetproperty.utils.code.entity.MavenPomData
|
||||||
import com.highcapable.sweetproperty.utils.debug.SError
|
import com.highcapable.sweetproperty.utils.debug.SError
|
||||||
import com.highcapable.sweetproperty.utils.deleteEmptyRecursively
|
import com.highcapable.sweetproperty.utils.deleteEmptyRecursively
|
||||||
import com.highcapable.sweetproperty.utils.parseFileSeparator
|
|
||||||
import com.highcapable.sweetproperty.utils.toFile
|
import com.highcapable.sweetproperty.utils.toFile
|
||||||
|
import net.lingala.zip4j.ZipFile
|
||||||
|
import net.lingala.zip4j.model.ZipParameters
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.jar.JarEntry
|
|
||||||
import java.util.jar.JarOutputStream
|
|
||||||
import javax.tools.DiagnosticCollector
|
import javax.tools.DiagnosticCollector
|
||||||
import javax.tools.JavaFileObject
|
import javax.tools.JavaFileObject
|
||||||
import javax.tools.StandardLocation
|
import javax.tools.StandardLocation
|
||||||
@@ -167,26 +166,8 @@ internal object CodeCompiler {
|
|||||||
*/
|
*/
|
||||||
private fun packageToJar(buildDir: File, outputDir: File, pomData: MavenPomData, isSourcesJar: Boolean) {
|
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}")
|
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()
|
val jarFile = "${outputDir.absolutePath}/${pomData.artifactId}-${pomData.version}${if (isSourcesJar) "-sources" else ""}.jar".toFile()
|
||||||
if (jarFile.exists()) jarFile.delete()
|
if (jarFile.exists()) jarFile.delete()
|
||||||
jarFile.outputStream().use { fos -> JarOutputStream(fos).use { jos -> buildDir.addToJar(jos) } }
|
ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false })
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user