Neovim and Java with LazyVim, part 3: Dependencies and Maven

This is the third part of a few tutorials on Java development with Neovim using the LazyVim setup.

This post assumes you already read and applied all the steps of the first part and the second part.

You need Java installed (possibly 21), while Maven is optional.

I will use this Maven example during the tutorial: https://github.com/LorenzoBettini/maven-bank-example, which is part of my TDD book.

The final result of this series of tutorials can be found here: https://github.com/LorenzoBettini/lazyvim-java. The “main” branch always points to the latest blog post.

The end of this part is still the branch “third-blog-post”.

In the first part, we saw how to enable Java LSP in the LazyVim distribution. We also saw a few interesting features for programming in Java in Neovim with such a configuration. In the second part, we saw a few of the IDE mechanisms we get from the Java LSP in LazyVim.

Let’s continue where we left off (remember, I’ll use the above-mentioned Maven project) and see how to deal with Maven dependencies.

Adding support for the pom.xml

By default, opening the pom.xml file gives only syntax highlighting but no LSP support:

We can install the XML LSP, “lemminx”, using Mason, e.g., by modifying the “extend-lsp.lua” file we saw above:

Restart Neovim and open the “pom.xml” file again. Wait some time for Mason to install the LSP, and the first thing you might notice is the status line showing the structure of the current XML element:

The XML LSP is schema-aware so that it can provide hovering support (“K”):

It also provides the symbol mechanisms shown above. It provides auto-closing of tags and rename refactoring. Moreover, it can validate what you write according to the schema (in this case, it checks that it is a valid Maven XML).

It also implements a “code action” for quick-fixing it:

Some other notes

If you add a dependency to the POM or change one of the existing dependencies version, you must run “:JdtUpdateConfig” so the Java LSP can download the new dependencies.

In general, when something is not working, “:JdtRestart” might fix things.

In case of misalignments of the workspace, e.g., if you wipe the local Maven cache (“~/.m2/repository”) when restarting an already opened project, you get errors due to unresolved dependencies:

In such a case, you can run “:JdtWipeDataAndRestart”, which wipes the Eclipse workspace (not your project) and restart the LSP, which will resolve the Maven dependencies from scratch.

That’s all for this part!

Note that for the moment, we cannot start/debug a Java program from Neovim. We’ll cover that in the next part.

We still have to cover:

  • how to run/debug Java programs (with the current configuration, this is not yet possible);
  • how to run tests
  • maybe other features

One thought on “Neovim and Java with LazyVim, part 3: Dependencies and Maven

  1. Pingback: Neovim and Java with LazyVim, part 1: initial configuration | Lorenzo Bettini

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.