Almost all the articles in Java Helps with a Java project use Apache Maven to create the project. This article shows how to create a new Apache Maven project in IntelliJ IDEA for beginners. Before getting your hands dirty let’s get yourself familiar with some keywords used in the Apache Maven context.
Requirements
- Intellij IDEA
- Apache Maven (Optional)
Apache Maven Terms
Term | Description |
---|---|
POM/pom.xml | Project Object Model. It is an XML file that contains information about the project and configuration details used by Maven to build the project. You will find this file in the project directory. |
Artifact | The output generated by building a Maven Project. Usually, it refers to the JAR files compiled from the source code. |
Group Id | A unique identifier for your project across all projects. The naming convention follows Java’s package name rules. This means it starts with a reversed domain name you control. For example, I use com.javahelps.project as the groupId for all my projects. |
Artifact Id | The name of your package (JAR most of the time) without the version number. Recommended using lowercase letters and no strange symbols (usually lowercase words separated by hyphens). |
Version | The package version. |
Snapshot Version | Version ending with -SNAPSHOT indicating the built package (JAR file) is under development and not for production use. Though you can still use it for production at your own risk. |
Dependency | Any library your project is depending on. |
Plugin | Additional components to extend the functionality of Apache Maven. |
Archetype | Apache Maven project templates that can be used to generate a particular type of project. For example, the scala-archetype-simple archetype can be used to create Scala projects. |
Maven Repository | A public repository hosting almost all Apache Maven dependencies (aka libraries). This is where you will find all your dependencies. The URL is: https://mvnrepository.com |
Local Repository | To avoid downloading the same dependencies, again and again, Apache Maven will maintain a local copy of the artifacts on your computer. Linux and Mac users can find it at ~/.m2/repository |
Creating A New Maven Project
Step 1:
Open IntelliJ IDEA and go to File → New → Project from the menubar or click on the “New Project” button on the welcome screen.
Step 2:
In the appeared dialog, enter the artifact id as the project name and select a project location to save the project. In the same dialog, select Java as the language along with your desired JDK version. Also, make sure that the Build system is set to Maven. Next, expand the Advanced Settings section and enter your Group Id. Finally, click the “Create” button.
In the following screenshot, I am creating a new project with the Group Id: com.javahelps.helloworld and the Artifact Id: hello-world.
Exploring The Maven Project
The created project will have the following file structure:
pom.xml
The pom.xml file contains the groupId, artifactId, version, and Java version for this project. The original pom.xml file may look like the following:
Suppose you want to add commons-lang3 and commons-collections4 libraries as dependencies to your project, you will have to modify the project as shown below:
You can add any number of dependencies to your project inside the <dependencies> tag. After modifying the pom.xml, make sure to load the changes into IntelliJ IDEA by clicking the popup that appears in the top right corner. Otherwise, right-click on the project and select Maven → Reload Project.
Main.java
Main.java is the main class created for your convenience. You can modify it, delete it or create new classes as you wish. Click the play button on the left side panel next to the class definition or main method to run the project from IntelliJ IDEA.
Maven Command Line
Alternatively, if you have Apache Maven installed, you can also run the following commands from the terminal to build and run the Main class.
Running the following command will compile and package your project into a JAR file.
I hope this article helped you create your first Apache Maven project in IntelliJ IDEA. If you find it useful or if you have any questions, please comment below.