使用 simple-build-tool (sbt) 和 IntelliJ 调试 Scala 代码
使用 IntelliJ 的内置调试器调试 sbt 管理的 Scala 代码的最简单方法是什么? 来自 sbt 的 Google 代码网站的“RunningSbt”文档列出了命令用于运行项目或测试的主类,但似乎没有用于调试的命令。
后续问题:使用 sbt 的 jetty-run 命令时,将 IntelliJ 的调试器附加到 Jetty 的最简单方法是什么?
What's the easiest way to debug Scala code managed by sbt using IntelliJ's built-in debugger? The documentation from "RunningSbt" from sbt's google code site lists commands for running the main class for a project or the tests, but there seem to be no commands for debugging.
Follow-up question: what's the easiest way to attach IntelliJ's debugger to Jetty when using sbt's jetty-run command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
官方 适用于 Mac、Linux 和 Linux 的 SBT 软件包 Windows。您可以使用该标志来指定调试端口:
底层,这将使用典型的详细调试咒语启动 SBT 的 JVM:
您现在可以正常运行代码,例如使用 sbt
run
命令。配置 IntelliJ 以连接到正在运行的代码...
现在,您可以使用 远程调试配置。请注意,此表单中的上面 3 个字段虽然令人恐惧,但只是供您将文本复制到其中,而不是复制到其中(它们给出了上面指定的详细调试咒语,其中
- jvm-debug
已经为您处理好了) - 您可以更改的唯一配置位于中间的Settings
部分:There's a very convenient
-jvm-debug
flag in the official SBT packages for Mac, Linux & Windows. You can use the flag to specify the debug port:Under the covers, this starts the JVM for SBT with the typical verbose debugging incantation:
You now can run your code as normal, for example with the sbt
run
command.Configuring IntelliJ to connect to the running code...
Now you connect IntelliJ to your running process using a Remote Debug configuration. Note that the upper 3 fields in this form, while scary, are just for you to copy text out of, rather than into (they're giving the verbose debugging incantation specified above, which
-jvm-debug
already takes care of for you) - the only configuration you can change is in theSettings
section halfway down:对于 IntelliJ 中的普通调试,您可以按照通常的方式使用应用程序运行/调试配置,无论您是否使用 sbt 来编译代码。
要连接到在 Jetty 中运行的应用程序,您需要创建远程调试配置。当您这样做时,IntelliJ 将为您提供一组用于运行远程 JVM 的命令行参数——类似于
使用这些参数启动 sbt,然后执行
jetty-run
。最后,在 IntelliJ 中启动远程调试配置。 此帖子可能有用。For ordinary debugging in IntelliJ, you can use an Application run/debug configuration in the usual way, regardless of whether you're using sbt to compile your code.
To connect to your application running in Jetty, you'll need to create a Remote debug configuration. When you do so, IntelliJ will give you a set of command line arguments for running the remote JVM -- something like
Launch sbt with these arguments and then execute
jetty-run
. Finally, launch your remote debug configuration in IntelliJ. This thread might be useful.我在这方面也遇到了一些麻烦,因此冒着过于详细的风险,这就是我所做的:
SETUP
为 sbt jetty-run 创建运行配置
创建调试配置用于远程调试
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
(确保单击“确定”以实际创建配置)设置 sbt 插件以运行上面的虚拟机选项
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
粘贴到虚拟机参数框中已有的参数后面DEBUGGING
I had some trouble with this too, so at the risk of being overly detailed, here's what I did:
SETUP
Create a run configuration for sbt jetty-run
Create a debug configuration for remote debugging
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
(make sure to click OK to actually create the configuration)Set up sbt plugin to run the vm options above
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
in the VM parameters box, after the ones that are already thereDEBUGGING
这对我来说每次都有效,唯一需要设置的是 IntelliJ 中的远程调试;我从 IntelliJ 的终端使用 JVM 参数启动 SBT:
之后我可以在 localhost:5005 上启动远程调试
This one works for me every time, and the only thing you need to set up is remote debugging in IntelliJ; I start up SBT with JVM parameters from the terminal in IntelliJ:
After that I can start remote debugging on localhost:5005
这些答案或提供的链接都不适合我,所以一旦我弄清楚了这一点,我想我会分享...
包括我开始时没有的背景理解...
这主要基于说明< a href="https://web.archive.org/web/20141228045746/http://exit4b.com:80/blog/2013/10/01/debugging-scala-sbt-intellij" rel="nofollow noreferrer" >这里只是有额外的解释帮助我完成它。感谢@AndrewBucknell 在该链接断开后恢复了这篇文章(下面的评论)。 原始死链接在这里
我的环境:
Scala 2.10.2、SBT 0.13 和 IntelliJ 13.1
背景:
什么是调试?
什么是远程调试?
参考上面的链接,以下解释/修改很有用:
None of these answers or provided links worked for me, so once I figured this out, I figured I'd share...
including the background understanding I didn't have when I started...
This is based mostly on the instructions here just with additional explanation that carried me through it. Thanks to @AndrewBucknell for recovering the article after this link broke (comment below). original dead link here
My Environment:
Scala 2.10.2, SBT 0.13, and IntelliJ 13.1
Background:
What is Debugging?
What is Remote Debugging?
Referencing the link above, the following explanations/modifications are useful:
我在这里添加另一个答案,因为我在查找相关问题时发现了这个问题:使用断点调试测试类。
我正在使用 ScalaTest,并且通常使用 sbt 的“仅测试”命令运行套件。现在,当我想使用交互式调试器时,我可以执行以下操作:
创建一个类型为“ScalaTest”的新运行/调试配置,输入主“测试类:”名称,然后选择“启动前:运行 SBT 操作'测试” -编译'”。就是这样,您现在可以在测试源中放置断点,并运行此配置。
I am adding another answer here, because I found this question when looking up a related problem: Debugging test classes using breakpoints.
I am using ScalaTest, and I typically run a suite using sbt's 'test-only' command. Now when I want to use the interactive debugger, I can do the following:
Create a new Run/Debug Configuration of type 'ScalaTest', put the main "Test Class:" name, and choose "Before launch: Run SBT Action 'test-compile'". That's it, you can place breakpoints now in the test sources, and run this configuration.
我也一直在努力在 Windows 上使用 Intellij 通过 SBT 构建的 Spray-can / Akka / Scala 应用程序进行调试。将各种建议拼凑在一起,对我来说最简单的方法是:
确保您的项目/plugsin.sbt 文件中有 sbt.Revolver,例如
addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.1")
在 build.sbt 文件中设置 javaoptions:
javaOptions := Seq("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")
特别要使用 suspend=y 选项。这将保留该应用程序,直到您从 Intellij 连接远程调试器
通过“运行/编辑配置”菜单在 Intellij 中设置调试器配置。按+按钮,选择“远程”选项。确保条目与上面的 javaoptions 匹配,特别是端口地址 5005。为配置指定一个名称,例如“Spray”。
从 SBT 控制台使用重新启动命令。您应该在反馈输出中看到 5005 端口地址。
在 Intellij 中设置断点。
从 Intellij 中,选择“运行\调试‘Spray’”。这应该连接到喷雾罐网络服务器。您应该能够在调试窗口中看到很多线程。
请注意,Spray 中的某些 Get 指令似乎在启动时执行,但在调用网站时不会重复执行。
I've been struggling with debugging too on Windows with a Spray-can / Akka / Scala app built via SBT, using Intellij. Piecing together various suggestions, the simplest approach for me was:
Make sure you have sbt.Revolver in your project/plugsin.sbt file e.g.
addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.1")
Set javaoptions in you build.sbt file:
javaOptions := Seq("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")
In particular use the suspend=y option. This will hold the app until you connect a remote debugger from Intellij
Set up a debugger configuration in Intellij via Run / Edit Configurations menu. Press the + button, select the "Remote" option. Make sure the entries match the javaoptions above, in particular the port address of 5005. Give the config a name like 'Spray'.
From your SBT console use the re-start command. You should see the 5005 port address in the feedback output.
In Intellij set your breakpoints.
From Intellij, select the Run \ Debug 'Spray'. This should connect to the spray-can web server. You should be able to see a lot of threads in the debug window.
Beware that some of the Get directives in Spray seem to get executed on start up but not repeatedly on calling the website.
文件->设置->其他设置->SBT
虚拟机参数
-Xmx512M -XX:MaxPermSize=256M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
运行->编辑配置
按+,然后选择远程
按“立即应用”
当您执行命令“run”时,在 SBT 控制台(由 intelliJ 内部启动)中
,您将看到“在地址:5005 监听传输 dt_socket” ,现在按“运行”->“调试”。您将看到下面的调试菜单已激活。它有两个选项卡“调试器”和“控制台”。
使用F7从下一行到下一行
我选择暂停为n。当我运行 run 命令时它被卡住了
File->Settings->Other Settings->SBT
VM parameters
-Xmx512M -XX:MaxPermSize=256M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
Run->Edit Configurations
Press + and then select remote
Press Apply
Now in the SBT console (Started inside by intelliJ) when you execute the command 'run' You will see "Listening for transport dt_socket at address: 5005"
Now press Run->Debug. You will see the debug menus below activated. It has two tabs Debugger and Console.
Use F7 to from next line to next
I chose suspend to be n. With it being y when I ran the run command it was stuck
试试这个
try this
对于 Windows 人员来说,编辑
%SBT_HOME%\bin\sbt.bat
并找到以下代码行:然后将其替换为以下代码:
我可以做的最好的事情是为
-jvm-debug
在 Bash 脚本启动器中看到时注意。我不认为
%SBT_HOME%
实际上存在于这个脚本之外,除非你在你的环境中明确创建,但无论如何你明白了:DFor what it's worth Windows folk, edit
%SBT_HOME%\bin\sbt.bat
and locate the following lines of code:then replace them with this code:
Best I can do to get same behaviour for
-jvm-debug
when seen in the Bash script launcherNB. I don't think
%SBT_HOME%
actually exists outside this script, unless you created explicitly in you environment, but anyway you get the point :DAttachMe IntelliJ 插件可能是附加调试器的更快方式,而无需摆弄端口号:
博客文章和自述文件有设置说明,但是我必须对它们进行一些更改才能使其在我的计算机上运行
下载 来自官方仓库的
installer.sh
脚本chmod u+x installer.sh
./installer.sh
运行它/.config/attachme/agent.jar
并创建~/.attachme
文件修改
~/.attachme
以包含安装相应的Attachme插件并重启:
IntelliJ |偏好 |插件
Attachme
运行配置:Run |编辑配置... |添加新配置 |附上调试器注册表| OK
在这些一次性的恶作剧之后,我们可以通过
Attachme
run configurationsource ~/.attachme
AttachMe IntelliJ plugin is potentially faster way off attaching the debugger without having to fiddle with port numbers:
Blog post and readme have setup instructions, however I had to alter them a bit to get it working on my machine
Download
installer.sh
script from official repochmod u+x installer.sh
./installer.sh
/.config/attachme/agent.jar
and create~/.attachme
fileModify
~/.attachme
to containInstall corresponding Attachme plugin and restart:
IntelliJ | Preferences | Plugins
Attachme
run configuration:Run | Edit configurations... | Add new configuration | Attachme debugger registry | OK
After these one-off shenanigans, we can attach a debugger automatically by
Attachme
run configurationsource ~/.attachme
sbt "runMain example.Hello"
关于相对较新版本的Intellij(2020.1.4),一步一步给分:
配置Intellij
这个新版本的Intellij不允许你编辑“命令行参数”中的字符串,你只能编辑上面的“主机”和“端口”,我保留默认值;我只是将下面的“模块路径”更改为“root”(不确定是否有必要)
远程调试
sbt
命令,例如sbt -jvm-debug 5005 test
Give a cent step by step regarding a relatively new version of Intellij (2020.1.4):
Configure Intellij
This new version of Intellij does not allow you to edit the string in the "Command line arguements", you can only edit the "Host" and "Port" above, which I leave it default; I only change the "module path" below to "root" (not sure if it's necessary)
Remote debug
sbt
command in command line, e.g.sbt -jvm-debug 5005 test
我也遇到了同样的问题,我想分享一下我是如何解决的。顺便说一下,我使用的是 Ubuntu 14.04 和 IntelliJ 15。
在设置 -> SBT->
将以下行粘贴到 VM 参数文本框中:
-XX:MaxPermSize=384M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
在 IntelliJ 中打开 Termilal 并运行:
sbt -jvm-调试 5005
注意:您应该在终端中看到这一行:“在地址:5005 监听传输 dt_socket”
编辑配置 ->点击+->选择“远程”
<前><代码>我。给出一些名称示例:DebugMyModule
二.所有需要的配置都会自动设置,但您只需验证即可。在命令行参数中,应类似于“-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005”。
三.在“在模块的类路径中搜索源”文本框中指定您的模块,您的测试用例位于哪个模块中。
四.将 127.0.0.1 主机代替“localhost”。 “本地主机”不适合我。
在
编辑配置
列表框中选择DebugMyModule,然后单击调试。注意:您应该看到调试视图已打开,并且在调试控制台中您应该能够看到“已连接到目标虚拟机,地址:
'127.0.0.1:5005'
, Transport: 'socket'"在您的测试类中的几个测试用例中放置断点。
进入运行“
sbt -jvm-debug 5005
”的终端并按如下方式运行例如: com.myproject.module.AddServiceTest[Spec]
您可以看到调试在测试用例断点中启动。
I also got the same problem, I like to share how I resolved. By the way I am using Ubuntu 14.04 and IntelliJ 15.
In Setting -> SBT ->
Pasted below line in VM Parameters text box:
-XX:MaxPermSize=384M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
Opened Termilal in IntelliJ and run:
sbt -jvm-debug 5005
Note: you should see this line in terminal: "Listening for transport dt_socket at address: 5005"
Edit Configurations -> Click + -> Select 'Remote'
Select DebugMyModule in
Edit Configurations
list box and click Debug.Note: You should see Debug View opened and in Debug Console You should able to see "Connected to the target VM, address:
'127.0.0.1:5005'
, transport: 'socket'"Put breakpoints in few test cases in your Testclass.
Come to Terminal where you run "
sbt -jvm-debug 5005
" and run like belowFor example: com.myproject.module.AddServiceTest[Spec]
You can see debug started in your test case breakpoint.
我从发布的答案中了解了每一步,但它是不同答案的组合:
I learned every step from the posted answers but it's a combination of different answers:
根据上面 Roberto Tyley 的回答,但在 Windows 中我们还需要在安装 sbt 后设置以下环境变量:
SBT_HOME
C:\Program Files (x86)\sbt\
[或安装 sbt 的任何位置]
SBT_OPTS
-Xdebug -runjdwp:transport=dt_socket,server=y,suspend=n,address=5005
[per RT's IntelliJ example]
添加到路径:%SBT_HOME%\bin;%SBT_OPTS%
然后运行以下命令在项目文件夹内的命令行中
“sbt 运行 -jvm -debug 5005”。
如果工作正常,命令行将首先输出一行 re
“侦听地址:5005 处的传输 dt_socket”。
在 IntelliJ 中设置断点。
在浏览器中打开网站以触发调试器断点,例如“http://localhost:9000/”。
As per Roberto Tyley's answer above, but in Windows we also need to set the following environment variables after installing sbt:
SBT_HOME
C:\Program Files (x86)\sbt\
[or wherever sbt is installed]
SBT_OPTS
-Xdebug -runjdwp:transport=dt_socket,server=y,suspend=n,address=5005
[per RT's IntelliJ example]
Add to path: %SBT_HOME%\bin;%SBT_OPTS%
Then run the following in the command line within the project folder
"sbt run -jvm -debug 5005".
If this is working properly, the command line will initially output a line re
"Listening for transport dt_socket at address: 5005".
Set breakpoint in IntelliJ.
Open website in browser to trigger the debugger breakpoint e.g. "http://localhost:9000/".