Maven Tutorial
Maven in IDE
Creating a new Maven project is a straightforward process. You can either use the command line or an Integrated Development Environment (IDE) to generate a Maven project. In this tutorial, we'll cover both methods.
Method 1: Command Line
Install Maven: Before creating a Maven project, ensure you have Maven installed on your system. If you haven't installed it yet, follow the installation guide for your operating system: https://maven.apache.org/install.html.
Open terminal or command prompt: Open a terminal (Linux or macOS) or command prompt (Windows).
Navigate to the desired location: Navigate to the directory where you want to create your Maven project.
Generate the project: Run the following command to generate a new Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Replace com.example
with your desired group ID and my-project
with your desired artifact ID.
This command generates a new Maven project using the maven-archetype-quickstart
archetype, which is a basic template for creating a simple Java project.
cd my-project
Replace my-project
with the artifact ID you used in the previous step.
mvn clean install
This command compiles, tests, and packages your project.
Method 2: Integrated Development Environment (IDE)
Most modern IDEs, such as IntelliJ IDEA or Eclipse, provide built-in support for creating Maven projects. In this example, we'll demonstrate how to create a Maven project using IntelliJ IDEA:
Install IntelliJ IDEA: Download and install IntelliJ IDEA from their official website: https://www.jetbrains.com/idea/download/.
Open IntelliJ IDEA: Launch IntelliJ IDEA.
Create a new project: Click on "Create New Project" on the welcome screen or go to "File" > "New" > "Project."
Select Maven: In the "New Project" window, select "Maven" from the options on the left.
Configure the project: Choose the JDK you want to use, and enter the desired GroupId and ArtifactId for your project. You can also choose additional project options, like the location and project name.
Create the project: Click "Finish" to create the Maven project.
Build the project: To build the project in IntelliJ IDEA, open the "Maven" tool window, usually located on the right side of the IDE, and expand the "Lifecycle" section. Double-click "clean" and "install" to build the project.
Your new Maven project is now ready for development. You can add source code, dependencies, and configure plugins in the pom.xml
file as needed.
Creating a Maven project from the command line:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Configuring project coordinates in Maven POM.xml:
groupId
, artifactId
, and version
in the POM.xml to uniquely identify your project.<!-- Example project coordinates in POM --> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version>
Adding dependencies to a new Maven project:
<dependencies>
section of the POM.xml.<!-- Example dependency in POM --> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> <!-- Additional dependencies... --> </dependencies>
Initializing a multi-module Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-parent-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=internal
Choosing Maven archetypes for project creation:
maven-archetype-quickstart
for a simple Java project.mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Integrating Maven projects with version control:
git init
.gitignore
file to exclude unnecessary files from version control.