Skip to Content
Course content

116: Building Projects with Maven

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a pattern with developers moving from basic Java to professional project structures. Most of them start by thinking that Maven is essentially just a fancy downloader for JAR files. They treat the pom.xml like a shopping list: "I need Gson for JSON, and I need JUnit for testing, so I'll just list them here and Maven will grab them for me."

The "JAR Downloader" Trap

If you think Maven is just about dependency management, you're only using about 30% of its power. When I first started, I used to manually create a /lib folder in my projects and dump JARs in there. When I moved to Maven, I thought, "Great, now I don't have to manually download files from a website." But that mindset leads to a mess. You end up ignoring the build lifecycle and treating your IDE as the primary way to compile code, which means the moment you hand your code to a teammate or a CI/CD pipeline, it breaks because "it worked on my machine."

The real problem with the "downloader" mindset is that it ignores transitive dependencies. Imagine you add a library for handling AWS S3 uploads. That library itself depends on three other libraries, which in turn depend on others. If you were doing this manually, you'd be hunting for ten different JARs. Maven handles this, yes, but the goal isn't just getting the files—it's ensuring that the entire environment is reproducible and standardized.

Standardizing the Lifecycle with the POM

Maven is actually a project management tool based on a "Project Object Model" (POM). The most important thing to understand is that Maven is opinionated. It doesn't want you to decide where your source code goes or how your tests are run; it has already decided the best way to do that. This is called Convention over Configuration.

In a Maven project, you don't just put files anywhere. You follow this structure:

  • src/main/java: Your actual application code.
  • src/main/resources: Configuration files, XML, or properties.
  • src/test/java: Your JUnit tests.

Because of this structure, Maven provides a standardized Build Lifecycle. I rarely use the "Run" button in my IDE for final checks; instead, I use the command line. When you run mvn package, Maven doesn't just "zip things up." It triggers a sequence of phases: it validates the project, compiles the source code, runs the tests, and only then packages the compiled code into a JAR. If a single test fails, the build fails. This is the "safety net" that professional software engineering relies on.

Here is a concrete example of a pom.xml for a project that parses weather data using the Gson library. Notice how we don't just list the library, but we define the project identity (groupId, artifactId, version) so that other projects could one day depend on this one.

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.weatherapp</groupId>
    <artifactId>weather-parser</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

One detail I want you to notice: the <scope>test</scope> tag. This tells Maven that JUnit is only needed to run tests. When Maven packages the final JAR for production, it won't include the testing library. This keeps your final deployment lean—something you'd never get if you were just dumping JARs into a folder.




📋 Practical Task

Build a JSON-based User Profile Validator

Your goal is to move away from manual classpath management and build a project using the Maven lifecycle. You will create a small utility that validates if a JSON user profile contains a required "email" field.

Requirements:

  1. Project Setup: Initialize a Maven project with the groupId com.validator and artifactId user-checker.
  2. Dependency: Add the com.google.code.gson dependency (version 2.10.1) to your pom.xml.
  3. Implementation:
    • Create a class UserProfile in src/main/java.
    • Create a UserValidator class that takes a JSON string, uses Gson to parse it into a UserProfile object, and returns true if the email field is not null.
  4. Testing: Create a test class in src/test/java using JUnit 5 that tests both a valid JSON string and an invalid one (missing email).
  5. The Build: Run the command mvn clean package from your terminal.

Success Criteria: The build must report BUILD SUCCESS, and you should find a user-checker-1.0-SNAPSHOT.jar file inside the newly created target folder.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.