Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

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.


Wednesday, December 3, 2014

Git: Installation on Ubuntu

Installation


Installing Git on Ubuntu 14.04; open a terminal window and type:
 sudo apt-get install git  


Operational Output

 craigtrim@CVB:~$ sudo apt-get install git  
 [sudo] password for craigtrim:   
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following extra packages will be installed:  
  git-man liberror-perl  
 Suggested packages:  
  git-daemon-run git-daemon-sysvinit git-doc git-el git-email git-gui gitk  
  gitweb git-arch git-bzr git-cvs git-mediawiki git-svn  
 The following NEW packages will be installed:  
  git git-man liberror-perl  
 0 upgraded, 3 newly installed, 0 to remove and 241 not upgraded.  
 Need to get 3,274 kB of archives.  
 After this operation, 21.6 MB of additional disk space will be used.  
 Do you want to continue? [Y/n] Y  
 Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main liberror-perl all 0.17-1.1 [21.1 kB]  
 Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/main git-man all 1:1.9.1-1 [698 kB]  
 Get:3 http://us.archive.ubuntu.com/ubuntu/ trusty/main git amd64 1:1.9.1-1 [2,555 kB]  
 Fetched 3,274 kB in 3s (1,009 kB/s)  
 Selecting previously unselected package liberror-perl.  
 (Reading database ... 168600 files and directories currently installed.)  
 Preparing to unpack .../liberror-perl_0.17-1.1_all.deb ...  
 Unpacking liberror-perl (0.17-1.1) ...  
 Selecting previously unselected package git-man.  
 Preparing to unpack .../git-man_1%3a1.9.1-1_all.deb ...  
 Unpacking git-man (1:1.9.1-1) ...  
 Selecting previously unselected package git.  
 Preparing to unpack .../git_1%3a1.9.1-1_amd64.deb ...  
 Unpacking git (1:1.9.1-1) ...  
 Processing triggers for man-db (2.6.7.1-1) ...  
 Setting up liberror-perl (0.17-1.1) ...  
 Setting up git-man (1:1.9.1-1) ...  
 Setting up git (1:1.9.1-1) ...  



Setting your Username and Email

Git uses your username to associate commits with an identity.

The git config command can be used to change your Git configuration, including your username. It takes two arguments:
 craigtrim@CVB:~$ git config --global user.name "craigtrim"  
 craigtrim@CVB:~$ git config user.name  
 craigtrim  
Notice how I was able to confirm the setting in the second line. Also, by using the -global flag, I've set this for every repository on the computer.

I also want to set the email:
 git config --global user.email "craigtrim@gmail.com"  



Cloning a Remote Repository

I want to get a copy of the SWTK commons project so I can look at the code. I'm going to clone the repository in Git. You simply First i'll create a directory for the contents
 mkdir -p $WS/swtk/commons  
 cd $WS/swtk/commons  

Then I'm going to set up a remote which tells Git where the repository is
 git remote add commons https://github.com/torrances/swtk-common.git  

then clone
 git clone https://github.com/torrances/swtk-common.git  

Operational Output

 craigtrim@CVB:~/workspace/swtk/commons$ git remote add commons https://github.com/torrances/swtk-common.git  
 craigtrim@CVB:~/workspace/swtk/commons$ git clone https://github.com/torrances/swtk-common.git  
 Cloning into 'swtk-common'...  
 remote: Counting objects: 770, done.  
 remote: Compressing objects: 100% (139/139), done.  
 remote: Total 770 (delta 44), reused 0 (delta 0)  
 Receiving objects: 100% (770/770), 1.57 MiB | 975.00 KiB/s, done.  
 Resolving deltas: 100% (264/264), done.  
 Checking connectivity... done.  
 craigtrim@CVB:~/workspace/swtk/commons$ ls -lah swtk-common/  
 total 36K  
 drwxrwxr-x 9 craigtrim craigtrim 4.0K Dec 3 17:53 .  
 drwxrwxr-x 4 craigtrim craigtrim 4.0K Dec 3 17:53 ..  
 drwxrwxr-x 2 craigtrim craigtrim 4.0K Dec 3 17:53 build  
 drwxrwxr-x 3 craigtrim craigtrim 4.0K Dec 3 17:53 dependencies  
 drwxrwxr-x 3 craigtrim craigtrim 4.0K Dec 3 17:53 docs  
 drwxrwxr-x 8 craigtrim craigtrim 4.0K Dec 3 17:53 .git  
 drwxrwxr-x 5 craigtrim craigtrim 4.0K Dec 3 17:53 org.swtk.common.core  
 drwxrwxr-x 7 craigtrim craigtrim 4.0K Dec 3 17:53 org.swtk.common.data  
 drwxrwxr-x 5 craigtrim craigtrim 4.0K Dec 3 17:53 org.swtk.common.mysql  
 -rw-rw-r-- 1 craigtrim craigtrim  0 Dec 3 17:53 README.md    



References

  1. https://help.github.com/articles/setting-your-username-in-git/
    1. Setting your username in Git
  2. [YouTube] How to Install and Configure Git & Repositorys on GitHub
  3. Cloning a Remote Repository using Git

Monday, December 1, 2014

Git: Cloning a Remote Repository

Assumptions

  1. A remote GIT repository exists
  2. I want to use the project with Eclipse
  3. I want a “projects” sub-directory that does not contain Eclipse meta-data


Command Sequence

  1. mkdir shortname
  2. cd shortname
  3. git init
  4. git remote add shortname url
  5. git clone url
  6. git pull -u shortname
  7. mkdir projects
  8. cd projects
  9. mv shortname/ projects/
Open Eclipse and Import Projects! Reference “Git with Eclipse: Design Patterns”



Case Study: Clone SWTK Commons

  1. mkdir commons
  2. cd commons
  3. git init
  4. git remote add commons https://github.com/torrances/swtk-common.git
  5. git clone https://github.com/torrances/swtk-common.git
    1. Operational Output
    2.  craigtrim@W540 /c/Backup/Java/workspaces/swtk/commons (master)  
       $ git clone https://github.com/torrances/swtk-common.git  
       Cloning into 'swtk-common'...  
       remote: Counting objects: 737, done.  
       remote: Compressing objects: 100% (106/106), done.  
       remote: Total 737 (delta 17), reused 0 (delta 0)  
       Receiving objects: 100% (737/737), 1.57 MiB | 455.00 KiB/s, done.  
       Resolving deltas: 100% (237/237), done.  
       Checking connectivity... done.  
      
  6. git pull -u commons
  7. mkdir projects
  8. cd projects
  9. mv commons/ projects/



Troubleshooting

  1. Remote already exists
    1. The remote add command was already executed
    2. List existing remotes on the command line using remote -v
    3. Git Reference: Working with Remotes
  2. Git Repository Information not displayed in the Eclipse Package Overview
    1. This is not important. Git interaction should occur within the Git Shell; not within Eclipse. I do not recommend using the built-in Git plugin with Eclipse. Eclipse is useful as an IDE, but building should be handled externally (eg. via Maven) as well as repository interaction (via the Git command line).