mirror of
https://github.com/HighCapable/SweetDependency.git
synced 2025-09-05 18:25:48 +08:00
refactor: using relative pom path to resolve generated dependencies jar
This commit is contained in:
@@ -21,7 +21,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.highcapable.sweetdependency.utils.code
|
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.code.entity.MavenPomData
|
||||||
import com.highcapable.sweetdependency.utils.debug.SError
|
import com.highcapable.sweetdependency.utils.debug.SError
|
||||||
import com.highcapable.sweetdependency.utils.deleteEmptyRecursively
|
import com.highcapable.sweetdependency.utils.deleteEmptyRecursively
|
||||||
@@ -48,7 +47,7 @@ internal object CodeCompiler {
|
|||||||
* @param outputDirPath 编译输出目录路径
|
* @param outputDirPath 编译输出目录路径
|
||||||
* @param files [JavaFileObject] 数组
|
* @param files [JavaFileObject] 数组
|
||||||
* @param compileOnlyFiles [JavaFileObject] 仅编译数组 - 默认空
|
* @param compileOnlyFiles [JavaFileObject] 仅编译数组 - 默认空
|
||||||
* @throws SweetDependencyExtension 如果编译失败
|
* @throws IllegalStateException 如果编译失败
|
||||||
*/
|
*/
|
||||||
internal fun compile(
|
internal fun compile(
|
||||||
pomData: MavenPomData,
|
pomData: MavenPomData,
|
||||||
@@ -84,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")
|
||||||
}
|
}
|
||||||
@@ -99,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
|
||||||
@@ -137,12 +135,6 @@ internal object CodeCompiler {
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
|
||||||
* 转换到 [MavenPomData] 目录名称
|
|
||||||
* @return [String]
|
|
||||||
*/
|
|
||||||
private fun String.toPomPathName() = trim().replace(".", "/").replace("_", "/").replace(":", "/").replace("-", "/")
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换到文件
|
* 转换到文件
|
||||||
* @param outputDir 输出目录
|
* @param outputDir 输出目录
|
||||||
@@ -163,11 +155,11 @@ internal object CodeCompiler {
|
|||||||
* @param outputDir 输出目录
|
* @param outputDir 输出目录
|
||||||
* @param pomData Maven POM 实体
|
* @param pomData Maven POM 实体
|
||||||
* @param isSourcesJar 是否为源码 JAR
|
* @param isSourcesJar 是否为源码 JAR
|
||||||
* @throws SweetDependencyExtension 如果编译输出目录不存在
|
* @throws IllegalStateException 如果编译输出目录不存在
|
||||||
*/
|
*/
|
||||||
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 })
|
||||||
}
|
}
|
||||||
|
@@ -27,4 +27,17 @@ package com.highcapable.sweetdependency.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("-", "/")
|
||||||
|
}
|
Reference in New Issue
Block a user