Issue
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
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.
Answered By - Steve Chambers
Answer Checked By - Clifford M. (JavaFixing Volunteer)