refactor: using relative pom path to resolve generated dependencies jar

This commit is contained in:
2023-09-06 19:32:00 +08:00
parent a2ca412ec9
commit ff0ae7f0ab
2 changed files with 28 additions and 23 deletions

View File

@@ -21,7 +21,6 @@
*/
package com.highcapable.sweetdependency.utils.code
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
@@ -48,7 +47,7 @@ internal object CodeCompiler {
* @param outputDirPath 编译输出目录路径
* @param files [JavaFileObject] 数组
* @param compileOnlyFiles [JavaFileObject] 仅编译数组 - 默认空
* @throws SweetDependencyExtension 如果编译失败
* @throws IllegalStateException 如果编译失败
*/
internal fun compile(
pomData: MavenPomData,
@@ -84,8 +83,8 @@ internal object CodeCompiler {
sourceFile.writeText(it.getCharContent(true).toString())
}
}; outputClassesDir.deleteEmptyRecursively()
writeMetaInf(outputClassesDir.absolutePath)
writeMetaInf(outputSourcesDir.absolutePath)
writeMetaInf(outputClassesDir)
writeMetaInf(outputSourcesDir)
createJarAndPom(pomData, outputDir, outputBuildDir, outputClassesDir, outputSourcesDir)
} else SError.make("Failed to compile java files into path: $outputDirPath\n$diagnosticsMessage")
}
@@ -99,30 +98,29 @@ internal object CodeCompiler {
* @param sourcesDir 编译源码目录
*/
private fun createJarAndPom(pomData: MavenPomData, outputDir: File, buildDir: File, classesDir: File, sourcesDir: File) {
val pomPath = "${pomData.groupId.toPomPathName()}/${pomData.artifactId}/${pomData.version}"
val pomDir = "${outputDir.absolutePath}/$pomPath".toFile().also { if (it.exists().not()) it.mkdirs() }
val pomDir = outputDir.resolve(pomData.relativePomPath).also { if (it.exists().not()) it.mkdirs() }
packageToJar(classesDir, pomDir, pomData, isSourcesJar = false)
packageToJar(sourcesDir, pomDir, pomData, isSourcesJar = true)
writePom(pomDir.absolutePath, pomData)
writePom(pomDir, pomData)
buildDir.deleteRecursively()
}
/**
* 写入 META-INF/MANIFEST.MF
* @param dirPath 当前目录路径
* @param dir 当前目录
*/
private fun writeMetaInf(dirPath: String) {
val metaInfFile = "$dirPath/META-INF".toFile().apply { mkdirs() }
"${metaInfFile.absolutePath}/MANIFEST.MF".toFile().writeText("Manifest-Version: 1.0")
private fun writeMetaInf(dir: File) {
val metaInfDir = dir.resolve("META-INF").apply { mkdirs() }
metaInfDir.resolve("MANIFEST.MF").writeText("Manifest-Version: 1.0")
}
/**
* 写入 POM
* @param dirPath 当前目录路径
* @param dir 当前目录
* @param pomData Maven POM 实体
*/
private fun writePom(dirPath: String, pomData: MavenPomData) =
"$dirPath/${pomData.artifactId}-${pomData.version}.pom".toFile().writeText(
private fun writePom(dir: File, pomData: MavenPomData) =
dir.resolve("${pomData.artifactId}-${pomData.version}.pom").writeText(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project
@@ -137,12 +135,6 @@ internal object CodeCompiler {
""".trimIndent()
)
/**
* 转换到 [MavenPomData] 目录名称
* @return [String]
*/
private fun String.toPomPathName() = trim().replace(".", "/").replace("_", "/").replace(":", "/").replace("-", "/")
/**
* 转换到文件
* @param outputDir 输出目录
@@ -163,11 +155,11 @@ internal object CodeCompiler {
* @param outputDir 输出目录
* @param pomData Maven POM 实体
* @param isSourcesJar 是否为源码 JAR
* @throws SweetDependencyExtension 如果编译输出目录不存在
* @throws IllegalStateException 如果编译输出目录不存在
*/
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}")
val jarFile = "${outputDir.absolutePath}/${pomData.artifactId}-${pomData.version}${if (isSourcesJar) "-sources" else ""}.jar".toFile()
val jarFile = outputDir.resolve("${pomData.artifactId}-${pomData.version}${if (isSourcesJar) "-sources" else ""}.jar")
if (jarFile.exists()) jarFile.delete()
ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false })
}

View File

@@ -27,4 +27,17 @@ package com.highcapable.sweetdependency.utils.code.entity
* @param artifactId Artifact Id
* @param version 版本
*/
internal data class MavenPomData(internal val groupId: String, internal val artifactId: String, internal val version: String)
internal data class MavenPomData(internal val groupId: String, internal val artifactId: String, internal val version: String) {
/**
* 获取 [MavenPomData] 相对路径
* @return [String]
*/
internal val relativePomPath get() = "${groupId.toPomPathName()}/$artifactId/$version"
/**
* 转换到 [MavenPomData] 目录名称
* @return [String]
*/
private fun String.toPomPathName() = trim().replace(".", "/").replace("_", "/").replace(":", "/").replace("-", "/")
}