INTRODUCTION
In today's free Java lesson, we will go step-by-step on how to create a Java project using Maven, how to compile our project, and finally, how to run it.
All the above steps will be performed by chosing the correct menu options within VS Code.
JAVA MAVEN PROJECT CREATION
To create a Java application, we need to start by setting up a Java Maven project. First, we create a folder anywhere we want on our hard drive. The name of the folder doesn't matter at all.
Now we open VS Code. From the main menu, we select File -> Open
Folder, and then
we click on Select Folder to select the folder we have created on the previous step. VS Code now displays the location of the folder
we have selected.
The next step is to create a Java Project inside the Java folder. Go to View -> Command Palette.
In the search field, type "Java" to display all the options that VS Code (via extensions) offers for this programming language. If you had not installed the Java extensions (as we did in UNIT 1), these options would not be available. Select Java: Create Java Project.
From the displayed options (depending on the extensions you have installed), choose Maven: Create from Archetype.
The Apache Maven offers archetypes – ready-made templates containing the minimum requirements for a Java project to build and run correctly. From the displayed options, select the template named maven-archetype-quickstart.
When you select the specific archetype, the version number of the archetype will immediately appear. Simply choose the latest version.
In the next step, Maven suggests a default name for the package where your Java classes will reside. Think of a package as a folder containing your Java files. Each class can belong to the same or a different package. Later, when you understand the concept of packages and how they help organize your code, you will assign better names to them. For now, simply accept the suggested name com.example provided by Maven.
In Maven, packages are officially referred to as the group id. So, if you’re using a different IDE and it asks you to name the group id, it’s essentially asking you to define a default package where your Java files will be placed.
This explanation of packages is very basic, but the main goal we try to accomplish right now is to understand the steps to create a basic Java Maven Project, and not to analyze every detail. These concepts will become clearer in future lessons.
So, press ENTER and accept the suggested name for the group id.
Now it's time to choose a name for our project. Maven's suggested name is demo, but let's change it and name our first project employeemanagement. The official term Maven uses for naming a project is artifact id. After typing the name, press ENTER.
The default location for our project will be the folder we initially added to VS Code. That is why, after pressing ENTER in the previous step, you will see the same folder is suggested as the default location for creating the Java Maven Project. Simply accept this choice by clicking Select Destination Folder.
We are almost done. Now, in the VS Code Terminal, you will see the process Apache Maven is following to create the project. However, it will pause at some point because it is waiting for us to decide what the version of the code will be.
When working on a large project, it is normal to make continuous updates to the code. As a result, the version number of your application will change. When the application code is still in progress but not yet completed, it is common to append the word SNAPSHOT to the version number. So, Apache Maven assumes that since you are creating the project now, your code is still in progress and suggests an initial version number with the word SNAPSHOT.
Simply accept this choice by pressing ENTER.
The final step is to accept all the choices we made during this process. Simply press ENTER, and the creation of our Java project will be completed.
The Java Maven Project has been created, and we can now see its structure in the EXPLORER section of VS Code. Additionally, the Maven extension that we added in UNIT 1 recognizes our project as an official Maven project.
You should now be able to see the following typical structure for a Maven project:
- src/main/java: This is where your Java source files are located.
- src/test/java: This is where your test files are located (for unit tests, for example).
- pom.xml: This is the project’s configuration file, where dependencies, plugins, and other Maven-specific settings are defined.
You can now begin working with your project, adding classes and logic as needed!
MODIFYING THE
POM.XML
Before compiling our project, we need to make a few small changes related to the version numbers of the libraries. The reason is that the archetype we used is based on Java 7, while we are using Java 23.
So, let's follow these steps:
- Click on
pom.xml
to open the file. - Inside the
pom.xml
, we will update the version numbers of the dependencies and the Java version to reflect the use of Java 23.
You will likely need to modify the maven-compiler-plugin
configuration to specify the correct version of Java. Look for the <properties>
section and adjust the Java version like this:
The first and major change is to replace the maven.compiler.source and maven.compiler.target number with 23, which is the version of Java we have installed on our system.
The pom.xml
file is where we declare all the dependencies that our project relies on for proper execution. Once declared in the pom.xml
, the dependencies will be automatically downloaded from the Maven repository.
For our basic project format, we do not need all the plugins that the maven template comes with. So, we can simply delete them all including the test folder.
MAVEN CLEAN & INSTALL COMMANDS
Now we are ready to compile our project. Although we can run the Maven commands from the terminal, we choose to use VS Code menu options that provide an easier way to select and execute them.
To view the commands, right-click on the employeemanagement project under the Maven category. From the context menu that appears, select to run the clean command first.
The clean command in Maven is used to remove any previously compiled files, ensuring that the next compilation is fresh and does not include any outdated or residual artifacts from previous builds. This helps in avoiding potential issues during the build process.
After running the clean command, you can proceed to run the compile or other Maven goals as needed.
Following the same process, now we need to run the install command. This command will rebuild the project from scratch and generate an executable file with a .jar
extension.
To do this:
- Right-click on the employeemanagement project under the Maven category again.
- From the context menu, select the install command.
The install command compiles the project, runs tests (if any), and then packages the project into a .jar
file. This file can be found in the target
directory of your project. This .jar
file is your project's executable artifact, which can be run or distributed.
RUNNING A JAVA PROJECT
Now we are ready to run our program. Every Java application, in order to be executable, must consist of at least one class, and one of those classes must contain the main()
method. This method is the entry point for the execution of any Java application.
Don't worry if you don't understand all these concepts just yet. We will explain them thoroughly in the next unit. For now, just keep in mind that the main()
method is where the program starts executing.
In order to run our application, we need to open the App.java class and locate the main()
method. Once you have located the main()
method inside App.java, you can run your application.
To help us run our program, VS Code automatically adds the Run | Debug option right before the definition of the main()
method when it detects the presence of the method in our code. After running it, you should expect to see "Hello World" as the output in the terminal or output panel, confirming that your Java application has run successfully.
With this process, we have completed our guide on how to create and run a Java Maven Project. For now, just remember that every time you write new code or modify existing code in your class, you should execute the clean and install commands before running your application.
This ensures that the project is properly rebuilt and all the changes are included in the execution, avoiding any issues with outdated or incomplete builds.
Don't forget to make a small donation so that this blog can grow even more and offer additional features for delivering free online courses.
full-width
0 Comments
What do you think about Ground of Code?