Skip to main content

Working with Build Tools

1. Using Maven in Pipelines

Property Value Description
maven mvn Command to execute Maven
mavenHome /opt/maven Path to Maven installation
goals clean install Maven goals to execute
settingsFile settings.xml Maven settings file path
localRepository ${HOME}/.m2/repository Local repository path

Example: Maven Pipeline Step

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                maven 'clean install'
            }
        }
    }
}

Example: Maven with Settings

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                maven 'clean package' settingsFile: 'custom-settings.xml'
            }
        }
    }
}
Note: Ensure the Maven tool is installed and configured in Jenkins before using it in pipelines.

2. Using Gradle in Pipelines

Property Values Description
gradle gradle Gradle command to execute in the pipeline
buildFile build.gradle Path to the Gradle build file
tasks build Gradle tasks to execute (space-separated)

Example: Running Gradle Build in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'gradle build'
            }
        }
    }
}

Example: Specifying Gradle Tasks

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'gradle test check'
            }
        }
    }
}
Note: Ensure Gradle is installed on the Jenkins agent or use a Docker image with Gradle pre-installed.

3. Using Ant in Pipelines

Property Values Description
ant ant Ant build tool executable to use in the pipeline
antHome /path/to/ant Path to the Ant installation directory
antOpts -Xmx512m Additional JVM options for Ant execution

Example: Running Ant Build in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                ant(
                    buildFile: 'build.xml',
                    target: 'compile'
                )
            }
        }
    }
}

Example: Using Custom Ant Home

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                withAnt(
                    antHome: '/opt/ant-1.10.12',
                    buildFile: 'build.xml'
                ) {
                    sh 'ant compile'
                }
            }
        }
    }
}
Note: The ant step requires the Ant plugin to be installed in Jenkins.
Warning: Ensure that the build.xml file is present in the workspace or checked out from SCM.

4. Using npm in Pipelines

Property Description Example
npm install Installs dependencies from package.json sh 'npm install'
npm test Runs test scripts defined in package.json sh 'npm test'
npm run build Runs build scripts defined in package.json sh 'npm run build'
npm ci Installs dependencies using clean install (faster) sh 'npm ci'

Example: Running npm install in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }
    }
}

Example: Running npm test in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Run Tests') {
            steps {
                sh 'npm test'
            }
        }
    }
}

Example: Using npm ci for faster builds

pipeline {
    agent any
    stages {
        stage('Install with CI') {
            steps {
                sh 'npm ci'
            }
        }
    }
}
Note: Ensure Node.js and npm are installed on the Jenkins agent or use a Docker image with them pre-installed.

5. Using Yarn and pnpm

Feature Description Usage in Jenkins
Yarn JavaScript package manager with fast, reliable builds Use yarn install or yarn ci in Jenkins pipeline steps
pnpm Fast, disk space efficient package manager using hardlinks Use pnpm install in Jenkins pipeline steps

Example: Using Yarn in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Install Dependencies') {
            steps {
                sh 'yarn install'
            }
        }
    }
}

Example: Using pnpm in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Install Dependencies') {
            steps {
                sh 'pnpm install'
            }
        }
    }
}

Yarn vs pnpm

  • Speed: pnpm is generally faster due to its use of hardlinks
  • Storage: pnpm uses less disk space with its symlinked dependencies
  • Compatibility: Both support Node.js and npm ecosystem packages
  • Lockfile: Yarn uses yarn.lock, pnpm uses pnpms or pnpm-lock.yaml
Note: Ensure that the Jenkins agent has yarn or pnpm installed in the environment.

6. Using MSBuild for .NET Projects

Property Value Description
msbuild.toolPath msbuild.exe Path to the MSBuild executable
msbuild.projectFile MyProject.csproj Path to the project file to build
msbuild.targets Build MSBuild targets to execute (e.g., Build, Rebuild)
msbuild.configuration Release Build configuration (e.g., Debug, Release)
msbuild.platform AnyCPU Target platform (e.g., x86, x64, AnyCPU)

Example: MSBuild Build Step in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                msbuild projectFile: 'MyProject.csproj',
                        targets: 'Build',
                        configuration: 'Release',
                        platform: 'AnyCPU'
            }
        }
    }
}
Note: Ensure that MSBuild is installed and available in the system PATH or specify the full path to msbuild.exe.
Warning: The msbuild plugin requires Jenkins to be run on a Windows agent or with a compatible MSBuild installation.

7. Using Make for C/C++ Projects

Property Value Description
makefile.path Makefile Path to the Makefile relative to workspace root
make.target all Target to execute in the Makefile
make.options -j$(nproc) Additional options passed to make command

Example: Building a C Project with Make

make -f Makefile clean
make -f Makefile all
Note: Ensure that make is installed on the Jenkins agent and available in the PATH.

Key Points

  • Jenkins can execute make commands to build C/C++ projects.
  • The Makefile must be present in the project root or specified via makefile.path.
  • Use -j$(nproc) to parallelize builds for better performance.

8. Configuring Build Tool Versions

Build Tool Version Property Configuration Method Notes
Maven maven.compiler.source Property in pom.xml Defines Java version for compilation
Gradle java.sourceCompatibility In build.gradle or build.gradle.kts Specifies source Java version
npm engines.node In package.json Declares required Node.js version
pip python_version In setup.py or pyproject.toml Indicates minimum Python version
Go GOPROXY Environment variable or go.mod Controls Go module proxy usage

Example: Maven Java Version Configuration

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
            </plugin>
        </plugins>
    </build>
</project>

Example: Gradle Java Version Configuration

plugins {
    id 'java'
}

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

// Or using the Kotlin DSL (build.gradle.kts)
//
// java {
//     sourceCompatibility = JavaVersion.VERSION_17
//     targetCompatibility = JavaVersion.VERSION_17
// }

Example: Node.js Version in package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "engines": {
    "node": ">=18.0.0 <20.0.0"
  },
  "scripts": {
    "build": "tsc"
  }
}
Note: Ensure that Jenkins agents have the specified versions installed to avoid build failures.

9. Using Build Wrapper Scripts

Build wrapper scripts in Jenkins are used to encapsulate build logic and provide a consistent interface for executing builds across different environments.

Script Type Description Usage Example
Shell Script Executes commands in a shell environment (Linux/macOS)
#!/bin/bash
echo "Starting build..."
make clean
make all
Batch Script Executes commands in a Windows batch environment
echo Building project...
msbuild MyProject.sln
PowerShell Script Executes PowerShell commands on Windows systems
Write-Host "Running tests..."
Invoke-Expression "npm test"

Example: Using a Shell Script Wrapper

#!/bin/bash
set -e

echo "Running pre-build tasks..."
./scripts/pre-build.sh

echo "Executing main build..."
make all

echo "Running post-build tasks..."
./scripts/post-build.sh
Note: Build wrapper scripts should be executable and placed in a location accessible to the Jenkins executor.
Warning: Ensure that wrapper scripts are properly secured and do not expose sensitive information or allow arbitrary command execution.

10. Caching Build Dependencies

Option Values Description
cache.path string Path to the cache directory
cache.key string Cache key to use for storing and retrieving cache
cache.restoreKeys string[] Alternative cache keys to try if the primary key is not found

Example: Using Cache in Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                cache(path: 'node_modules', key: 'npm-${env.BRANCH_NAME}') {
                    sh 'npm install'
                }
            }
        }
    }
}
Note: The cache step requires the Cache Plugin to be installed.

Was this article helpful?