How to use maven package jar file in another jenkins pipeline stage?

0 votes

I'm trying to build a CI/CD pipeline using Jenkins. I am using a maven project. I would like to use the .jar file produced by mvn package in another pipeline stage. is that possible? 

pipeline {
  agent none
  stages{
    stage('Build Jar'){
        agent {
          docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
          }
        }
        steps {
            sh 'mvn package'
            stash includes: 'target/*.jar', name: 'targetfiles'
        }
    }
    stage('Deploy') {
        agent {
            node {
                label 'DockerDefault'
            }
         }

      steps {
            script{
                def image = docker.build("image-name:test", ' .')
            }
      }
    }
  }
}

This is the Dockerfile:

#install OS
FROM centos
#install java
RUN yum install -y java
#make directory structure to store temporary files
RUN mkdir -p /store
#put jar into container
ADD target/AdWordsProducer-1.0-SNAPSHOT-shaded.jar adwordsproducer.jar
#run jar
ENTRYPOINT ["java", "-jar", "/adwordsproducer.jar"]
May 2, 2018 in Jenkins by Damon Salvatore
• 5,450 points
1,820 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I think you need to pop the files from the stash here. try using unstash. The files will remain in the location where you stashed them, i.e. "target/" in your case.

stage('Deploy') {
  steps {
        script{
            unstash 'targetfiles'
            sh 'docker build image-name:test'
        }
  }
}
answered May 2, 2018 by DareDev
• 6,670 points
0 votes

You can use the same dockerfile for all stages and just install docker cmd on it and mount the docker.sock to it.

Add this to the Dockerfile:

RUN apt-get update && \
    apt-get install -y apt-transport-https ca-certificates curl software-properties-common && \
    curl -fsSL https://download.docker.com/linux/centos/gpg | apt-key add - && \
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/centos trusty stable" && \
    apt-get update && \
    apt-get install -y docker-ce

set the jenkins file in the following way

pipeline {
    agent {
       docker {
         image 'maven:3-alpine'
         args '-u root -v /root/.m2:/root/.m2 -v /var/run/docker.sock:/var/run/docker.sock'
       }
    }
    stage('Build Jar'){
        steps {
            sh 'mvn package'
            stash includes: 'target/*.jar', name: 'targetfiles'
        }
    }
    stage('Deploy') {
      steps {
            script{
                sh 'docker build image-name:test'
            }
      }
    }
}
answered Aug 2, 2018 by lina
• 8,100 points

Related Questions In Jenkins

0 votes
1 answer

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 9,330 points
1,324 views
0 votes
1 answer

How to construct Maven installation in Jenkins for each Slave node?

I’m assuming that Java and Maven are ...READ MORE

answered Aug 1, 2018 in Jenkins by Sophie may
• 9,330 points
429 views
0 votes
1 answer

How to get the Maven Version of a project in Jenkins?

Follow the below steps: 1.      Install the Groovy ...READ MORE

answered Aug 9, 2018 in Jenkins by Sophie may
• 9,330 points
244 views
0 votes
1 answer

How to rebuild an item on Jenkins pipeline

Yes you can, its pretty simple actually. Go ...READ MORE

answered Jul 10, 2018 in Jenkins by Kalgi
• 2,620 points
107 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,140 points
80 views
0 votes
1 answer

unable to compile java code in Jenkins

If the code is compiling and running ...READ MORE

answered Apr 25, 2018 in Jenkins by DareDev
• 6,670 points
113 views
0 votes
1 answer

any way to integrate Jenkins with ServiceNow?

Now, there is no official servicenow support ...READ MORE

answered Apr 10, 2018 in Jenkins by DareDev
• 6,670 points
821 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.