Sunday, December 21, 2014

Creating a Maven Project in Jenkins

Previous Article: Installing Jenkins on Ubuntu.


Outline

  1. Creating the Build
    1. Maven Project Name and Description
    2. Discarding Old Builds
    3. Source Code Management
    4. Build Triggers
    5. Root POM
  2. Running the Build
  3. Troubleshooting
    1. Local JAR Installations

Environment Assumptions

  1. Jenkins 1.595
  2. Ubuntu 14.04
  3. Apache Maven 3.2.3
  4. Git 1.9.5
  5. Oracle JDK 7



Creating a Maven Build Project


Let's tell Jenkins to create a new item.

I'll open my web browser to
http://localhost:8080
and click on new item:


I'm going to name the maven project using the artifactId from my POM file.  This is convention; not a Jenkins requirement:



Scroll down a little further, and enter the

  1. Maven Project Name 
  2. Description
  3. Github Project URL
  4. and optionally, select an option for Discarding Old Builds



Discarding Old Builds


It's best to have a strategy for discarding old builds.  The server host can quickly run out of disk space if old builds are retained indefinitely.

Jenkins will permit configuration for "Days to keep builds" and "Max # of builds to keep.  I generally keep the last 5 builds:




Source Code Management

Select "Git" and supply the same URL you use to clone your repository in the "Repository URL" section.  Credentials will be either added to the global repository for the first time, or selected from pre-existing credentials:





Building Triggers


Jenkins comes with five options for build triggers.

1. Build whenever a SNAPSHOT dependency is built
I leave this checked as default, but don't overly rely on it.

2. Build after other projects are built
I have a series of projects that are interconnected.  When one of them is built in Jenkins, this will trigger downstream (dependent) projects to be built as well.  I find it useful to schedule a CRON job (see next section) on the base project, and let the downstream triggered using this option.

This option likewise gives me the option to assess the stability of an upstream build, prior to initiating this trigger.


In this case, I choose to move forward with the build, even if the "trend" of the build is unstable.

3. Building Periodically
Jenkins uses CRON expressions as a schedule to execute some routine.  A CRON expression is a string comprising five or six fields separated by white space.

I'm currently scheduling by builds to run every 10 minutes by including this expression:
 H/10 * * * *   

The Maven configuration section looks like this:



4. Build when a change is pushed to GitHub
This is another useful trigger (additional configuration required).

5. Poll SCM
Similar to the fourth option.  I'm not sure if there's any benefit to configuring both of these simultaneously.

Quick References:
  1. [Stackoverflow] Using Snapshot Dependencies
  2. [Stackoverflow] How to schedule jobs in Jenkins
  3. Trigger Builds when Changes are Pushed to Github



The Root POM


It's not unlikely that your Git repository will have multiple Java projects.  In this case, you will want to have a parent POM file that can control the build for the entire workspace.

This is the POM that you pass off to Jenkins here:





Building the Project


At this point, configuration is complete, and you can click the Save button.

On the Jenkins dashboard, select "Build Now" to validate that your project can be built correctly:


The progress of this task can be tracked by clicking on the build work item that shows up in the Build History box.

This will take you to a page that will give you both real-time and (eventually) historical progress of this build:


Clicking on Console Output will give you the Maven console output from this build.


Local JAR Installations


Do any of your Maven POM reference JAR files that are installed locally?  You will need to install these on your Jenkins server prior to a successful build.

I have had some trouble getting this to work, and it would appear that I am not alone:
  1. Jenkins Bug Report [10123]
    1. No formal fix provided; some users report success and are unable to replicate the issue.
  2. [Stackoverflow] How to manage third-party dependency with Eclipse, Maven and Jenkins?
    1. A primary suggestion is to go with a third-party repository (like Sonatype Nexus).

I am using a workaround at the moment:
 <dependency>  
      <groupId>jaws</groupId>  
      <artifactId>jaws</artifactId>  
      <version>1.2</version>  
      <type>jar</type>  
      <scope>system</scope>  
      <systemPath>/home/craig/jaws-bin.jar</systemPath>  
 </dependency>  

This will get Jenkins to work, but it's not a recommended approach.


8 comments:

  1. Thank you for sharing the article. The data that you provided in the blog is informative and effective. Best Devops Training Institute

    ReplyDelete
  2. Really very happy to say that your post is very interesting. I never stop myself to say something about it. You did a great job. Keep it up.
    We have an excellent IT courses training institute in Hyderabad. We are offering a number of courses that are very trendy in the IT industry. For further information, please once go through our site.
    DevOps Training In Hyderabad

    ReplyDelete

  3. Very Nice Blog…Thanks for sharing this information with us. Here am sharing some information about training institute.
    tableau training in hyderabad

    ReplyDelete