From ff0ae7f0aba492e8dd581d57ef6f87202909d836 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 6 Sep 2023 19:32:00 +0800 Subject: [PATCH] refactor: using relative pom path to resolve generated dependencies jar --- .../utils/code/CodeCompiler.kt | 36 ++++++++----------- .../utils/code/entity/MavenPomData.kt | 15 +++++++- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/sweetdependency-gradle-plugin/src/main/java/com/highcapable/sweetdependency/utils/code/CodeCompiler.kt b/sweetdependency-gradle-plugin/src/main/java/com/highcapable/sweetdependency/utils/code/CodeCompiler.kt index 47e68b6..f58d233 100644 --- a/sweetdependency-gradle-plugin/src/main/java/com/highcapable/sweetdependency/utils/code/CodeCompiler.kt +++ b/sweetdependency-gradle-plugin/src/main/java/com/highcapable/sweetdependency/utils/code/CodeCompiler.kt @@ -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( """