How do I run an sbt main class from the shell as normal command-line program?

前端 未结 4 1694
轻奢々
轻奢々 2020-12-02 15:00

How can I run an sbt app from the shell, so that I can run my app as a normal command-line program (as if run directly via scala but without having to set up an

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 15:23

    Here's what I have in my SBT (version 0.10) project definition,

      val Mklauncher = config("mklauncher") extend(Compile)
      val mklauncher = TaskKey[Unit]("mklauncher")
      val mklauncherTask = mklauncher <<= (target, fullClasspath in Runtime) map { (target, cp) =>
        def writeFile(file: File, str: String) {
          val writer = new PrintWriter(file)
          writer.println(str)
          writer.close()
        }
        val cpString = cp.map(_.data).mkString(":")
        val launchString = """
    CLASSPATH="%s"
    scala -usejavacp -Djava.class.path="${CLASSPATH}" "$@"
    """.format(cpString)
        val targetFile = (target / "scala-sbt").asFile
        writeFile(targetFile, launchString)
        targetFile.setExecutable(true)
      }
    
      ... // remember to add mklauncherTask to Project Settings
    

    The mklauncher task creates a script target/scala-sbt that executes scala with the project classpath already set. It would be nice to have mklauncher executed automatically whenever the classpath changes, but I haven't looked into doing this yet.

    (I use the Java classpath, rather than Scala's, for ease of creating embedded interpreters.)

提交回复
热议问题