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

Apache Maven Terms

TermDescription
POM/pom.xmlProject 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.
ArtifactThe output generated by building a Maven Project. Usually, it refers to the JAR files compiled from the source code.
Group IdA 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 IdThe 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).
VersionThe package version.
Snapshot VersionVersion 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.
DependencyAny library your project is depending on.
PluginAdditional components to extend the functionality of Apache Maven.
ArchetypeApache 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 RepositoryA 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 RepositoryTo 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:

hello-world/
├── pom.xml
└── src/
├── main/
│ ├── java/
│ │ └── com/
│ │ └── javahelps/
│ │ └── helloworld/
│ │ └── Main.java
│ └── resources/
└── test/
└── java/

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:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.javahelps.helloworld</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

</project>

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:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.javahelps.helloworld</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
</dependencies>

</project>

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.

mvn compile
mvn exec:java -Dexec.mainClass=com.javahelps.helloworld.Main


Running the following command will compile and package your project into a JAR file.

mvn package

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.

Share.
Exit mobile version