DevOps

Build Tools

Learn build tools including Maven, Gradle, and npm. Understand dependency management, build lifecycle, and creating artifacts.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn build tools including Maven, Gradle, and npm. Understand dependency management, build lifecycle, and creating artifacts. This hands-on tutorial focuses on practical implementation of build tools concepts.

Build Tools

Build tools automate the process of compiling source code, running tests, packaging applications, and managing dependencies. They're essential in any DevOps pipeline.

Why Build Tools Matter

┌───────────────────────────────────────────────────────────────┐
│                   Build Pipeline                              │
├───────────────────────────────────────────────────────────────┤
│                                                               │
│   Source Code ──> Compile ──> Test ──> Package ──> Deploy  │
│        │            │         │         │          │        │
│        │            │         │         │          │        │
│     [Git]      [Maven]   [JUnit]   [JAR]    [Docker]        │
│     [Repo]     [Gradle]  [Jest]    [WAR]    [K8s]           │
│                [npm]     [PyTest]  [ZIP]                    │
│                                                               │
└───────────────────────────────────────────────────────────────┘

Maven (Java)

Project Structure

my-project/
├── pom.xml              # Project Object Model
├── src/
│   ├── main/
│   │   ├── java/        # Application source
│   │   └── resources/   # Configuration files
│   └── test/
│       ├── java/        # Test source
│       └── resources/   # Test resources
└── target/              # Build output

Basic pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.0.0</version>
        </dependency>
        
        <!-- Testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Maven Lifecycle Phases

┌─────────────────────────────────────────────────────────┐
│                   Maven Build Lifecycle                   │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  validate ──> compile ──> test ──> package ──> verify  │
│                                                         │
│  install ──> deploy                                     │
│                                                         │
└─────────────────────────────────────────────────────────┘

validate    - Validate project structure
compile     - Compile source code
test        - Run unit tests
package     - Create JAR/WAR file
verify      - Run integration tests
install     - Install to local repository
deploy      - Deploy to remote repository

Essential Maven Commands

# Create new project
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app

# Compile
mvn compile

# Run tests
mvn test

# Package (creates JAR/WAR)
mvn package

# Install to local repo
mvn install

# Clean build artifacts
mvn clean

# Full build
mvn clean install

# Skip tests
mvn clean install -DskipTests

# Run specific test
mvn test -Dtest=MyTestClass

# View effective POM
mvn help:effective-pom

# View dependency tree
mvn dependency:tree

# Check for updates
mvn versions:display-dependency-updates

Gradle (Java/Groovy/Kotlin)

Project Structure

my-project/
├── build.gradle         # Build configuration
├── settings.gradle      # Project settings
├── gradlew             # Gradle wrapper (Unix)
├── gradlew.bat         # Gradle wrapper (Windows)
├── gradle/
│   └── wrapper/
├── src/
│   ├── main/
│   │   ├── java/
│   │   └── resources/
│   └── test/
│       ├── java/
│       └── resources/
└── build/              # Build output

Basic build.gradle

plugins {
    id 'java'
    id 'application'
    id 'org.springframework.boot' version '3.0.0'
}

group = 'com.example'
version = '1.0.0'

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:3.0.0'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

application {
    mainClass = 'com.example.Application'
}

// Custom tasks
task hello {
    doLast {
        println 'Hello, Gradle!'
    }
}

Gradle Tasks

┌─────────────────────────────────────────────────────────┐
│                   Gradle Task Graph                       │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  assemble ──> build                                     │
│     ↑                     ↑                             │
│  check  ────> test                                      │
│     ↑                                                   │
│  classes ──> compileJava                                │
│                                                         │
└─────────────────────────────────────────────────────────┘

assemble    - Create all outputs
build       - Assemble and test
check       - Run all checks
test        - Run tests
classes     - Compile classes
compileJava - Compile Java source
jar         - Create JAR file

Essential Gradle Commands

# Wrapper (recommended)
./gradlew build

# Create wrapper
gradle wrapper

# Build
./gradlew build

# Compile only
./gradlew compileJava

# Run tests
./gradlew test

# Create JAR
./gradlew jar

# Clean build
./gradlew clean build

# Run application
./gradlew run

# View tasks
./gradlew tasks
./gradlew tasks --all

# View dependencies
./gradlew dependencies

# Continuous build
./gradlew build --continuous

# Skip tests
./gradlew build -x test

npm (Node.js)

Project Structure

my-project/
├── package.json        # Project configuration
├── package-lock.json   # Locked dependencies
├── node_modules/       # Installed packages
├── src/               # Source code
└── dist/              # Build output

Basic package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "My Node.js application",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "build": "webpack --mode=production",
    "test": "jest",
    "test:watch": "jest --watch",
    "lint": "eslint src/",
    "lint:fix": "eslint src/ --fix"
  },
  "dependencies": {
    "express": "^4.18.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "jest": "^29.0.0",
    "nodemon": "^2.0.20",
    "eslint": "^8.0.0",
    "webpack": "^5.0.0"
  },
  "engines": {
    "node": ">=16.0.0"
  }
}

npm Commands

# Initialize project
npm init
npm init -y  # Accept defaults

# Install dependencies
npm install
npm i

# Install package
npm install express
npm install express --save-prod
npm install jest --save-dev
npm i lodash@4.17.21

# Global install
npm install -g typescript

# Update packages
npm update
npm update express

# Run scripts
npm start
npm run dev
npm test
npm run build

# List packages
npm list
npm list --depth=0

# Audit security
npm audit
npm audit fix

# Clean cache
npm cache clean --force

# View package info
npm view express
npm view express versions

# Remove package
npm uninstall express
npm rm express

npx - Execute Packages

# Run package without installing
npx create-react-app my-app
npx ts-node script.ts

# Execute local binary
npx jest
npx eslint src/

Build Tool Comparison

FeatureMavenGradlenpm
LanguageJava (XML)Groovy/KotlinJavaScript/JSON
Build SpeedGoodBetter (incremental)Good
ConfigurationDeclarative (XML)ProgrammaticJSON-based
Learning CurveMediumMediumLow
Best ForJava projectsMulti-language, complex buildsNode.js projects

CI/CD Integration

Maven in Jenkins Pipeline

pipeline {
    agent any
    
    tools {
        maven 'Maven-3.9'
        jdk 'JDK-17'
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean compile'
            }
        }
        
        stage('Test') {
            steps {
                sh 'mvn test'
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                }
            }
        }
        
        stage('Package') {
            steps {
                sh 'mvn package -DskipTests'
                archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
            }
        }
    }
}

Docker Integration

# Maven Build Stage
FROM maven:3.9-eclipse-temurin-17-alpine AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests

# Runtime Stage
FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

Quiz

Quiz

Question 1 of 5

In Maven, what does 'mvn clean install' do?

Only compiles the code
Removes build artifacts, compiles, tests, packages, and installs to local repository
Deploys to remote repository only
Cleans the source code

Next Steps

Now let's explore artifact management—storing and versioning your build outputs.