refactor: using relative pom path to resolve generated dependencies jar

This commit is contained in:
2023-09-06 19:32:08 +08:00
parent 6297cb9919
commit 8cc09e7fcd
2 changed files with 26 additions and 20 deletions

View File

@@ -83,8 +83,8 @@ internal object CodeCompiler {
sourceFile.writeText(it.getCharContent(true).toString()) sourceFile.writeText(it.getCharContent(true).toString())
} }
}; outputClassesDir.deleteEmptyRecursively() }; outputClassesDir.deleteEmptyRecursively()
writeMetaInf(outputClassesDir.absolutePath) writeMetaInf(outputClassesDir)
writeMetaInf(outputSourcesDir.absolutePath) writeMetaInf(outputSourcesDir)
createJarAndPom(pomData, outputDir, outputBuildDir, outputClassesDir, outputSourcesDir) createJarAndPom(pomData, outputDir, outputBuildDir, outputClassesDir, outputSourcesDir)
} else SError.make("Failed to compile java files into path: $outputDirPath\n$diagnosticsMessage") } else SError.make("Failed to compile java files into path: $outputDirPath\n$diagnosticsMessage")
} }
@@ -98,30 +98,29 @@ internal object CodeCompiler {
* @param sourcesDir 编译源码目录 * @param sourcesDir 编译源码目录
*/ */
private fun createJarAndPom(pomData: MavenPomData, outputDir: File, buildDir: File, classesDir: File, sourcesDir: File) { 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.resolve(pomData.relativePomPath).also { if (it.exists().not()) it.mkdirs() }
val pomDir = "${outputDir.absolutePath}/$pomPath".toFile().also { if (it.exists().not()) it.mkdirs() }
packageToJar(classesDir, pomDir, pomData, isSourcesJar = false) packageToJar(classesDir, pomDir, pomData, isSourcesJar = false)
packageToJar(sourcesDir, pomDir, pomData, isSourcesJar = true) packageToJar(sourcesDir, pomDir, pomData, isSourcesJar = true)
writePom(pomDir.absolutePath, pomData) writePom(pomDir, pomData)
buildDir.deleteRecursively() buildDir.deleteRecursively()
} }
/** /**
* 写入 META-INF/MANIFEST.MF * 写入 META-INF/MANIFEST.MF
* @param dirPath 当前目录路径 * @param dir 当前目录
*/ */
private fun writeMetaInf(dirPath: String) { private fun writeMetaInf(dir: File) {
val metaInfFile = "$dirPath/META-INF".toFile().apply { mkdirs() } val metaInfDir = dir.resolve("META-INF").apply { mkdirs() }
"${metaInfFile.absolutePath}/MANIFEST.MF".toFile().writeText("Manifest-Version: 1.0") metaInfDir.resolve("MANIFEST.MF").writeText("Manifest-Version: 1.0")
} }
/** /**
* 写入 POM * 写入 POM
* @param dirPath 当前目录路径 * @param dir 当前目录
* @param pomData Maven POM 实体 * @param pomData Maven POM 实体
*/ */
private fun writePom(dirPath: String, pomData: MavenPomData) = private fun writePom(dir: File, pomData: MavenPomData) =
"$dirPath/${pomData.artifactId}-${pomData.version}.pom".toFile().writeText( dir.resolve("${pomData.artifactId}-${pomData.version}.pom").writeText(
""" """
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project <project
@@ -136,12 +135,6 @@ internal object CodeCompiler {
""".trimIndent() """.trimIndent()
) )
/**
* 转换到 [MavenPomData] 目录名称
* @return [String]
*/
private fun String.toPomPathName() = trim().replace(".", "/").replace("_", "/").replace(":", "/").replace("-", "/")
/** /**
* 转换到文件 * 转换到文件
* @param outputDir 输出目录 * @param outputDir 输出目录
@@ -166,7 +159,7 @@ 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}")
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() if (jarFile.exists()) jarFile.delete()
ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false }) ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false })
} }

View File

@@ -27,4 +27,17 @@ package com.highcapable.sweetproperty.utils.code.entity
* @param artifactId Artifact Id * @param artifactId Artifact Id
* @param version 版本 * @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("-", "/")
}