DevSecOps - Kubernetes DevOps & Security

DevOps Pipeline

Maven Basics

In this guide, you’ll learn what Apache Maven is, why it’s essential for Java project automation, and how to leverage its lifecycle and directory conventions to streamline your build, test, and deployment processes.

What Is Maven?

Apache Maven is a powerful build automation and project management tool based on the Project Object Model (POM). By defining dependencies and build settings in a pom.xml file, Maven handles:

  • Downloading and managing third-party libraries
  • Compiling Java source code
  • Running unit tests
  • Packaging artifacts (JAR, WAR)
  • Deploying to local or remote repositories

Note

Your pom.xml sits at the root of your project. It controls everything from dependency versions to plugin executions.

Standard Project Structure

Maven enforces a conventional directory layout to keep builds predictable:

my-app
├── src
│   ├── main
│   │   ├── java            # Application source code
│   │   └── resources       # Configuration files & properties
│   └── test
│       ├── java            # Unit test source code
│       └── resources       # Test-specific resources
└── target                  # Compiled classes & packaged artifacts

Core Lifecycle Phases

Maven executes build steps in a predefined sequence of lifecycle phases. Common phases you’ll use daily:

PhaseDescriptionOutcome
validateEnsure project is correct and all info is availableChecks pom.xml validity
compileCompile application source codeGenerates .class files in target/classes
testRun unit testsExecutes tests in src/test/java
packageBundle compiled code into a JAR/WARProduces artifact in target/
installInstall package to local repositoryInstalls to ~/.m2/repository
deployCopy final artifacts to remote repositoriesPublishes for sharing across teams

The image provides an overview of Maven basics, explaining its role in automating Java projects and detailing phases like validate, compile, test, package, install, and deploy. It also lists default directory paths for source code, resources, tests, and compiled outputs.

Running Tests

To compile your code and run all unit tests:

mvn test

This executes the test phase, compiling classes and running tests under src/test/java.

Packaging Artifacts

When you’re ready to create your deployable artifact:

mvn package

Maven runs up to the package phase, producing a JAR or WAR (depending on your packaging setting) in the target directory.

Warning

Ensure your pom.xml <packaging> element matches your intended artifact type (jar, war, etc.) to avoid build failures.

Integrating Maven with Jenkins

Automate your CI/CD pipeline by invoking Maven goals in a Jenkinsfile:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'mvn clean compile'
      }
    }
    stage('Test') {
      steps {
        sh 'mvn test'
      }
    }
    stage('Package') {
      steps {
        sh 'mvn package'
      }
    }
  }
  post {
    always {
      junit 'target/surefire-reports/*.xml'
    }
  }
}

This pipeline runs through the compile, test, and package phases and archives test results automatically.

Watch Video

Watch video content

Previous
Demo Running Microservices Local Machine