Maven Example |Video upload date:  · Duration: PT8M6S  · Language: EN

Practical Maven Example guide for creating a Java project managing dependencies and running builds with clear steps and a helpful tip.

Intro with minimal drama

If you need a Java build that does not require ritual sacrifice then Maven is your friend and your mild tormentor. This short Maven tutorial shows how to create a simple project folder structure add a minimal pom.xml declare dependencies and run a build with mvn so you get a runnable artifact without crying into your terminal.

Project layout that keeps surprise bugs away

Maven expects the conventional layout so give it what it wants. Create a project folder with these paths:

  • src/main/java for production code
  • src/test/java for tests

Using this layout avoids manual classpath work and keeps your CI pipeline from inventing new ways to fail.

Minimal pom.xml you can copy and not feel guilty about

Put a minimal pom at the project root. The pom tells Maven the group id artifact id and version and controls dependency resolution and the build lifecycle. Here is a tiny example you can paste and adjust.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myapp</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Add a dependency and let Maven fetch the drama

To add a library declare its groupId artifactId and version inside the dependencies block. Maven will fetch the jar from a remote repository and store it in your local cache so your project can compile against it. Transitive dependencies will also show up whether you meant them or not.

Example dependency snippet

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

Build with mvn and get the artifact

Run the build with the usual command.

mvn clean package

That runs the default lifecycle phases including compile test and package and puts the output in the target folder. If you need a fat jar use a plugin such as the Maven shade plugin or the assembly plugin to bundle dependencies into a runnable jar.

Run the produced jar

From the project root run the jar like this.

java -jar target/myapp-1.0-SNAPSHOT.jar

If your jar is not runnable then double check your plugin config or your main class because Java is not psychic.

Quick tips for CI and dependency sanity

  • Use scopes to keep test jars out of production artifacts
  • Run mvn dependency tree early to inspect transitive surprises before CI melts down
  • Cache the Maven local repository in your CI to avoid repeated downloads and to speed up builds
  • Pin versions where it matters to avoid a surprise upgrade breaking the build

Wrap up that actually helps

This tutorial covered project layout pom basics dependency declaration and the simple build and run sequence that gets a Java project from source to runnable artifact with minimal fuss. You now know where to put code how to declare dependencies how to build with mvn and how to run the jar. Go break something in CI and then fix it like a pro.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.