Java Essentials: Building a Full Project with Maven
Why Maven
Real Java applications have dependencies, resources, and tests. Maven is a build tool that brings order to all of it. It reads one central file, pom.xml, to learn what the project contains, which libraries it needs, and how the final artifact should be packaged.
The Project Object Model
Every Maven project starts with a pom.xml that declares the project coordinates and its dependencies.
<project>
<groupId>com.example</groupId>
<artifactId>greeter</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.0.0-jre</version>
</dependency>
</dependencies>
</project>The groupId and artifactId together uniquely identify your library. The version pins the exact release that Maven downloads from a public repository such as Maven Central. Dependencies are cached locally, so later builds are fast.
The Build Lifecycle
Maven orders work into phases. The most useful ones are:
- compile - turns sources into classes.
- test - runs unit tests.
- package - builds a jar or war file.
- install - puts the artifact into the local repository.
mvn compile
mvn test
mvn clean packagemvn clean package removes old output, compiles everything, runs the tests, and produces a ready-to-run artifact under the target folder.
A Working Main Class
With the exec plugin or a modern jar plugin, you can run a simple application after building.
public class App {
public static void main(String[] args) {
System.out.println("Project builds and runs");
}
}A Maven project usually follows a standard folder layout: sources live under src/main/java, tests under src/test/java, and resources under src/main/resources. Following this convention means Maven finds your code without extra configuration.
Key Points
- Maven reads pom.xml to manage dependencies and builds.
- groupId, artifactId, and version uniquely identify a project.
- Phases such as compile, test, and package run in a defined order.
- The standard source layout avoids manual configuration.