Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

How do you build and run a Java project with Maven?

BeginnerHands-onJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

Run mvn clean package to compile the code, run the tests and create a JAR (or WAR) in the target/ folder. Then run the JAR with java -jar target/<name>.jar.

Step by step

# 1. Check Java and Maven are installed
java -version
mvn -version

# 2. Build: compile + test + package
mvn clean package

# 3. See the output
ls target/
# orders-service-1.0.0.jar

# 4. Run the application
java -jar target/orders-service-1.0.0.jar
# ... Tomcat started on port 8080

What mvn clean package actually does

Maven runs a lifecycle — a fixed list of phases in order. When you ask for one phase, it runs every phase before it too.

Phase What happens
clean Deletes the old target/ folder, so you start fresh
validate Checks the pom.xml is correct
compile Turns .java files into .class files
test Runs unit tests (JUnit) — the build fails if any test fails
package Bundles everything into a JAR or WAR
verify Runs extra checks such as integration tests
install Copies the JAR into your local ~/.m2 repository
deploy Uploads the JAR to a remote repository such as Nexus

Useful options in pipelines

mvn clean package -DskipTests      # skip running tests (use carefully)
mvn -B clean verify                # -B = batch mode: cleaner logs in CI
mvn dependency:tree                # see every dependency and its version
java -Xmx512m -jar app.jar         # limit memory to 512 MB
java -jar app.jar --server.port=9090   # override a Spring Boot setting

Example — in a Dockerfile (multi-stage)

# Stage 1: build
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn -B dependency:go-offline        # cache dependencies in a layer
COPY src ./src
RUN mvn -B clean package -DskipTests

# Stage 2: run (small image, no Maven inside)
FROM eclipse-temurin:21-jre
COPY --from=build /app/target/*.jar /app/app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

What to say in the interview

"We build Java services with mvn clean package, which runs the Maven lifecycle — compile, test and package — and produces the JAR in target/. We run it with java -jar. In the pipeline we use batch mode, cache the .m2 repository to speed things up, and in Docker we use a multi-stage build so the final image only has the JRE and the JAR, not Maven and the source code."

Follow-up questions you may get

  • What's the difference between package and install? package creates the JAR in target/; install also copies it into the local ~/.m2 repository so other local projects can use it.
  • How do you speed up Maven builds in CI? Cache ~/.m2, use dependency:go-offline as a separate Docker layer, and run with -T 1C for parallel module builds.
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)