Emacs lsp-java Project Settings
Update: 2023-2-25 use eglot instead(in the last of the article)
Create a java project with third libriaries support
lsp-java
is an awesome package for lsp-mode
to open a java project. However, after some testing, I found it
has difficulty in importing third party libraries with pure java project(not use maven, Gradle). Moreover, it
only has a limited ability to interact with a Gradle project. However, it has great support for maven
project, which
means we can use maven to import third party java libraries.
This article will focus on import a local library by maven.
Initialize a project
|
|
This command will create a folder named PacMan=(=artifactId
), where there is folder called src
, and its structure is like:
|
|
If your groupId
is "com.pacman.xx", then the directory structure may be like:
|
|
In our example, you can write code in the pacman
folder.
Specify java version:
edit pom.xml
file in the project root directory
|
|
Add third party libriary
|
|
Note that ${basedir}
stands for the directory that pom.xml
located.
Run Project:
create a Makefile
in the project root directory:
|
|
Use command make
to run, and make clean
to clean
In doom emacs
, you can type <SPC>pR
and enter make
to run the project
Eglot
- make sure you have installed `jdtls`, and have a system java version >= 17
to run the java project, you can use this `Makefile`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Makefile for c/c++ program # # ======================================== # @author: Ziqi Yang<mr.ziqiyang@gmail.com> # @reference: https://makefiletutorial.com/#makefile-cookbook # ======================================== SRC_DIR := src LIBS_DIR := libs BUILD_DIR := target ENTRY_POINT := Main ENTRY_FILE := $(SRC_DIR)/Main.java LIBS := $(shell find $(LIBS_DIR) -name "*.jar" | paste -sd ":" -) ARGS := .PHONY: build run build: mkdir -p $(BUILD_DIR) javac -d $(BUILD_DIR) -cp .:$(LIBS) -sourcepath $(SRC_DIR) $(ENTRY_FILE) run: java -cp $(BUILD_DIR):$(LIBS) $(ENTRY_FILE) $(ARGS)