How to create a Java / Maven project that works in Visual Studio Code?
I'm trying to create a maven project - so that I can compile Java files in the root folder and output the class files in another folder.
I've already downloaded mvn.
I'm trying to integrate with VS Code. My goal is to edit the java files in VS Code and on saving the compiler saves the .class file in the appropriate output folder.
That's all - no war or jar files.
Any help?
Solution 1:
Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-
- Download VS Code and Apache Maven and install both.
- Install the Visual Studio extension pack for Java - e.g. by pasting this URL into a web browser:
vscode:extension/vscjava.vscode-java-pack
and then clicking on the green Install button after it opens in VS Code. -
NOTE: See the comment from ADTC for an "Easier GUI version of step 3...(Skip step 4)." If necessary, the Maven quick start archetype could be used to generate a new Maven project in an appropriate local folder:
mvn archetype:generate -DgroupId=
com.companyname.appname-DartifactId=
appname-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
. This will create an appname folder with Maven's Standard Directory Layout (i.e.src/main/java/com/companyname/appname
andsrc/main/test/com/companyname/appname
to begin with and a sample "Hello World!" Java file named appname.java
and associated unit test named appnameTest.java
).* - Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
- Open the Command Palette (via the View menu or by right-clicking) and type in and select
Tasks: Configure task
then selectCreate tasks.json from template
. -
Choose maven ("Executes common Maven commands"). This creates a tasks.json file with "verify" and "test" tasks. More can be added corresponding to other Maven Build Lifecycle phases. To specifically address your requirement for classes to be built without a JAR file, a "compile" task would need to be added as follows:
{ "label": "compile", "type": "shell", "command": "mvn -B compile", "group": "build" },
Save the above changes and then open the Command Palette and select "Tasks: Run Build Task" then pick "compile" and then "Continue without scanning the task output". This invokes Maven, which creates a
target
folder at the same level as thesrc
folder with the compiled class files in thetarget\classes
folder.
Addendum: How to run/debug a class
Following a question in the comments, here are some steps for running/debugging:-
- Show the Debug view if it is not already shown (via View menu - Debug or CtrlShiftD).
- Click on the green arrow in the Debug view and select "Java".
- Assuming it hasn't already been created, a message "launch.json is needed to start the debugger. Do you want to create it now?" will appear - select "Yes" and then select "Java" again.
- Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
- Click on the green arrow in the Debug view again.
Solution 2:
An alternative way is to install the Maven for Java
plugin and create a maven project within Visual Studio. The steps are described in the official documentation:
- From the Command Palette (Crtl+Shift+P), select Maven: Generate from Maven Archetype and follow the instructions, or
- Right-click on a folder and select Generate from Maven Archetype.
Solution 3:
This is not a particularly good answer as it explains how to run your java code n VS Code and not necessarily a Maven
project, but it worked for me because I could not get around to doing the manual configuration myself. I decided to use this method instead since it is easier and faster.
Install VSCode (and for windows, set your environment variables), then install vscode:extension/vscjava.vscode-java-pack
as detailed above, and then install the code runner extension pack, which basically sets up the whole process (in the background) as explained in the accepted answer above and then provides a play button to run your java code when you're ready.
This was all explained in this video.
Again, this is not the best solution, but if you want to cut to the chase, you may find this answer useful.