87

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?

2

5 Answers 5

131
+250

Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-

  1. Download VS Code and Apache Maven and install both.
  2. 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.
  3. 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 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).*
  4. Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
  5. Open the Command Palette (via the View menu or by right-clicking) and type in and select Tasks: Configure task then select Create tasks.json from template.
  6. 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"
    },
  1. 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 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:-

  1. Show the Debug view if it is not already shown (via View menu - Debug or CtrlShiftD).
  2. Click on the green arrow in the Debug view and select "Java".
  3. 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.
  4. Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
  5. Click on the green arrow in the Debug view again.
8
  • Thank you - I was able to get the above to work. Two quick questions - looks like a package name is required. Anyway to create one without a package name and/or more control over the folder structure. Also, if I've got a java file in the editor, how to run it.
    – dashman
    Oct 13, 2017 at 21:59
  • Not sure I've fully understand the first part of your question - are you saying you want to have a class without a package name explicitly specified, i.e. in the default package? Maven's folder structure is pretty standard - not sure why you wouldn't want to use this and it's outside the realms of anything I've tried before but here's some more info here if it helps: maven.apache.org/guides/mini/…. Re: Second part of your question I've added some steps to run a class that ought to work, although on my setup they didn't (possibly a Firewall issue). Oct 16, 2017 at 10:04
  • 2
    Agreed - I'd personally stick with Eclipse or IntelliJ. At present, VS Code for Java doesn't really seem to be mature enough for serious Java development, although that may change... Oct 17, 2017 at 9:09
  • 1
    It should be "label" instead of "taskName"
    – moondaisy
    Jan 13, 2018 at 17:14
  • 16
    Easier GUI version of step 3: Open VSCode on the empty folder where you want to create the project. Right-click the empty space in Explorer pane (in VSCode sidebar) and click 'Generate from Maven Archetype', then click Select Destination (on the same empty folder). Select 'maven-archetype-quickstart'. A terminal will open with VSCode and after downloading some updates, Maven will prompt for the groupId, artifactId and Java package name. Enter that info, and you're done! (Skip step 4.)
    – ADTC
    Aug 9, 2018 at 18:41
10

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:

  1. From the Command Palette (Crtl+Shift+P), select Maven: Generate from Maven Archetype and follow the instructions, or
  2. Right-click on a folder and select Generate from Maven Archetype.
1

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.

1

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.

1
  • Install the VSCode for mac
  • Install below plugin in VSCode

Maven for Java

  • Get the JDK(open/licensed)
  • cmd + shift + p & create maven project

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.