Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
Open a plain project and a Maven project in IntelliJ, read an invoice app's pom.xml, follow jackson-databind from Maven Central into ~/.m2, and see why each lifecycle phase runs the ones before it.
What you will learn
- Run a plain project and a Maven project in IntelliJ by finding main
- Read a pom.xml: coordinates, properties, dependencies and test scope
- Explain how a dependency travels from Maven Central to the local repository
- Run compile, test, package and exec:java and say what each phase does
Before you start
- Java Hello World with Maven and the JDK
Most Java Foundation projects run the same way: find the file with main and click the green arrow. Maven projects add one file, pom.xml, and a fixed folder layout. This article opens both kinds of project, reads a real pom.xml, and runs the Maven lifecycle on a small invoice app.
Open a project, find main
A plain project has a src folder, one file and no pom.xml. Which file do you run? The one with the small green play arrow next to its main method.
public class WelcomeNote {
public static void main(String[] args) {
// No pom.xml, no Maven: IntelliJ compiles this with the project SDK
System.out.println("Welcome to the course, Emma Clark!");
System.out.println("Running on Java " + System.getProperty("java.version"));
}
}
Welcome to the course, Emma Clark!
Running on Java 25.0.x
IntelliJ compiles the class, then runs it. The second line prints your JDK's version, so the exact number depends on your machine.
The same steps in a Maven project
A Maven project's tree goes deeper: code in src/main/java, tests in src/test/java, and pom.xml at the top. Finding main works exactly the same. InvoiceApp only uses the JDK, so you can run it with the green arrow and no Maven command:
package org.studyeasy.s02l03;
public record Line(String item, int qty, double price) {
public double total() {
return qty * price;
}
}
package org.studyeasy.s02l03;
import java.util.List;
public class InvoiceApp {
public static void main(String[] args) {
List<Line> lines = List.of(
new Line("Keyboard", 2, 45.00),
new Line("Monitor", 1, 189.99),
new Line("USB-C cable", 3, 9.50));
System.out.println("Invoice INV-1042");
double sum = 0;
for (Line line : lines) {
System.out.printf("%-12s %6.2f%n", line.item(), line.total());
sum += line.total();
}
System.out.printf("Total: %.2f%n", sum);
}
}
Invoice INV-1042
Keyboard 90.00
Monitor 189.99
USB-C cable 28.50
Total: 308.49
Don't worry about record, List or printf yet; they all come later in the course. Here the point is the project around the code.
The pom.xml file
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.studyeasy</groupId>
<artifactId>invoice-app</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.19.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- build / plugins section shown further down -->
</project>
- Three words name the project.
groupIdis who made it,artifactIdis what it is,versionis which build. - Properties.
maven.compiler.releasesets the Java version the code is compiled for: 25. - Dependencies are named with the same three words.
jackson-databindturns Java objects into JSON.junit-jupiteris for testing, and itsscopeistest: only the tests use it, so the app itself doesn't carry it.
How a dependency arrives
Maven first looks in the local repository, a folder on your own computer, where the three words become nested folders. If the jar isn't there, Maven downloads it once from Maven Central and keeps it. The project points to it on its classpath. Two more jars come along, jackson-annotations and jackson-core, because jackson-databind needs them.
Now the code can use Jackson:
package org.studyeasy.s02l03;
import com.fasterxml.jackson.databind.ObjectMapper;
public class InvoiceJson {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Line line = new Line("Monitor", 1, 189.99);
String json = mapper.writeValueAsString(line);
System.out.println(json);
Line back = mapper.readValue(json, Line.class);
System.out.println("Read back: " + back.item());
}
}
{"item":"Monitor","qty":1,"price":189.99}
Read back: Monitor
The lifecycle: compile, test, package
Open the Maven panel (the M on the right edge). It lists lifecycle phases. Double-click one to run it.
compile
Maven first handles the resources step (there's no resources folder, so it skips it). Then the compiler turns the three source files into class files in a new target folder. BUILD SUCCESS. The program is built, but nothing has run.
test
The test code is compiled into its own folder, target/test-classes. Then the surefire plugin finds LineTest and runs it:
package org.studyeasy.s02l03;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class LineTest {
@Test
void totalIsQuantityTimesPrice() {
Line keyboard = new Line("Keyboard", 2, 45.00);
assertEquals(90.00, keyboard.total(), 0.001);
}
@Test
void oneItemCostsItsPrice() {
Line monitor = new Line("Monitor", 1, 189.99);
assertEquals(189.99, monitor.total(), 0.001);
}
}
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
If even one test failed, the build would stop here.
package
package runs the tests first, and only when both pass does it build the jar in target. The artifactId and version make up the file name, so you can tell which build it is.
Each phase runs the ones before it
| Phase | What it does |
|---|---|
| validate | checks pom.xml |
| compile | compiles the main code |
| test | runs the tests |
| package | builds the jar |
| verify | extra checks |
| install | copies the jar into your local repository |
| deploy | uploads it to a shared repository |
Run test and Maven goes through validate, compile and test in order. Run package and it does those three again, then builds the jar. Two commands sit outside this chain: clean deletes target so you can build fresh, and site makes an HTML report about the project. To start with, compile, test and package are all you need.
Run the app with exec:java
No phase above printed the invoice. For that there's the exec plugin, and pom.xml must name the class to start:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.6.4</version>
<configuration>
<mainClass>org.studyeasy.s02l03.InvoiceApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
In the Maven panel under Plugins, double-click exec:java. It starts main with the classes that compile built, and the same invoice prints between Maven's own lines, total 308.49.
Common mistakes
- No
mainClassin the exec plugin. Maven doesn't know whichmainto start. Check this first whenexec:javafails. - Expecting
packageto skip tests. It runs them first; a failing test means no jar. - Using a test-scoped library in main code. JUnit with
scopetestisn't available tosrc/main/java.
Recap
| Piece | Remember |
|---|---|
| Running anything | Find main, click the green arrow |
pom.xml | Names the project, sets Java 25, lists dependencies |
| Dependencies | From Maven Central once, cached in ~/.m2 |
| Lifecycle | Each phase runs the ones before it |
exec:java | Runs the mainClass named in the pom |
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
