Lab-3 (Installing Maven in Jenkins)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

1

Installing Maven in Jenkins


Jenkins Configuration:
In this lab, we will improve the configuration of our Jenkins server. Many Java
projects use the Maven build system. While you can always use a freestyle job to
build just about anything Jenkins can provide additional functionality when
properly configured to use Maven projects directly.

Our Jenkins server is not yet configured to send email messages. In order to take
advantage of the several useful build notification features provided by Jenkins we
will need to set our lab system up with mail services and then configure Jenkins to
use them.

This lab will walk you through some common Jenkins server configuration tasks
and help you gain familiarity with Jenkins server setup and the ways that Jenkins
manages configuration settings. First, we will configure Jenkins to use Maven and
then we will setup Jenkins mail services.

STEP1. Install Maven

Before we configure Jenkins to use Maven we need to install an appropriate version


of Maven on the system. There are many ways we can install maven, however
because maven is so ubiquitous we can usually install it from packages on Linux
systems and get a fairly recent version. In a terminal window use the apt-get package
manager to install maven:

user@ubuntu:~$ sudo apt-get install maven


...
user@ubuntu:~$ mvn --version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-57-generic", arch: "amd64", family: "unix"

We now have maven 3.3.9 installed with a home directory set to /usr/share/maven.
2

STEP2. Configure Jenkins to use Maven:

1. Open a browser and point it at your Jenkins server. From the dashboard select “Manage
Jenkins” and choose “Manage Plugins”

Go to Available and on top Search for Maven and select only maven integration
plugin and choose to install without restart:
3

STEP3: Go back to dashboard and select manage plugins and choose Global
Tool Configuration
• Scroll down to search for maven and in your Terminal enter
maven –version
4

Add a maven installation to your system with the name “Maven 3.3.9” and the
correct MAVEN_HOME path reported by the mvn --version command (be sure
to uncheck install automatically).
Save your changes by clicking the “Save” button at the bottom left of the
configuration page.
STEP4- Test your Maven Installation
From the Jenkins Dashboard create a new project, name it maventest and select the
maven project type
5

Without changing the configuration, click save to create your project. If this works
you have correctly configured Jenkins with Maven. Without a valid Maven
configuration Jenkins will not allow you to create a maven project. Also, if there are
multiple Maven configurations Jenkins will require you to choose the version to use
with your new project.

Leave all of the projects defaults in place and save the new maventest project. When
the project page is displayed click the “Delete Maven project” link from the
navigation bar at the left of the page to delete the project.

We will create a complete Jenkins Maven project in a later lab.

STEP5. Setting up a local delivery mail server


For testing purposes, it can be useful to configure a mail server capable of delivering
Jenkins build notifications. Most Linux distributions, including our lab system, no
longer come with a preinstalled mail client or server, though a range of solutions
can be easily installed.
Here we will install the standard Ubuntu Postfix mail server and configure it to
deliver mail locally to system user mailboxes. This will allow us to configure
our Jenkins server to send mail to the Postfix system so that we can examine the
results in our “user” account mailbox.
Step 1: Install Postfix
To install Postfix on an Ubuntu 14.04 system we can simply install the mailutils
package:
• sudo apt-get install mailutils
6

The package installer automatically runs the configuration utility. Configure the
server for local only delivery:

Select Local Only and press Enter


7

This will set up a mail server that will accept mail only for local users.

Next set the host name for the mail system. Normally this would be myco.com or
some such, but for our purposes we can just use the default Ubuntu host name
“ubuntu”.

When the installation completes, test the mail server using the mail command line
client to send an email to “user” on the localhost “ubuntu”:

echo "This is the body of the email" |mail -s "this is the Subject line" ubuntu@ubuntu

This sends the echoed string with the subject specified by the mail -s switch to the
mailbox for “user” on the host “ubuntu”.

Now read you mail using the main command line mail reader (yes, people actually
used to read their mail at the command prompt):
8

In this example, the mail command is executed with no parameters causing it to


run interactively, displaying the list of messages in the current user’s inbox. In
this case the user “user” on the host “ubuntu” has one message waiting with the
serial number “1”. To display a message, we can just enter its number.

To delete the most recently displayed message we use the “d” command. To quit
the mail utility, we use the “q” command. You can get help with the mailer by
typing “?”.

Step 2: Configure Jenkins to send email

From the Jenkins home page click the “Manage Jenkins” link in the left-hand side
navigation bar.
9

In the Manage Jenkins page click the “Configure System” link:

Scroll to the bottom of the configuration page and locate the “E-mail Notification”
section. Enter “localhost” as the SMTP server and “@ubuntu” as the default e-mail
suffix. Finally click the “Test configuration by sending test e-mail” check box and
specify the “user” user as the email target. Finally click the “Test configuration”
button at the bottom right to test the email settings:

You can configure a “reply to” address and other settings in the advanced block
using the “Advanced…” button but we do not need to configure any of the
advanced settings for our configuration. Save your configuration changes by
clicking the “Save” button at the bottom left.
Now return to the lab machine command prompt and check your mail:
10

Success! You now have your Jenkins server configured to send email automatically.

5. Under the configuration hood

Jenkins maintains essentially all of its server configuration settings in xml files
found in JENKINS_HOME.

List the contents of the .jenkins directory on your lab system.


11

As you can see there are a number of .xml files. The config.xml is the main Jenkins
configuration files. This file contains less and less over time because all of the
functions that Jenkins orchestrates are either currently or soon to be migrated to
plugins. Jenkins being therefore the framework within which build plugins are
managed.

For example, to see our Mailer configuration settings you can display the
hudson.tasks.Mailer.xml file contents:

cat .jenkins/hudson.tasks.Mailer.xml
12

To see our Maven configuration settings, display the hudson.tasks.Maven.xml file


contents:

cat .jenkins/hudson.tasks.Maven.xml

While it is not generally necessary to edit these files directly, knowing your way
around makes it easier to fix problems, clone and backup configurations among
other administrative tasks.

You have now completed the Jenkins configuration lab.

You might also like