简体   繁体   中英

How to debug a Clojure web application in Intellij?

I am using Intellij + Cursive and I want to debug a Clojure web application written using ring + compojure. I start the application in the Intellij terminal using lein and the ring plugin:

> lein ring server-headless

I want to debug this application using Intellij to set up breakpoints in the source code, see variables, etc.

But Intellij's Leiningen tab does not show a task with the ring command. Run configurations also do not have an option to run the ring command.

You need two steps:

  1. Update your project.clj to pass extra parameters, like this
  :ring {:nrepl {:start? true :port 4001}      ;; <== Add this
         :handler com.mycompany.web/myhandler} ;; you should have this 

... this should launch the web application in port 4000 and also an nREPL port for debugging, etc. in port 4001. You can check the lein-ring documentation for more details.

You should see the following when starting your app:

$ lein ring server-headless 4000
[... some output omitted ...]
Started nREPL server on port 4001
Started server on port 4000
  1. In Cursive, connect to the nREPL server as described in the section Remote REPLs in the Cursive docs . You should use either localhost or 0.0.0.0 in the host name and 4001 (or whatever nREPL port you used in the configuration in the previous step).

Intellij has a remote debug Run Configuration that can be used with Clojure .

First add the following options to the jvm in the project.clj file:

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

Where 5010 is port number to be specified in Intellij Remote Debug Configuration.

Then, in Intellij, go to Run -> Run... -> Edit Configurations... Use the + button and choose Remote. Give a name to the configuration, change the port to 5010 and click OK. Run the application using lein:

> lein ring server-headless

After the application is running, run (in Intellij) the Intellij Remote Debug Configuration you created. You will be able to setup breakpoints, run line by line, etc.

Without Leiningen

Another option is to drop leiningen and run the ring application as a Clojure Application in Cursive. You have to add a -main function:

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

app is the function where you define your routes and use as ring handler :ring {:handler xxx/app} in project.clj. You have to require [ring.adapter.jetty :refer [run-jetty]] and debug the file in Intellij as a Clojure Application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM