Intermediate level · Module 4: Packages and Builds with Maven · Lesson 2 of 3
Maven Projects and Dependencies
Create a Maven project, understand pom.xml and add libraries as dependencies instead of copying JAR files.
In this lesson you will
- Explain the standard Maven directory layout and pom.xml
- Add and use a dependency from Maven Central
Key concepts
pom.xmlgroupId, artifactId, versiondependency scopelocal repository
The lesson
Maven is a build tool that compiles code, runs tests, manages libraries and packages applications. A Maven project has a standard layout: src/main/java for code, src/test/java for tests and pom.xml at the root describing the project.
Each project and library is identified by groupId, artifactId and version. Adding a dependency to pom.xml tells Maven to download that library from Maven Central into your local repository and put it on the classpath. Scope test means the library is used only for tests.
Never copy JAR files into a project by hand. With Maven, anyone can build the project with the same library versions just by running a command, which is essential when working in a team or on a server.
Step by step
- Create a new Maven project in IntelliJ.
- Find the standard folders and open pom.xml.
- Add the JUnit Jupiter dependency with test scope.
- Reload the project and confirm the library is available.
Practice activity
Convert the library project into a Maven project with the correct folder layout and a pom.xml that sets the Java version.
Expected result: The project opens and builds as a Maven project in IntelliJ and from the terminal.
Check your understanding
-
Where do test classes go in a Maven project?
Show answer
src/test/java.
-
Which three values identify a Maven artifact?
Show answer
groupId, artifactId and version.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.