scala - 如何使用 VS Code 和 Metals 运行现有的 Scala 项目?

标签 scala visual-studio-code sbt scala-ide scala-metals

我是 Scala 的新手,我发现 Scala IDE在我的机器上搜索代码库和编辑代码等基本操作非常慢。我习惯了 Visual Studio Code,很高兴找到 this metals extension .

我能够“导入构建”并修复问题,例如在我的项目中提高 Scala 版本,但我不确定如何重现此步骤以设置运行配置并在 Scala IDE 中实际启动我们的应用程序。

enter image description here
enter image description here

我们有一个包含一堆项目的父文件夹和一个“consoleapp”项目,它是我们应用程序的主要入口点——它导入所有其他项目的逻辑/路由。

|____parent
| |____consoleapp
| |____project1
| |____project2 

我试过 sbt runsbt runMain consoleapp来自 consoleapp 文件夹和父文件夹,但它们不起作用。

我不确定我们设置中的哪些其他信息是相关的 - 很乐意根据需要提供更多信息。

更新 在下面添加更多详细信息:

控制台应用程序/build.sbt
name := "consoleapp"

version := "1.0"

scalaVersion := "2.12.10"

packMain := Map("consoleapp" -> "consoleapp")

libraryDependencies ++=  Seq (...)

我运行的命令的输出 - sbt runsbt runMain

从 ~/scala/parent 运行
> sbt run                                                                                                                                                                 masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/project
[info] Loading settings for project consoleapp from build.sbt ...
... 
Loading settings for all other projects in parent folder
...
[info] Loading settings for project parent from build.sbt ...
[info] Resolving key references (22435 settings) ...
[info] Set current project to parent (in build file:/Users/pradhyo/scala/parent/)
[error] java.lang.RuntimeException: No main class detected.
[error]     at scala.sys.package$.error(package.scala:30)
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] (Compile / bgRun) No main class detected.
[error] Total time: 1 s, completed 18-Dec-2019 1:41:25 PM

从 ~/scala/parent 运行
> sbt "runMain consoleapp.consoleapp"                                                                                                                                     masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/project
[info] Loading settings for project consoleapp from build.sbt ...
... 
Loading settings for all other projects in parent folder
...
[info] Loading settings for project parent from build.sbt ...
[info] Resolving key references (22435 settings) ...
[info] Set current project to parent (in build file:/Users/pradhyo/scala/parent/)
[info] running consoleapp.consoleapp 
[error] (run-main-0) java.lang.ClassNotFoundException: consoleapp.consoleapp
[error] java.lang.ClassNotFoundException: consoleapp.consoleapp
[error]     at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
[error] stack trace is suppressed; run last Compile / bgRunMain for the full output
[error] Nonzero exit code: 1
[error] (Compile / runMain) Nonzero exit code: 1
[error] Total time: 0 s, completed 18-Dec-2019 1:46:21 PM

从 ~/scala/parent/consoleapp 运行
> sbt run                                                                                                                                                                 masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/consoleapp/project
[info] Loading settings for project consoleapp from build.sbt ...
[info] Set current project to consoleapp (in build file:/Users/pradhyo/scala/parent/consoleapp/)
[error] java.lang.RuntimeException: No main class detected.
[error]     at scala.sys.package$.error(package.scala:30)
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] (Compile / bgRun) No main class detected.
[error] Total time: 0 s, completed 18-Dec-2019 1:49:26 PM

从 ~/scala/parent/consoleapp 运行
> sbt "runMain consoleapp"                                                                                                                                                masterstate [0a8dab85] modified
[info] Loading settings for project global-plugins from metals.sbt,build.sbt ...
[info] Loading global plugins from /Users/pradhyo/.sbt/1.0/plugins
[info] Loading project definition from /Users/pradhyo/scala/parent/consoleapp/project
[info] Loading settings for project consoleapp from build.sbt ...
[info] Set current project to consoleapp (in build file:/Users/pradhyo/scala/parent/consoleapp/)
[info] running consoleapp 
[error] (run-main-0) java.lang.ClassNotFoundException: consoleapp
[error] java.lang.ClassNotFoundException: consoleapp
[error]     at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
[error] stack trace is suppressed; run last Compile / bgRunMain for the full output
[error] Nonzero exit code: 1
[error] (Compile / runMain) Nonzero exit code: 1
[error] Total time: 1 s, completed 18-Dec-2019 1:50:06 PM

最佳答案

按照 Scala Metals VSCode Readme 中的说明操作后,对问题中的 Eclipse 屏幕截图使用与此类似的启动配置。

.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "scala",
            "name": "Debug consoleapp",
            "request": "launch",
            "mainClass": "consoleapp",
            "buildTarget": "consoleapp",
            "args": [],
            "jvmOptions": ["-J-Dconfig.file=/path/to/config/file"]
        }
    ]
}

我在传递 pureconfig 的配置文件时遇到问题正确。这是the Github issue使用正确的 jvmOptions线。

关于scala - 如何使用 VS Code 和 Metals 运行现有的 Scala 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59398213/

相关文章:

scala - 使用 FlatMap 使用 Spark 和 Scala 将列名称附加到元素

asp.net - 在 VS Code 和 VS 2015 之间切换

playframework - Play 2.0 无法解析 javax.ws.rs/jsr311-api/1.1.1

scala - "str"% "str"在 SBT 中是什么意思?

scala - 如何将SBT类附加到Intellij Idea?

scala - h2o scala代码编译错误找不到对象ai

scala - 在 Play 框架 2.5 (Scala) 中使用 CSRF token 测试请求

scala - 我们应该直接使用 ScalaSignature 吗?

dart - 如何修复VSCode中dart:html的查找错误?

go - "Exported type should have comment or be unexported"golang VS 代码