构建工具配置 #

Jenkins支持多种构建工具,本节将详细介绍如何配置和使用Maven、Gradle、npm等常用构建工具。

全局工具配置 #

访问配置 #

  1. 进入 Manage Jenkins
  2. 点击 Global Tool Configuration

JDK配置 #

自动安装 #

text
JDK:
  Name: JDK 11
  ☑ Install automatically
  Install from java.sun.com: JDK 11.0.x

手动安装 #

text
JDK:
  Name: JDK 11
  JAVA_HOME: /usr/lib/jvm/java-11-openjdk

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    tools {
        jdk 'JDK 11'
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'java -version'
            }
        }
    }
}

多JDK版本 #

groovy
pipeline {
    agent any
    
    stages {
        stage('Build with JDK 8') {
            tools {
                jdk 'JDK 8'
            }
            steps {
                sh 'java -version'
            }
        }
        
        stage('Build with JDK 11') {
            tools {
                jdk 'JDK 11'
            }
            steps {
                sh 'java -version'
            }
        }
    }
}

Maven配置 #

全局配置 #

text
Maven:
  Name: Maven 3.8
  ☑ Install automatically
  Version: 3.8.6

配置settings.xml #

text
Maven Configuration:
  Default settings provider: File on master
  File path: /opt/maven/settings.xml
  
  Default global settings provider: File on master
  File path: /opt/maven/global-settings.xml

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    tools {
        maven 'Maven 3.8'
        jdk 'JDK 11'
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}

withMaven包装器 #

groovy
steps {
    withMaven(maven: 'Maven 3.8', jdk: 'JDK 11') {
        sh 'mvn clean package'
    }
}

配置选项 #

groovy
steps {
    withMaven(
        maven: 'Maven 3.8',
        jdk: 'JDK 11',
        mavenSettingsConfig: 'maven-settings',
        mavenLocalRepo: '.repository',
        options: [
            artifactsPublisher(disabled: false),
            junitPublisher(disabled: false, ignoreAttachments: false),
            findbugsPublisher(disabled: true),
            tasksPublisher(disabled: true)
        ]
    ) {
        sh 'mvn clean package'
    }
}

Maven多模块项目 #

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

Gradle配置 #

全局配置 #

text
Gradle:
  Name: Gradle 7.6
  ☑ Install automatically
  Version: 7.6

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    tools {
        gradle 'Gradle 7.6'
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'gradle build'
            }
        }
    }
}

withGradle包装器 #

groovy
steps {
    withGradle {
        sh 'gradle build'
    }
}

Gradle Wrapper #

groovy
pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                sh './gradlew build'
            }
        }
    }
}

Gradle多项目 #

groovy
pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                sh './gradlew clean build'
            }
        }
        
        stage('Test') {
            steps {
                sh './gradlew test'
            }
            post {
                always {
                    junit '**/build/test-results/**/*.xml'
                }
            }
        }
        
        stage('Package') {
            steps {
                sh './gradlew jar'
            }
            post {
                success {
                    archiveArtifacts artifacts: '**/build/libs/*.jar', fingerprint: true
                }
            }
        }
    }
}

npm配置 #

Node.js配置 #

text
NodeJS:
  Name: NodeJS 18
  ☑ Install automatically
  Version: 18.x

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    tools {
        nodejs 'NodeJS 18'
    }
    
    stages {
        stage('Install') {
            steps {
                sh 'npm ci'
            }
        }
        
        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }
        
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
    }
}

withNode包装器 #

groovy
steps {
    nodejs(nodeJSInstallationName: 'NodeJS 18') {
        sh 'npm install'
        sh 'npm run build'
    }
}

npm私有仓库 #

groovy
steps {
    withCredentials([usernamePassword(
        credentialsId: 'npm-registry',
        usernameVariable: 'NPM_USER',
        passwordVariable: 'NPM_PASS'
    )]) {
        sh """
            npm config set registry https://registry.example.com
            npm config set //registry.example.com/:_authToken ${NPM_PASS}
            npm ci
        """
    }
}

.npmrc配置 #

groovy
steps {
    configFileProvider([
        configFile(fileId: 'npmrc', variable: 'NPM_CONFIG_USERCONFIG')
    ]) {
        sh 'npm ci'
    }
}

Yarn配置 #

groovy
pipeline {
    agent any
    
    tools {
        nodejs 'NodeJS 18'
    }
    
    stages {
        stage('Install') {
            steps {
                sh 'yarn install --frozen-lockfile'
            }
        }
        
        stage('Build') {
            steps {
                sh 'yarn build'
            }
        }
        
        stage('Test') {
            steps {
                sh 'yarn test'
            }
        }
    }
}

Python配置 #

Python工具 #

text
Python:
  Name: Python 3.10
  ☑ Install automatically

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    stages {
        stage('Setup') {
            steps {
                sh 'python3 -m venv venv'
                sh '. venv/bin/activate && pip install -r requirements.txt'
            }
        }
        
        stage('Test') {
            steps {
                sh '. venv/bin/activate && pytest'
            }
        }
    }
}

withPython包装器 #

groovy
steps {
    withPythonEnv('Python 3.10') {
        sh 'pip install -r requirements.txt'
        sh 'pytest'
    }
}

Docker配置 #

Docker工具 #

text
Docker:
  Name: Docker
  Install automatically: (通常不需要,使用系统Docker)

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    environment {
        DOCKER_REGISTRY = 'registry.example.com'
    }
    
    stages {
        stage('Build Image') {
            steps {
                script {
                    docker.build("${DOCKER_REGISTRY}/myapp:${BUILD_NUMBER}")
                }
            }
        }
        
        stage('Push Image') {
            steps {
                script {
                    docker.withRegistry("https://${DOCKER_REGISTRY}", 'docker-credentials') {
                        docker.image("${DOCKER_REGISTRY}/myapp:${BUILD_NUMBER}").push()
                    }
                }
            }
        }
    }
}

Docker Agent #

groovy
pipeline {
    agent {
        docker {
            image 'maven:3.8-openjdk-11'
            args '-v $HOME/.m2:/root/.m2'
        }
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }
    }
}

Go配置 #

Go工具 #

text
Go:
  Name: Go 1.20
  ☑ Install automatically
  Version: 1.20

在Pipeline中使用 #

groovy
pipeline {
    agent any
    
    tools {
        go 'Go 1.20'
    }
    
    environment {
        GOPATH = "${WORKSPACE}/go"
        PATH = "${env.GOPATH}/bin:${env.PATH}"
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'go build ./...'
            }
        }
        
        stage('Test') {
            steps {
                sh 'go test ./...'
            }
        }
    }
}

工具版本矩阵 #

groovy
pipeline {
    agent any
    
    matrix {
        axes {
            axis {
                name 'JAVA_VERSION'
                values '8', '11', '17'
            }
            axis {
                name 'MAVEN_VERSION'
                values '3.6', '3.8'
            }
        }
        
        stages {
            stage('Build') {
                tools {
                    jdk "JDK ${JAVA_VERSION}"
                    maven "Maven ${MAVEN_VERSION}"
                }
                steps {
                    sh 'mvn clean package'
                }
            }
            
            stage('Test') {
                steps {
                    sh 'mvn test'
                }
            }
        }
    }
}

完整示例 #

groovy
pipeline {
    agent any
    
    tools {
        jdk 'JDK 11'
        maven 'Maven 3.8'
        nodejs 'NodeJS 18'
    }
    
    environment {
        APP_NAME = 'myapp'
        VERSION = "${BUILD_NUMBER}"
        DOCKER_REGISTRY = 'registry.example.com'
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        stage('Backend Build') {
            steps {
                withMaven(maven: 'Maven 3.8', jdk: 'JDK 11') {
                    sh 'mvn clean package -DskipTests'
                }
            }
        }
        
        stage('Backend Test') {
            steps {
                withMaven(maven: 'Maven 3.8', jdk: 'JDK 11') {
                    sh 'mvn test'
                }
            }
            post {
                always {
                    junit '**/target/surefire-reports/*.xml'
                }
            }
        }
        
        stage('Frontend Build') {
            steps {
                dir('frontend') {
                    nodejs(nodeJSInstallationName: 'NodeJS 18') {
                        sh 'npm ci'
                        sh 'npm run build'
                    }
                }
            }
        }
        
        stage('Frontend Test') {
            steps {
                dir('frontend') {
                    nodejs(nodeJSInstallationName: 'NodeJS 18') {
                        sh 'npm test'
                    }
                }
            }
        }
        
        stage('Docker Build') {
            steps {
                script {
                    docker.withRegistry("https://${DOCKER_REGISTRY}", 'docker-credentials') {
                        def backendImage = docker.build("${DOCKER_REGISTRY}/${APP_NAME}-backend:${VERSION}", '-f backend/Dockerfile backend')
                        backendImage.push()
                        
                        def frontendImage = docker.build("${DOCKER_REGISTRY}/${APP_NAME}-frontend:${VERSION}", '-f frontend/Dockerfile frontend')
                        frontendImage.push()
                    }
                }
            }
        }
        
        stage('Deploy') {
            steps {
                withCredentials([file(credentialsId: 'kube-config', variable: 'KUBECONFIG')]) {
                    sh """
                        kubectl set image deployment/${APP_NAME}-backend \
                            ${APP_NAME}-backend=${DOCKER_REGISTRY}/${APP_NAME}-backend:${VERSION}
                        
                        kubectl set image deployment/${APP_NAME}-frontend \
                            ${APP_NAME}-frontend=${DOCKER_REGISTRY}/${APP_NAME}-frontend:${VERSION}
                    """
                }
            }
        }
    }
    
    post {
        always {
            cleanWs()
        }
    }
}

下一步学习 #

小结 #

  • 使用全局工具配置统一管理工具版本
  • 支持自动安装和手动配置
  • Pipeline中通过tools指令使用工具
  • 支持Maven、Gradle、npm等多种构建工具
  • 可以使用矩阵构建测试多版本兼容性
最后更新:2026-03-28