Using Maven for Project Management
In this step, you will learn how to use Apache Maven to manage your Java project. Maven is a powerful build automation and dependency management tool that simplifies project setup and maintenance.
Understanding Maven
Maven provides:
- Standard project structure
- Dependency management
- Build automation
- Project information management
- Consistent build process across projects
Setting Up a Maven Project
Let's create a new project using Maven:
- First, check if Maven is installed:
mvn --version
You should see output similar to this:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.18, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-1036-azure", arch: "amd64", family: "unix"
- Navigate to your project directory:
cd /home/labex/project
- Create a new Maven project using an archetype (a project template):
mvn archetype:generate \
-DgroupId=com.example.calculator \
-DartifactId=simple-calculator \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false
This command will create a new project with a standard directory structure.
- Examine the project structure:
cd simple-calculator
ls -la
You should see output similar to:
total 24
drwxr-xr-x 4 labex labex 4096 ... .
drwxr-xr-x 6 labex labex 4096 ... ..
-rw-r--r-- 1 labex labex 174 ... .gitignore
-rw-r--r-- 1 labex labex 720 ... pom.xml
drwxr-xr-x 4 labex labex 4096 ... src
The key file here is pom.xml
(Project Object Model), which defines the project configuration.
- Examine the Maven standard directory layout:
find src -type d
You should see:
src
src/main
src/main/java
src/main/java/com
src/main/java/com/example
src/main/java/com/example/calculator
src/test
src/test/java
src/test/java/com
src/test/java/com/example
src/test/java/com/example/calculator
This is the Maven standard directory layout:
src/main/java
: Source code
src/main/resources
: Resource files
src/test/java
: Test code
src/test/resources
: Test resources
- Let's look at the generated
App.java
file:
cat src/main/java/com/example/calculator/App.java
You should see a simple "Hello World" class:
package com.example.calculator;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Enhancing the Maven Project
Let's enhance our calculator project by adding more classes:
- Create a new file called
Calculator.java
in src/main/java/com/example/calculator/
:
package com.example.calculator;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public double divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return (double) a / b;
}
}
- Now, modify the existing
App.java
file to use our Calculator class:
package com.example.calculator;
/**
* Simple Calculator Application
*/
public class App
{
public static void main( String[] args )
{
Calculator calculator = new Calculator();
// Perform some calculations
System.out.println("Addition: 5 + 3 = " + calculator.add(5, 3));
System.out.println("Subtraction: 10 - 4 = " + calculator.subtract(10, 4));
System.out.println("Multiplication: 6 * 7 = " + calculator.multiply(6, 7));
System.out.println("Division: 20 / 4 = " + calculator.divide(20, 4));
System.out.println("Calculator application completed successfully!");
}
}
- Build the project using Maven:
mvn compile
You should see output ending with:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
- Package the application into a JAR file:
mvn package
This command compiles your code, runs tests, and packages the application.
- Run the packaged application:
java -cp target/simple-calculator-1.0-SNAPSHOT.jar com.example.calculator.App
You should see the output:
Addition: 5 + 3 = 8
Subtraction: 10 - 4 = 6
Multiplication: 6 * 7 = 42
Division: 20 / 4 = 5.0
Calculator application completed successfully!
Understanding the Maven POM File
The Project Object Model (POM) file contains the project configuration. Open the pom.xml
file in the editor and examine its structure:
<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.example.calculator</groupId>
<artifactId>simple-calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>simple-calculator</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build configuration... -->
</project>
Key elements in the POM file:
groupId
: Organization or project identifier
artifactId
: Project name
version
: Project version
dependencies
: External libraries used by the project
build
: Configuration for building the project
Maven Key Commands
Here are some essential Maven commands:
mvn compile
: Compiles source code
mvn test
: Runs tests
mvn package
: Creates distributable package (JAR, WAR)
mvn install
: Installs package in local repository
mvn clean
: Removes build artifacts (target directory)
mvn clean install
: Combination of clean and install
Maven has dramatically simplified Java project management by providing conventions, dependency management, and build automation. This standardized approach helps developers focus on writing code rather than managing project structure.