Reputation: 3018
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?
Upvotes: 91
Views: 228171
Reputation: 39424
Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-
vscode:extension/vscjava.vscode-java-pack
and then clicking on the green Install button after it opens in VS Code.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
and src/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
).*Tasks: Configure task
then select Create tasks.json from template
. {
"label": "compile",
"type": "shell",
"command": "mvn -B compile",
"group": "build"
},
target
folder at the same level as the src
folder with the compiled class files in the target\classes
folder.Addendum: How to run/debug a class
Following a question in the comments, here are some steps for running/debugging:-
Upvotes: 136
Reputation: 357
Maven for Java
Upvotes: 1
Reputation: 596
I surprise no one had mentioned this possible easy approach in visual studio code.
Install VS Code and Apache maven ( just as mentioned by @Steve Chambers)
After installing this extension vscode:extension/vscjava.vscode-java-pack
In the java overview page , there is a an option which reads 'Create Maven Project' which further takes to a simple wizard to generate maven project.
Its pretty quick which is intutitive enough, even newbies can very well start with a Maven project.
Upvotes: 1
Reputation: 902
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.
Upvotes: 1
Reputation: 453
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:
Upvotes: 12