0
0
Jenkinsdevops~5 mins

Build tools (Maven, Gradle, npm) in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Build tools help automate the process of compiling code, managing dependencies, and packaging applications. They save time and reduce errors by running these tasks automatically instead of doing them by hand.
When you want to compile Java code and manage libraries easily using Maven.
When you need a flexible build system that can handle complex tasks with Gradle.
When you want to manage JavaScript project dependencies and run scripts using npm.
When you want to automate builds in Jenkins using common build tools.
When you want to ensure consistent builds across different environments.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build with Maven') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Build with Gradle') {
            steps {
                sh './gradlew build'
            }
        }
        stage('Build with npm') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
    }
}

This Jenkinsfile defines a pipeline with three stages to build projects using Maven, Gradle, and npm.

  • agent any: Runs the pipeline on any available Jenkins agent.
  • stage('Build with Maven'): Runs the Maven clean and package commands to compile and package Java code.
  • stage('Build with Gradle'): Runs the Gradle build command using the Gradle wrapper.
  • stage('Build with npm'): Installs npm dependencies and runs the build script defined in package.json.
Commands
This command cleans previous build files and packages the Java project using Maven.
Terminal
mvn clean package
Expected OutputExpected
[INFO] Scanning for projects... [INFO] Building my-app 1.0 [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ my-app --- [INFO] Deleting /workspace/my-app/target [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ my-app --- [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ my-app --- [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ my-app --- [INFO] Building jar: /workspace/my-app/target/my-app-1.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.123 s [INFO] Finished at: 2024-06-01T12:00:00Z [INFO] ------------------------------------------------------------------------
This command runs the Gradle build using the Gradle wrapper script to compile and package the project.
Terminal
./gradlew build
Expected OutputExpected
> Task :compileJava > Task :processResources > Task :classes > Task :jar > Task :assemble > Task :build BUILD SUCCESSFUL in 4s 5 actionable tasks: 5 executed
This command installs all the dependencies listed in the package.json file for a Node.js project.
Terminal
npm install
Expected OutputExpected
+ lodash@4.17.21 added 50 packages from 37 contributors and audited 50 packages in 2.5s found 0 vulnerabilities
This command runs the build script defined in the package.json file, usually to compile or bundle JavaScript code.
Terminal
npm run build
Expected OutputExpected
> my-app@1.0.0 build /workspace/my-app > webpack --mode production Hash: abc123def456 Version: webpack 5.88.2 Built at: 2024-06-01 12:00:00 Asset Size Chunks Chunk Names main.js 1.2 MiB main [emitted] main Entrypoint main = main.js [./src/index.js] 1.5 KiB {main} [built] Build completed successfully.
Key Concept

If you remember nothing else from this pattern, remember: build tools automate compiling and packaging so you don't have to do it manually.

Common Mistakes
Running 'mvn package' without cleaning first
Old build files may cause conflicts or outdated artifacts to remain.
Use 'mvn clean package' to remove old files before building.
Running 'npm run build' before 'npm install'
Build scripts may fail because dependencies are missing.
Always run 'npm install' first to install dependencies.
Running './gradlew build' without executable permission
The command will fail with a permission denied error.
Run 'chmod +x ./gradlew' once to make the wrapper executable.
Summary
Use 'mvn clean package' to compile and package Java projects with Maven.
Use './gradlew build' to build projects with Gradle using the wrapper.
Use 'npm install' to install Node.js dependencies and 'npm run build' to build the project.
Automate these commands in Jenkins pipelines using a Jenkinsfile for consistent builds.