Last active
April 5, 2022 02:56
-
-
Save Jason5Lee/75b17cc82289f944342e5f2a7a025294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The following task would create a `classpath` directory under the build dir, with a copy of the built jar and links of every jar from classpath. | |
// You can start the project by `java -classpath "classpath/*" MainClass`. | |
// This is useful for creating a personal-used script reusing gradle cache. | |
// This idea is inspired by the pnpm project. | |
task("linkClasspath") { | |
val jarTask = tasks.getByName("jar", Jar::class) | |
dependsOn(jarTask) | |
doLast { | |
val targetDir = File(buildDir, "classpath") | |
if (!targetDir.mkdirs()) { | |
targetDir.walkBottomUp().forEach { file -> | |
if (file != targetDir) { | |
if (!file.delete()) { | |
throw Exception("unable to delete old file `$file`") | |
} | |
} | |
} | |
} | |
sourceSets["main"] | |
.runtimeClasspath | |
.filter { it.isFile } | |
.forEach { file -> | |
println(file.absolutePath) | |
Files.createLink(File(targetDir, file.name).toPath(), file.toPath()) | |
} | |
val jarName = jarTask.archiveFileName.get() | |
Files.copy(Paths.get(buildDir.path, "libs", jarName), File(targetDir, jarName).toPath()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment