2015-12-02 14:23:39 +00:00
#+TITLE : Java layer
2015-07-04 04:55:38 +00:00
2019-05-07 08:53:56 +00:00
#+TAGS : general|layer|multi-paradigm|programming
2019-05-05 17:26:40 +00:00
2015-07-04 05:44:50 +00:00
[[file:img/java.png ]]
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description ][Description ]]
2017-11-29 19:47:06 +00:00
- [[#features ][Features: ]]
2017-05-22 14:16:12 +00:00
- [[#layer-installation ][Layer Installation ]]
- [[#layer ][Layer ]]
- [[#choosing-a-backend ][Choosing a backend ]]
- [[#backends ][Backends ]]
2019-09-18 19:14:42 +00:00
- [[#lsp-java ][LSP Java ]]
2017-05-22 14:16:12 +00:00
- [[#installation ][Installation ]]
2019-09-18 19:14:42 +00:00
- [[#meghanada ][Meghanada ]]
2017-05-22 14:16:12 +00:00
- [[#installation-1 ][Installation ]]
2019-09-18 19:14:42 +00:00
- [[#about-maven ][About Maven ]]
2017-05-22 14:16:12 +00:00
- [[#key-bindings ][Key bindings ]]
2019-09-18 19:14:42 +00:00
- [[#lsp-java-1 ][LSP Java ]]
- [[#code-completion ][Code completion ]]
- [[#debugger ][Debugger ]]
2017-05-22 14:16:12 +00:00
- [[#meghanada-1 ][Meghanada ]]
- [[#server ][Server ]]
- [[#navigation ][Navigation ]]
- [[#compilation ][Compilation ]]
- [[#tests-junit ][Tests (junit) ]]
- [[#refactoring ][Refactoring ]]
- [[#tasks ][Tasks ]]
2017-12-17 23:39:55 +00:00
- [[#maven ][Maven ]]
- [[#gradle ][Gradle ]]
2015-07-04 04:55:38 +00:00
* Description
2017-11-29 19:47:06 +00:00
This layer adds support for the Java language.
** Features:
- Support for the following backends:
2019-09-18 19:14:42 +00:00
- [[https://github.com/emacs-lsp/lsp-java ][LSP Java ]] client/server (default)
- [[https://github.com/mopemope/meghanada-emacs ][Meghanada ]] client/server
2019-09-30 01:30:53 +00:00
- Auto-completion using company
- Linting using flycheck integration
- Maven and Gradle integration
- Interactive debugger using [[https://github.com/emacs-lsp/dap-mode ][dap-mode ]]
2015-07-04 04:55:38 +00:00
* Layer Installation
** Layer
2016-01-06 05:21:55 +00:00
To use this configuration layer, add it to your =~/.spacemacs= . You will need to
add =java= to the existing =dotspacemacs-configuration-layers= list in this
2017-02-09 22:22:38 +00:00
file.
2017-03-19 17:15:33 +00:00
** Choosing a backend
2017-02-09 22:22:38 +00:00
To choose a default backend set the layer variable =java-backend= :
#+BEGIN_SRC elisp
2019-09-30 04:49:44 +00:00
(java :variables java-backend 'meghanada)
2016-11-14 22:09:54 +00:00
#+END_SRC
2015-07-04 04:55:38 +00:00
2019-09-30 04:49:44 +00:00
Alternatively the =lsp= backend will be automatically chosen if the layer =lsp=
is used and you did not specify any value for =java-backend= .
2017-02-09 22:22:38 +00:00
Backend can be chosen on a per project basis using directory local variables
(files named =.dir-locals.el= at the root of a project), an example to use the
2019-09-18 19:14:42 +00:00
=lsp= backend:
2017-02-09 22:22:38 +00:00
#+BEGIN_SRC elisp
2018-09-19 03:54:47 +00:00
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")
2017-02-09 22:22:38 +00:00
2019-09-18 19:14:42 +00:00
((java-mode (java-backend . lsp)))
2017-02-09 22:22:38 +00:00
#+END_SRC
*Note:* you can easily add a directory local variable with ~SPC f v d~ .
2017-03-19 17:15:33 +00:00
* Backends
2019-09-18 19:14:42 +00:00
** LSP Java
LSP Java is the Java adapter for [[https://github.com/emacs-lsp/lsp-mode ][LSP Mode ]] which is the Emacs client for [[https://github.com/Microsoft/language-server-protocol ][Language Server Protocol ]].
It integrates with =company= and =flycheck= . In addition it also provides
full graphical debugging support via =DAP mode= .
LSP is the default backend used by this layer, see [[#choosing-a-backend ][Choosing a backend ]] in
order to lean how to select a different backend.
*** Installation
The lsp server will be installed automatically whenever a Java file
is opened.
2017-03-19 17:15:33 +00:00
** Meghanada
Meghanada is composed of a lightweight server and an Emacs minor mode. It
is unintrusive and integrates with =company= and =flycheck= .
Both =Gradle= and =Maven= projects are compatible with Meghanada.
*** Installation
The Meghanada server will be installed automatically whenever a Java file
is opened.
If you get issues with the auto-installation you can either download the last
version of the server manually [[https://dl.bintray.com/mopemope/meghanada/ ][there ]] or build from source hosted on [[https://github.com/mopemope/meghanada-server][the GitHub
repository]] and copy the =jar= file to =.emacs.d/.cache/meghanada= folder.
No additional configuration is necessary!
*** About Maven
Maven is not fully supported by Meghanada (but should still work fine for most
projects). If you encounter disconnection from the server then run =mvn install=
at the root of your project first.
* Key bindings
2018-08-21 18:47:48 +00:00
** LSP Java
2019-09-18 19:14:42 +00:00
*** Code completion
2018-12-05 03:03:03 +00:00
2019-05-12 10:37:48 +00:00
| Key binding | Description |
|---------------+------------------------------------------|
| ~SPC m g g~ | Go to definition |
| ~SPC m g r~ | Find references |
| ~SPC m g R~ | Peek references using ~lsp-ui~ |
| ~SPC m g d~ | Goto type definition |
| ~SPC m g a~ | Search type in project |
| ~SPC m g A~ | Search type in project using ~lsp-ui~ |
| ~SPC m h h~ | Describe thing at point |
| ~SPC m e l~ | List project errors/warnings |
| ~SPC m p u~ | Refresh user settings |
| ~SPC m e a~ | Execute code action |
| ~SPC m q r~ | Restart workspace |
| ~SPC m r o i~ | Organize imports |
| ~SPC m r r~ | Rename symbol |
| ~SPC m r a i~ | Add import |
| ~SPC m r a m~ | Add unimplemented methods |
| ~SPC m r c p~ | Create parameter |
| ~SPC m r c f~ | Create field |
| ~SPC m r e c~ | Extract constant |
| ~SPC m r e l~ | Extract local |
| ~SPC m r e m~ | Extract method |
| ~SPC m r c i~ | Convert to static import |
| ~SPC m r a t~ | Add throws exception |
| ~SPC m r a a~ | Assign all parameters to fields |
| ~SPC m r a f~ | Assign parameter to field |
| ~SPC m r g g~ | Generate getters/setters |
| ~SPC m r g e~ | Generate =equals= and =hashCode= methods |
| ~SPC m r g t~ | Generate =toString= method |
| ~SPC m r g o~ | Generate method overrides |
| ~SPC m c c~ | Build project |
| ~SPC m c p~ | Create spring boot project |
| ~SPC m a n~ | Actionable notifications |
| ~SPC m =~ | Format code |
2018-12-05 03:03:03 +00:00
2019-09-18 19:14:42 +00:00
*** Debugger
2019-09-30 01:30:53 +00:00
Using the =dap= layer you'll get access to all the DAP key bindings, see the
complete list of key bindings on the [[https://github.com/syl20bnr/spacemacs/tree/develop/layers/%2Btools/dap#key-bindings ][dap layer description ]].
2018-12-05 03:03:03 +00:00
2019-09-18 19:14:42 +00:00
** Meghanada
*** Server
| Key binding | Description |
|-------------+----------------------------------------------|
| ~SPC m D c~ | Connect to server |
| ~SPC m D d~ | Disconnect from server |
| ~SPC m D i~ | Install server (should be done automatically |
| ~SPC m D k~ | Kill server |
| ~SPC m D l~ | Clear server cache |
| ~SPC m D p~ | Ping server |
| ~SPC m D r~ | Restart server |
| ~SPC m D s~ | Start server |
| ~SPC m D u~ | Update server |
| ~SPC m D v~ | Print version of the server |
*** Navigation
| Key binding | Description |
|-------------+--------------------------------|
| ~SPC m g b~ | Jump back to previous location |
| ~SPC m g g~ | Jump to declaration |
*** Compilation
| Key binding | Description |
|-------------+-----------------------|
| ~SPC m c b~ | Compile file (buffer) |
| ~SPC m c c~ | Compile project |
*** Tests (junit)
| Key binding | Description |
|-------------+-----------------------------|
| ~SPC m t a~ | Run all tests |
| ~SPC m t c~ | Run test class around point |
| ~SPC m t l~ | Run last tests |
| ~SPC m t t~ | Run test cause around point |
*** Refactoring
| Key binding | Description |
|-------------+----------------------------------------|
| ~SPC m =~ | Beautify code |
| ~SPC m r i~ | Optimize imports |
| ~SPC m r I~ | Import all |
| ~SPC m r n~ | Create a new class, interface, or enum |
*** Tasks
| Key binding | Description |
|-------------+-------------|
| ~SPC m x :~ | Run task |
2017-12-17 23:39:55 +00:00
** Maven
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2017-12-17 23:39:55 +00:00
|-----------------+------------------------------------------------------|
| ~SPC m m c c~ | Compile |
| ~SPC m m c C~ | Clean |
| ~SPC m m c r~ | Clean and compile |
| ~SPC m m g a~ | Switch between class and test file |
| ~SPC m m g A~ | Switch between class and test file in another window |
| ~SPC m m t a~ | Run all tests |
| ~SPC m m t C-a~ | Clean and run all tests |
| ~SPC m m t b~ | Run current buffer tests |
| ~SPC m m t i~ | Test and install |
| ~SPC m m t t~ | Run a specific test |
** Gradle
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2017-12-17 23:39:55 +00:00
|---------------+--------------------------|
| ~SPC m l c c~ | Compile |
| ~SPC m l c C~ | Clean |
| ~SPC m l c r~ | Clean and compile |
| ~SPC m l t a~ | Run all tests |
| ~SPC m l t b~ | Run current buffer tests |
| ~SPC m l t t~ | Run a specific test |
| ~SPC m l x~ | Execute a Gradle task |