Skip to content

Commit

Permalink
update packageJars to allow packaging to specific file
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Nov 19, 2024
1 parent 47831a2 commit 0d7ec2e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions project/JarExport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import sbt.Keys.{crossTarget, fullClasspathAsJars}
import sbt.{Compile, File, IO, Setting, TaskKey, SettingKey}
import java.nio.file.Path

// Extending sbt.AutoPlugin causes this plugin to be automatically added to all sbt projects that match the triggers.
// And because we don’t really specify any triggers, it is just added everywhere.
Expand All @@ -14,17 +15,21 @@ object JarExport extends sbt.AutoPlugin {
// though, it does not yet define behaviour
val packageJars = TaskKey[File]("packageJars", "copies classpath jars to a file in the target dir")

val writeClasspathPath = SettingKey[File]("writeClasspathPath", "The file to write the classpath to.")
val packageJarsPath = SettingKey[String]("packageJarsPath", "The file to write the jars to.")

val writeClasspathPath = SettingKey[String]("writeClasspathPath", "The file to write the classpath to.")
// second additional command, same as the above
val writeClasspath = TaskKey[File]("writeClasspath", "writes the classpath to a file in the target dir")

// This defines settings the plugin makes.
// It is essentially the same as if this was in a `.settings()` block in the build.sbt
override lazy val projectSettings: Seq[Setting[?]] = Seq(

packageJarsPath := crossTarget.value.toPath.resolve("jars").toString,
// copy all jars required in the class path to a `jars` folder in the target directory
packageJars := {
val cp = (Compile / fullClasspathAsJars).value
val targetpath = crossTarget.value.toPath.resolve("jars")
val targetpath = Path.of(packageJarsPath.value)
IO.delete(targetpath.toFile)
IO.createDirectory(targetpath.toFile)
cp.foreach { at =>
Expand All @@ -33,16 +38,16 @@ object JarExport extends sbt.AutoPlugin {
// the return value is what `show stageJars` will display
targetpath.toFile
},
writeClasspathPath := crossTarget.value.toPath.resolve("classpath.txt").toFile,

writeClasspathPath := crossTarget.value.toPath.resolve("classpath.txt").toString,
// write the classpath into a file that can be passed to java as a commandline argument file
writeClasspath := {
val cp = (Compile / fullClasspathAsJars).value
val cpstring = cp.map { at =>
val pathstring = at.data.toString.replace("\\", "/")
s"""-cp "${pathstring}"\n"""
}.mkString("")
val targetpath = writeClasspathPath.value.toPath
val targetpath = Path.of(writeClasspathPath.value)
IO.write(targetpath.toFile, cpstring)
targetpath.toFile
}
Expand Down

0 comments on commit 0d7ec2e

Please sign in to comment.