【问题标题】:How to debug a Clojure web application in Intellij?如何在 Intellij 中调试 Clojure Web 应用程序?
【发布时间】:2019-10-11 12:04:08
【问题描述】:

我正在使用 Intellij + Cursive,我想调试一个使用 ring + compojure 编写的 Clojure Web 应用程序。我使用 lein 和 ring 插件在 Intellij 终端中启动应用程序:

> lein ring server-headless

我想使用 Intellij 调试这个应用程序,在源代码中设置断点,查看变量等。

但是 Intellij 的 Leiningen 选项卡没有显示带有 ring 命令的任务。运行配置也没有运行 ring 命令的选项。

【问题讨论】:

    标签: intellij-idea clojure ring cursive


    【解决方案1】:

    你需要两个步骤:

    1. 更新您的project.clj 以传递额外的参数,像这样
      :ring {:nrepl {:start? true :port 4001}      ;; <== Add this
             :handler com.mycompany.web/myhandler} ;; you should have this 
    

    ...这应该在端口 4000 中启动 Web 应用程序,并在端口 4001 中启动用于调试等的 nREPL 端口。您可以查看lein-ring documentation 了解更多详细信息。

    启动应用时,您应该会看到以下内容:

    $ lein ring server-headless 4000
    [... some output omitted ...]
    Started nREPL server on port 4001
    Started server on port 4000
    
    1. 在 Cursive 中,按照Cursive docs 中的远程 REPL 部分所述连接到 nREPL 服务器。您应该在主机名和 4001 中使用 localhost 或 0.0.0.0(或您在上一步的配置中使用的任何 nREPL 端口)。

    【讨论】:

    • 我能够按照您的描述运行,但 Cursive 不允许在调试模式下进行远程 REPL。我无法在我的代码中设置断点。
    • 如何在 Cursive 的调试模式下运行lein ring server-headless,以便设置断点?也许我可以在本地 REPL 或 Clojure 应用程序中运行它。
    • 我没有找到使用 Cursive 在调试模式下运行 lein(环形)应用程序的方法,但您可以使用以下命令在 Cursive 中调试环形应用程序(不带 lein):(defn -main [] (run-jetty app {:port 8080})。 app 是您定义路线并在project.clj 中用作:ring {:handler xxx/app} 的函数。您必须要求 [ring.adapter.jetty :refer [run-jetty]] 并作为 Clojure 应用程序运行。
    【解决方案2】:

    Intellij 有一个 remote debug Run Configuration 和 can be used with Clojure。

    首先在project.clj文件中的jvm中添加如下选项:

    :jvm-opts ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5010"]
    

    其中 5010 是要在 Intellij 远程调试配置中指定的端口号。

    然后,在 Intellij 中,转到 Run -&gt; Run... -&gt; Edit Configurations...使用 + 按钮并选择 Remote. 为配置命名,将端口更改为 5010,然后单击确定。使用 lein 运行应用程序:

    &gt; lein ring server-headless

    应用程序运行后,运行(在 Intellij 中)您创建的 Intellij 远程调试配置。您将能够设置断点、逐行运行等。

    没有莱宁根

    另一种选择是删除 leiningen 并将 ring 应用程序作为 Cursive 中的 Clojure 应用程序运行。你必须添加一个-main 函数:

    (defn -main [] (run-jetty app {:port 8080})
    

    app 是您定义路由并在project.clj. 中用作环处理程序:ring {:handler xxx/app} 的函数,您必须要求[ring.adapter.jetty :refer [run-jetty]] 并将Intellij 中的文件作为Clojure 应用程序进行调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多