Jenkins Tutorial – Page 4
Installation of Maven on local box
What does Maven do?
It describes how the software is build and tells about project dependies. Many Java build tools are there for example gradle but Maven is still very popular and widly used one. Moreover, Jenkins provides excellent support for Maven.
Google ‘Download Maven’. First entry is the official Apache Maven website. Go to the site and scroll down. Now click ‘Binary zip archieve’ and start downloading.
After the downloading, it will appear in the download folder. Now double click ‘.zip file’.
We need to add bin drectory of this folder to Path environment variable so that OS should know where to find Maven executable. (Note: For windows, just go to official Maven website , click ‘install’ and follow Windows Tips).
Let’s see how we are going to do this for Mac. Enter Document in directory, then enter extracted folder and after it, enter bin directory.
Write pwd to print out current dircetory. Copy and paste the path.
Lets open up ‘.bash_profile’.
Just add one more command to add Maven executable path to the path variable. Then execute the file.
Once everything is set up currectly, if we type mvn-v, it will print out the Maven version for us.
Configure Jenkins to Work with Java, Git and Maven
To set up Jenkins configuration, just go to the ‘Manage Jenkins’ option and click ‘Global Tool Configuration’ which loads Jenkins configuration for us.
Here we need to add three things so Jenkins knows where to locate those tools in the build process.
- JDK (write localJDK as name, uncheck ‘install automatically’ and fill Java home box with the path where Java is installed on the local box).
- Git (name it as localGit and in the path just add Git).
- Maven (just click on ‘Add Maven’ and name it as localMaven. Fill Maven home box with the path where Maven is installed, it does not include bin directory in the end).
Click ‘save’ icon as we are done with Jenkins configuration. ‘Apply’ button will save changes but remains on the same page but ‘Save’ button will move back to the previous page.
Creation of first Jenkins Based Maven Project
Have a look of the project we are building. It is hosted on GitHub. This is a simple project which has been adopted from another project for the purpose of this course. This project is a web application displaying just ‘Hello, World!’ on the browser.
Structure of Maven Project
Let’s quickly go through the structure of this Maven project. This project has two modules as shown.
Webapp module has web.xml deployment descriptive file which gives minimal information about how to deploy this web application.
The index.jsp file describes html page which we have seen in the browser (Hello, World!).
Server contains simple Java class and corresponding test class which are not related to business logic of this web app.
Three pom. xml files are there mentioned in green colour. Maven uses pom.xml to describe software project that has been build.
Now let’s start creating the Jenkins job. Here we are on Jenkins main page:
- Click on ‘New Items’.
- Enter item name. Let it be maven-project.
- Select ‘freestyle project’. Click ‘ok’ and move on to configuring Jenkins project.
- Add description.
- Move to next section ‘Source Code Management’. Select ‘Git’.
- Add repository URL from Maven project that is opened on the other tab and copy it after clicking on green button.
- Go back to Jenkins and just paste it. (Note: In case of errors, just visit this link) https://docs.google.com/document/d/1Ju3KnHV84OGKa1dF4G_cOD9KQ2lOBzHZax3geiodr0I/edit?usp=sharing
- In ‘Branches to Build’, just leave it as master.
- Don’t click any option in the next section ‘Build Triggers’.
- In ‘Build’ section, click ‘Add build step’ and select ‘Invoke top-level Maven targets’.
- Add localMaven in the version box.
- Goals are phases of Maven build. There are different phases in Maven Build Lifecycle as shown below:
- All of these executed sequentially.
- As we need to specify Maven package command, this command will execute validate, compile, test before executing the package.
- In goals, just write:
- Clean makes sure that we have a clean environment before executing any further Maven steps.
- Just click save and Maven job will be configured.