{"id":1135,"date":"2023-08-31T10:29:12","date_gmt":"2023-08-31T04:59:12","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=1135"},"modified":"2024-04-02T09:06:21","modified_gmt":"2024-04-02T03:36:21","slug":"jenkins-maven-pipeline","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=1135","title":{"rendered":"Jenkins Maven pipeline"},"content":{"rendered":"<p>In this session we will cover two labs:<\/p>\n<p>Youtube Link: <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.youtube.com\/watch?v=4AFq3L2kbEs&amp;list=PLwO9EbabNLzsaHgP1dUCkdLg2CxZoPXGR&amp;index=12\">Navigation for labs<\/a><\/span><\/p>\n<p><strong>Lab1: pipeline for java project<\/strong><\/p>\n<p>Create a pipeline job: New item&#8211;&gt; name the project &#8220;mavenproject&#8221;&#8211;&gt;pipeline &#8211;&gt;ok<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins18.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1136\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins18-300x159.png\" alt=\"\" width=\"300\" height=\"159\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins18-300x159.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins18-700x370.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins18-768x406.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins18.png 1130w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Install maven build agent on Jenkins master server [use putty]<\/p>\n<pre>apt install maven -y<\/pre>\n<p>In the pipeline script block, copy and paste below code<\/p>\n<pre>pipeline {\r\nagent any\r\nstages {\r\n  stage('SCM code') {\r\n   steps {\r\n    git 'https:\/\/github.com\/hellokaton\/java11-examples.git'\r\n   }\r\n  }\r\n stage('Build') {\r\n  steps {\r\n   sh 'mvn clean package'\r\n  }\r\n }\r\n stage('Publish') {\r\n  steps {\r\n  \/\/ Add steps to publish artifacts or deploy the application\r\n  \/\/ For example, you can use the 'archiveArtifacts' step to archive built artifacts\r\n  archiveArtifacts 'target\/*.jar'\r\n     }\r\n  }\r\n}\r\n}<\/pre>\n<p>Break down the stages and steps in the pipeline:<\/p>\n<ol>\n<li><strong>SCM code Stage:<\/strong> This stage is responsible for checking out the source code from a Git repository.\n<ul>\n<li>It uses the <code>git<\/code> step to clone the repository from the provided URL.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Build Stage:<\/strong> In this stage, the code is built using Maven.\n<ul>\n<li>It uses the <code>sh<\/code> step to execute the Maven command <code>mvn clean package<\/code>, which cleans the project, compiles the code, runs tests, and packages the application into a <code>.jar<\/code> file.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Publish Stage:<\/strong> This stage handles the publishing or archiving of the built artifacts.\n<ul>\n<li>It uses the <code>archiveArtifacts<\/code> step to archive the <code>.war<\/code> files located in the <code>target<\/code> directory. Archiving artifacts is important in case you want to keep track of built binaries for future reference or deployment.<\/li>\n<\/ul>\n<\/li>\n<li>Click on Build now and check the build history<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins19.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1137\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins19-300x170.png\" alt=\"\" width=\"300\" height=\"170\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins19-300x170.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins19-700x397.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins19-768x435.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins19.png 1055w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Lab2:<\/strong><\/p>\n<pre>pipeline {\r\nagent any\r\nparameters {\r\nchoice(\r\nname: 'ENVIRONMENT',\r\nchoices: ['dev', 'qa', 'prod'],\r\ndescription: 'Select the deployment environment'\r\n)\r\n}\r\nstages {\r\nstage('SCM code') {\r\nsteps {\r\ngit 'https:\/\/github.com\/hellokaton\/java11-examples.git'\r\n}\r\n}\r\nstage('Build') {\r\nsteps {\r\nsh 'mvn clean package'\r\n}\r\n}\r\nstage('Publish') {\r\nwhen {\r\nexpression { params.ENVIRONMENT == 'prod' }\r\n}\r\nsteps {\r\n\/\/ Add steps to publish artifacts or deploy the application for 'prod'\r\n\/\/ For example, you can use the 'archiveArtifacts' step to archive built artifacts\r\narchiveArtifacts 'target\/*.jar'\r\n}\r\n}\r\n}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Lab 3 : Priority<\/strong><\/p>\n<pre>pipeline {\r\nagent any\r\nparameters {\r\nchoice(\r\nname: 'ENVIRONMENT',\r\nchoices: ['dev', 'qa', 'prod'],\r\ndescription: 'Select the deployment environment'\r\n)\r\nchoice(\r\nname: 'PRIORITY',\r\nchoices: ['high', 'low', 'middle'],\r\ndescription: 'Select the priority'\r\n)\r\n}\r\nstages {\r\nstage('SCM code') {\r\nsteps {\r\ngit 'https:\/\/github.com\/hellokaton\/java11-examples.git'\r\n}\r\n}\r\nstage('Build') {\r\nsteps {\r\nsh 'mvn clean package'\r\n}\r\n}\r\nstage('Publish High Priority') {\r\nwhen {\r\nexpression { params.ENVIRONMENT == 'prod' &amp;&amp; params.PRIORITY == 'high' }\r\n}\r\nsteps {\r\n\/\/ Add steps to publish high-priority artifacts or deploy the application for 'prod'\r\n\/\/ For example, you can use the 'archiveArtifacts' step to archive built artifacts\r\narchiveArtifacts 'target\/*.jar'\r\n}\r\n}\r\nstage('Publish Low Priority') {\r\nwhen {\r\nexpression { params.ENVIRONMENT == 'prod' &amp;&amp; params.PRIORITY == 'low' }\r\n}\r\nsteps {\r\n\/\/ Add steps to publish low-priority artifacts or deploy the application for 'prod'\r\n\/\/ For example, you can use the 'archiveArtifacts' step to archive built artifacts\r\narchiveArtifacts 'target\/*.jar'\r\n}\r\n}\r\nstage('Publish Middle Priority') {\r\nwhen {\r\nexpression { params.ENVIRONMENT == 'prod' &amp;&amp; params.PRIORITY == 'middle' }\r\n}\r\nsteps {\r\n\/\/ Add steps to publish middle-priority artifacts or deploy the application for 'prod'\r\n\/\/ For example, you can use the 'archiveArtifacts' step to archive built artifacts\r\narchiveArtifacts 'target\/*.jar'\r\n}\r\n}\r\n}\r\n}<\/pre>\n<p><strong>Lab4 : Same Jenkins project we will build using maven plugin<\/strong><\/p>\n<ol>\n<li>Go to Dashboard-&gt;Manage Jenkins-&gt;Manage Plugins<\/li>\n<\/ol>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins20.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1138\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins20-300x173.png\" alt=\"\" width=\"300\" height=\"173\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins20-300x173.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins20-700x404.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins20-768x443.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins20.png 1037w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>2. Available plugin&#8211;&gt;search for maven&#8211;&gt;check mark maven integration plugin&#8211;&gt;Install without restart<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1140\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins21-300x177.png\" alt=\"\" width=\"300\" height=\"177\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins21-300x177.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins21-700x414.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins21-768x454.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins21.png 1012w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>3. Set the java home and maven home<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Run the command on ubuntu master from putty<\/li>\n<li>\n<pre>mvn --version<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Jenkins22-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1142\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Jenkins22-1-300x57.png\" alt=\"\" width=\"300\" height=\"57\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Jenkins22-1-300x57.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Jenkins22-1-700x134.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Jenkins22-1-768x147.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/Jenkins22-1.png 1285w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/li>\n<li>Note down the mvn home and java home<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>mvn home:  \/usr\/share\/maven\r\n\r\njava home:  \/usr\/lib\/jvm\/java-17-openjdk-amd64<\/pre>\n<p>3. Setup javahome and mvn home on jenkins dashboard<\/p>\n<p>Go to Dashboard-&gt;Manage Jenkins&#8211;&gt; System configuration&#8211;&gt;Global tools configuration<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins23.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1144\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins23-300x177.png\" alt=\"\" width=\"300\" height=\"177\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins23-300x177.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins23-700x414.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins23-768x454.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins23.png 1012w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>4. JDK&#8211;&gt;Add Jdk&#8211;&gt;name as Java home&#8211;&gt;path &#8220;\/usr\/lib\/jvm\/java-17-openjdk-amd64&#8221;<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1145\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins24-300x158.png\" alt=\"\" width=\"300\" height=\"158\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins24-300x158.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins24-700x368.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins24-768x404.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins24.png 1137w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>5. Same page only Under global tool configuration&#8211;&gt; Setup maven home<\/p>\n<p>maven&#8211;&gt;add maven&#8211;&gt;uncheck install automatically&#8211;&gt;maven name &#8220;mvnho&#8221;&#8211;&gt;maven_home &#8211;&gt;\/usr\/share\/maven and save it<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins25.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1146\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins25-300x153.png\" alt=\"\" width=\"300\" height=\"153\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins25-300x153.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins25-700x357.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins25-768x392.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins25.png 1173w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>6. Create a New Item&#8211;&gt;name &#8220;mvnnewproj&#8221;&#8211;&gt;maven project&#8211;&gt;ok<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins27.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1148\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins27-300x176.png\" alt=\"\" width=\"300\" height=\"176\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins27-300x176.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins27-700x411.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins27-768x451.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins27.png 1019w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>7. under source code management add git repo and save<\/p>\n<p>https:\/\/github.com\/hellokaton\/java11-examples.git<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins26.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1149\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins26-300x177.png\" alt=\"\" width=\"300\" height=\"177\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins26-300x177.png 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins26-700x414.png 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins26-768x454.png 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/jenkins26.png 1012w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>8. Build now&#8211;&gt;check build history&#8211;&gt;check console output<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Observe difference between lab1 and lab2<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this session we will cover two labs: Youtube Link: Navigation for labs Lab1: pipeline for java project Create a pipeline job: New item&#8211;&gt; name the project &#8220;mavenproject&#8221;&#8211;&gt;pipeline &#8211;&gt;ok Install maven build agent on Jenkins master server [use putty] apt install maven -y In the pipeline script block, copy and paste below code pipeline { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-1135","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1135"}],"version-history":[{"count":8,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1135\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1135\/revisions\/1409"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}