Skip to lesson

Create a Maven project in your IDE and from the terminal

A new student project needs a folder, a pom.xml and the right source folders. Typing them by hand is slow and easy to get wrong, so IDEs and Maven can generate the whole project in seconds.

New Maven project in IntelliJ IDEA

GroupId names who owns the project, usually your organisation's domain reversed, such as com.shoplite. With the ArtifactId and the version it becomes the project's identity in pom.xml.

IntelliJ IDEA's New Project wizard creates the folder, the pom.xml and the source folders for you. The one choice that matters is Build system: Maven; the default, IntelliJ, makes a project that only IntelliJ can build.

Example 1 · The IntelliJ wizard fields and what they become

Runs in: IntelliJ IDEA

Text
File > New > Project...   (left list: Java)Name:            hello-mavenLocation:        C:\Users\asha\projectsCreate Git repository:  (leave off for now)Build system:    Maven                 <- the important choiceJDK:             25 (or 21, 17)Add sample code: onAdvanced Settings  GroupId:       com.shoplite  ArtifactId:    hello-maven
Result (IntelliJ IDEA 2025; screens can change slightly between versions)
hello-maven/
  pom.xml          groupId com.shoplite, artifactId hello-maven, version 1.0-SNAPSHOT
  src/main/java/com/shoplite/Main.java     the sample code
  src/main/resources/
  src/test/java/
  .idea/           IntelliJ's own settings (not part of the build)

What this code does

  • Name and ArtifactId are usually the same. GroupId plus ArtifactId become the project's identity in pom.xml.
  • IntelliJ puts the sample Main.java in a package named after the GroupId. Move it to com.shoplite.app (right-click the package > Refactor > Rename, or create the package and drag the class) to follow this course's layout.

The ArtifactId is the project's own name in lower case with hyphens, such as hello-maven, and Maven fills in the version 1.0-SNAPSHOT, which the pom lesson explains. After Create, IntelliJ reads the new pom.xml and downloads what it needs: wait until the progress bar at the bottom finishes before you run anything.

Order of execution

Create the project in IntelliJ

Put the wizard steps in the order you do them.

VS Code and Eclipse can also create Maven projects, but their screens differ. If you use them, create the project with the terminal command in the next section and open the folder; every IDE understands a pom.xml.

mvn archetype:generate from the terminal

An archetype is a project template. The command mvn archetype:generate copies it into a new folder and fills in your groupId, artifactId and package.

maven-archetype-quickstart is the standard template for a plain Java app with one class and one JUnit test. One command creates the whole hello-maven project.

Example 2 · Create hello-maven from the terminal

Runs in: a terminal in your projects folder

Terminal
mvn archetype:generate "-DgroupId=com.shoplite" "-DartifactId=hello-maven" "-Dpackage=com.shoplite.app" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.5" "-DinteractiveMode=false"
Sample output
$ mvn archetype:generate "-DgroupId=com.shoplite" "-DartifactId=hello-maven" "-Dpackage=com.shoplite.app" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.5" "-DinteractiveMode=false"
[INFO] Scanning for projects...
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] >>> archetype:3.4.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] <<< archetype:3.4.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] --- archetype:3.4.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.5
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.shoplite
[INFO] Parameter: artifactId, Value: hello-maven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.shoplite.app
[INFO] Parameter: packageInPathFormat, Value: com/shoplite/app
[INFO] Parameter: junitVersion, Value: 5.11.0
[INFO] Parameter: package, Value: com.shoplite.app
[INFO] Parameter: groupId, Value: com.shoplite
[INFO] Parameter: artifactId, Value: hello-maven
[INFO] Parameter: javaCompilerVersion, Value: 17
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[WARNING] Don't override file C:\Users\asha\projects\hello-maven\src\main\java\com\shoplite\app
[WARNING] Don't override file C:\Users\asha\projects\hello-maven\src\test\java\com\shoplite\app
[WARNING] CP Don't override file C:\Users\asha\projects\hello-maven\.mvn
[INFO] Project created from Archetype in dir: C:\Users\asha\projects\hello-maven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.747 s
[INFO] Finished at: 2026-10-16T08:17:48+05:30
[INFO] ------------------------------------------------------------------------

What this code does

  • -Dpackage=com.shoplite.app puts the generated class in that package; without it, the package would be the groupId. -DarchetypeVersion=1.5 picks the template that targets Java 17 and JUnit 5.
  • The "Don't override file" warnings come from this template version and are harmless. The line that matters is "Project created from Archetype in dir".

Each setting is passed as -Dname=value (D stands for define), and -DinteractiveMode=false stops Maven from asking questions one by one. Run the command in the folder above where the project should go, because it creates hello-maven there. The first run downloads the archetype plugin and its template, so it takes a minute.

In Windows PowerShell, put each -D... option in double quotes, as in the example. Without quotes PowerShell splits -DgroupId=com.shoplite at the dot, and Maven fails with an error such as Unknown lifecycle phase ".shoplite". The quotes do no harm in other shells.

The standard layout: src/main/java, src/main/resources, src/test/java, target

The standard directory layout is Maven's fixed set of folders: src/main/java for app code, src/main/resources for files the app reads, src/test/java for tests and target for build output.

Every Maven project has the same four places. One mvn package build touches all of them, so run it in hello-maven and then look at what the template made.

Example 3 · What the template made, then mvn package and a run

Runs in: a terminal in the hello-maven project

Terminal
cd hello-mavenmvn packagejava -cp target/classes com.shoplite.app.App
Sample output
Files created by the template:
hello-maven/
  .mvn/jvm.config
  .mvn/maven.config
  pom.xml
  src/main/java/com/shoplite/app/App.java
  src/test/java/com/shoplite/app/AppTest.java

$ mvn package
[INFO] Scanning for projects...
[INFO] ----------------------< com.shoplite:hello-maven >----------------------
[INFO] Building hello-maven 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- resources:3.3.1:resources (default-resources) @ hello-maven ---
[INFO] skip non existing resourceDirectory C:\Users\asha\projects\hello-maven\src\main\resources
[INFO] --- compiler:3.13.0:compile (default-compile) @ hello-maven ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 1 source file with javac [debug release 17] to target\classes
[INFO] --- resources:3.3.1:testResources (default-testResources) @ hello-maven ---
[INFO] skip non existing resourceDirectory C:\Users\asha\projects\hello-maven\src\test\resources
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ hello-maven ---
[INFO] Recompiling the module because of changed dependency.
[INFO] Compiling 1 source file with javac [debug release 17] to target\test-classes
[INFO] --- surefire:3.3.0:test (default-test) @ hello-maven ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.shoplite.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.091 s -- in com.shoplite.app.AppTest
[INFO] Results:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] --- jar:3.4.2:jar (default-jar) @ hello-maven ---
[INFO] Building jar: C:\Users\asha\projects\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.042 s
[INFO] Finished at: 2026-10-16T08:18:37+05:30
[INFO] ------------------------------------------------------------------------
$ java -cp target/classes com.shoplite.app.App
Hello World!

What this code does

  • One build touches every part of the layout: src/main/java is compiled into target/classes, src/test/java into target/test-classes, the test runs, and a JAR lands in target.
  • java -cp target/classes runs the compiled class directly. The "skip non existing resourceDirectory" lines just mean there is no src/main/resources yet.

Example 4 · Mistake: code in src/com instead of src/main/java

Runs in: a terminal in a project with the wrong folder

Terminal
mvn compilejava -cp target/classes com.shoplite.app.Main
Sample output
Main.java is at src/com/shoplite/app/Main.java

$ mvn compile
[INFO] Scanning for projects...
[INFO] ----------------------< com.shoplite:hello-maven >----------------------
[INFO] Building hello-maven 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- resources:3.3.1:resources (default-resources) @ hello-maven ---
[INFO] skip non existing resourceDirectory C:\Users\asha\projects\hello-maven\src\main\resources
[INFO] --- compiler:3.13.0:compile (default-compile) @ hello-maven ---
[INFO] No sources to compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.076 s
[INFO] Finished at: 2026-10-16T08:19:27+05:30
[INFO] ------------------------------------------------------------------------
$ java -cp target/classes com.shoplite.app.Main
Error: Could not find or load main class com.shoplite.app.Main
Caused by: java.lang.ClassNotFoundException: com.shoplite.app.Main

What this code does

  • Maven only looks in src/main/java, finds nothing, prints "No sources to compile" and still reports BUILD SUCCESS. It did not create target/classes at all.
  • The real error shows up later, when java cannot find the class. Fix: move the file to src/main/java/com/shoplite/app/Main.java.

src/main/resources holds files the app reads at run time, such as .properties settings, and Maven copies them next to the compiled classes. src/test/java holds JUnit tests, which are compiled and run but never shipped. The template does not create src/main/resources: add it the first time you need it. target appears on the first build and can be deleted any time, because Maven rebuilds it.

The layout is not a suggestion. Code outside src/main/java is not compiled at all, and Maven still says BUILD SUCCESS, as Example 4 shows.

Predict the output

Wrong folder: what does Maven say?

The file is not under src/main/java. Pick what mvn compile prints.

Main.java was saved in the wrong place.

src/com/shoplite/app/Main.java
$ mvn compile

Packages that match folders: com.shoplite.app

The default package is where a class lives when its file has no package line. Avoid it: classes in named packages cannot import classes from the default package.

A Java package must match the folders under src/main/java. The class com.shoplite.app.Main lives in src/main/java/com/shoplite/app/Main.java and starts with package com.shoplite.app;. Each dot in the name is one folder.

Example 5 · src/main/java/com/shoplite/app/Main.java

Runs in: the hello-maven Maven project

Main.javaJava
package com.shoplite.app;
public class Main {    public static void main(String[] args) {        System.out.println("ShopLite starts here");        System.out.println("Class:   " + Main.class.getName());        System.out.println("Package: " + Main.class.getPackageName());    }}
Output
$ mvn -q compile
$ java -cp target/classes com.shoplite.app.Main
ShopLite starts here
Class:   com.shoplite.app.Main
Package: com.shoplite.app

What this code does

  • The package line, the folders com/shoplite/app and the full class name com.shoplite.app.Main all agree.
  • mvn -q compile (quiet) prints nothing when it works; java then runs the class by its full name from target/classes.

Example 6 · Mistake: the package line does not match the folder

Runs in: a terminal in the hello-maven project

Terminal
mvn -q compilejava -cp target/classes com.shoplite.app.Mainjava -cp target/classes com.shoplite.Main
Sample output
The file is src/main/java/com/shoplite/app/Main.java, but its first line is: package com.shoplite;

$ mvn -q compile
$ java -cp target/classes com.shoplite.app.Main
Error: Could not find or load main class com.shoplite.app.Main
Caused by: java.lang.ClassNotFoundException: com.shoplite.app.Main
$ java -cp target/classes com.shoplite.Main
ShopLite starts here
Class:   com.shoplite.Main
Package: com.shoplite

What this code does

  • The compiler follows the package line, so the class becomes com.shoplite.Main in target/classes/com/shoplite/Main.class. The name you expected does not exist.
  • IntelliJ marks such a file with a red package line. Fix the package line (or move the file) so folder and package agree.

Package names start with the groupId and add one part for each area of the app: com.shoplite.app for the entry point, and later com.shoplite.model, com.shoplite.dao and so on. Put Main in a package, never directly in src/main/java.

javac does not stop you from writing a package line that disagrees with the folder. The class is then compiled into the folder of its package line, and java cannot find it under the name you expect.

Find the bug

Find the wrong line

Each snippet breaks the package rules. Tap the wrong line, then check the fix.

The file is src/main/java/com/shoplite/app/Main.java.

Round 1 of 3

Opening an existing Maven project and reloading it after pom changes

Reload makes the IDE re-read pom.xml after it changes, so new libraries and settings reach its classpath. In IntelliJ the button is called Load Maven Changes.

To open a Maven project, open the folder that contains pom.xml, never src or a single Java file. The IDE then imports the project as Maven and downloads its libraries. To check a project you just cloned, build it in the terminal.

Terminal
git clone https://github.com/your-name/hello-maven.gitcd hello-mavenmvn compile

When pom.xml changes, because you added a library or pulled a teammate's commit, reload the Maven project. IntelliJ shows a floating Load Maven Changes button; until you click it, imports from the new library stay red even though mvn compile works in the terminal.

  • IntelliJ, Load Maven Changes: Ctrl+Shift+O on Windows, Cmd+Shift+I on macOS.
  • The terminal is the referee: if mvn compile passes, the project is fine and only the IDE needs a reload.
  • If IntelliJ opened the folder as a plain project, right-click pom.xml and choose Add as Maven Project.

Next step

Check your understanding

11 quick questions