skip to Main Content

I have been desperately trying to figure out if it’s possible to build a mvn project on a jenkins agent with a docker build file. The error that I am getting from running the pipeline is [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile

pipeline {
    agent {
        dockerfile { 
            filename 'DockerFile.build'
        }
    }
    environment {
        JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el8_2.x86_64"
    }
    stages {
        stage('Build DockerFile') {
            steps {
                echo "building docker image"
                sh "echo 1 | alternatives --config java"
                sh "java -version"
                sh "echo ${JAVA_HOME}"

                git branch: "develop", credentialsId: 'cred_id', url: "[email protected]:repo.git"
                dir("repo"){
                    sh "mvn clean install -X"
                }
            }
        }
    }
}

DockerFile.build

FROM centos

RUN yum update -y
RUN yum install -y java-11-openjdk-devel
RUN yum install -y java-11-openjdk

RUN yum install -y maven


ENV HOME=${home}

The line about echo 1 | alternatives --config java is to switch to java 11. Any help would be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER
    docker {
                image 'maven:3-alpine'
                args '-v $HOME/.m2:/root/.m2'
            }
    

    Need to mount the .m2 dir. It's under Caching containers here: https://www.jenkins.io/doc/book/pipeline/docker/


  2. Seems like you have a docker build file that you want to build so you have a deployable image you can run. Then when you have the image you can run you actually want to pull it down and run it with your application code mounted inside.

    I answered a similar question recently here: Dockerfile can't build app with Maven – No goals specified for this build
    Perhaps this helps?

    Seems like you have an error because you’re attempting to build directly on the jenkins host rather than in the docker container with maven/java11 installed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search