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,980 points
6,747 views

2 answers to this question.

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,890 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,220 points

Related Questions In Jenkins

+5 votes
3 answers

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
• 10,550 points
47,096 views
0 votes
1 answer

How to setup which trigger email notification to email id's in Jenkins pipeline script at every stage?

Hi@Shashi, You need to install some plugins in ...READ MORE

answered Sep 18, 2020 in Jenkins by MD
• 95,180 points
1,128 views
0 votes
1 answer

How to deploy the war file in Tomcat Server with Jenkins pipeline job on an windows machine?

Use deploy to container plugin. Go to the ...READ MORE

answered Sep 30, 2020 in Jenkins by Adil Abdullah Khan
1,689 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
• 10,550 points
2,456 views
+2 votes
1 answer
+2 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
• 58,120 points
1,147 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,890 points
1,069 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,890 points
2,821 views