Sunday, April 29, 2012

Continuous Integration Using Jenkins, Git, and Maven

This tutorial demonstrates how to set up Jenkins, Git, and Maven to work together and provide a smooth continuous integration system. While the examples shown here are on a Windows machine, the concepts are the same for other operating systems such as Linux or Mac OS X, and with slight adjustments, can be adapted accordingly. This article assumes the following is installed and running on the system:

Why CI?

Continuous integration attempts to automate building and testing of source code at regular intervals in order to alert a team as early as possible about problems and merging issues.

From Wikipedia: Continuous Integration, "In software engineering, continuous integration (CI) implements continuous processes of applying quality control — small pieces of effort, applied frequently. Continuous integration aims to improve the quality of software, and to reduce the time taken to deliver it, by replacing the traditional practice of applying quality control after completing all development."

From Martin Fowler: "Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly."

Configure Jenkins

Step 1: Enable security. From the Jenkins dashboard, navigate to 'Manage Jenkins' and then 'Configure System'. Note: Newer versions have this under "Configure Global Security" instead.

1.a. Check 'Enable security'

1.b. Choose 'Jenkins's own user database'

1.c. Uncheck 'Allow users to sign up'

1.d. Choose 'Matrix-based security'

1.e. Enter an admin username next to 'User/group to add:'

1.f. Check all rights for the user.


1.g. Click 'Save'

1.h. Enter log in information for the admin user in the form that appears.


Step 2: Add a JDK. From the Jenkins dashboard, navigate to 'Manage Jenkins' and then 'Configure System'.

2.a. Click 'Add JDK' and enter Name and JAVA_HOME.


2.b. Click 'Apply'

Step 3: Add Maven information.

3.a. Click 'Add Maven' and enter Name and MAVEN_HOME.


3.c. Click 'Save'

Step 4: Add Git information.

4.a. From the Jenkins dashboard, navigate to 'Manage Jenkins' and then 'Manage Plugins'.

4.b Click on the 'Available' tab and search using the browser (CTRL+F) for 'git plugin'.

4.c. Check 'Git Plugin' and click 'Download now and install after restart'. The install can take a while.

4.d. From the Jenkins dashboard, navigate to 'Manage Jenkins' and then 'Configure System'.

4.e. Add Git information.


4.f. Add Git user and email info under section "Git Plugin".



4.g. Click 'Save'.

Step 5: Configure a Jenkins Job.

5.a. From the Jenkins dashboard, navigate to 'New Job'.

5.b. Enter a 'Job Name' and select 'Build a maven2/3 project'. Click 'OK'.


5.c. Enter a 'Description'.

5.d. Select 'Discard Old Builds'.

5.e. Enter an amount by 'Max # of builds to keep'.


5.f. Select 'Git' and enter a 'Repository URL'. If you wanted to track a git repo on Github, this is where you would enter the git URL.


5.g. Check 'Poll SCM' and add '* * * * *' to 'Schedule'. '* * * * *' is a cron expression which means 'every minute'.

5.h. Under 'Build' add a Maven goal in 'Goals and options'.


5.i. Click 'Save'.

Step 6: Run the Job.

6.a. From the Jenkins dashboard, run the new job by clicking the clock with arrow icon on the far right. If everything is set up correctly, the job should run and a blue ball should appear. You may have to refresh the page after the job runs to refresh the ball.


If you need to debug a Jenkins job either while it is running or after it is finished, you can view console output. From the dashboard, click on the job name. Once on the job's page, hover over a link in the 'Build History' section and select 'Console Output'.


Also, when you push new commits to the git repo, Jenkins should recognize that and start the job automatically.

Piece of Cake!!!

Helpful Links

Install Jenkins on Windows as a Windows Service
Hello World - Maven, M2Eclipse and Eclipse
Setup Git on Windows

Saturday, April 28, 2012

Wingsuit Flying



Wingsuit Flying on Wikipedia

Install Jenkins on Windows as a Windows Service

Installing Jenkins on Windows and setting up a Jenkins Windows Service, which starts Jenkins automatically whenever Windows is restarted is now easier than ever since there is a Jenkins Windows install wizard for it. This short tutorial shows how to setup Jenkins on Windows.

Step 1: Download the Windows native package from the Jenkins main website.


Step 2: Unpack the .zip file.

Step 3: Click through the install wizard.





Step 4: Verify that Jenkins is running. Open up a browser and go to: http://localhost:8080/. You should see the following:


Piece of Cake!

Force Google Chrome to use google.com as the Default Search Engine with Omnibox

As an American living abroad, it's somewhat annoying that the default search engine in Google Chrome in conjunction with Chrome's Omnibox is set to google.ch rather than google.com. Here's what I did to force Chrome to use google.com. You could adjust the following to suit your particular needs.

Step 1: Open Settings in Google Chrome.

Step 2: Under 'Basics', click on 'Manage Search Engines...'

Step 3: Delete all the search engines that are present.

Step 4: Set up a new entry with the following as the URL (all one line):

https://google.com/search?{google:RLZ}{google:acceptedSuggestion}{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}{google:instantFieldTrialGroupParameter}sourceid=chrome&ie={inputEncoding}&q=%s

Step 5: Add the new entry as the default search engine. The setting should look something like this:


Piece of Cake!!!

Friday, April 27, 2012

Hello World - Maven, M2Eclipse and Eclipse

This tutorial takes you through all steps needed to create a Hello World Java Application using Maven, M2Eclipse and Eclipse from scratch. The instructions are geared toward a Windows machine, but you should probably be able to easily adapt the steps to apply to Linux or Mac OS X.

Eclipse

Step 1: Download Eclipse. Download the lastest version of Eclipse at the Main Eclipse download page. You can download which ever flavor of Eclipse you like the best. I chose Eclipse IDE for J2EE Developers.


Step 2: Install Eclipse. Unpack the Eclipse .zip file and move the entire folder 'eclipse' to the 'Programs' folder on the hard drive.

M2Eclipse

Step 3. In Eclipse, go to Help --> 'Eclipse Marketplace...' and search for 'Maven'. Click on 'Install' for the m2eclipse plugin title 'Maven Integration for Eclipse'.


Step 4: Click through the wizard, installing m2eclipse.

Maven (Optional)

Installing Maven is optional because M2Eclipse does have its own embedded version of Maven. I chose to install Maven separately for two reasons. First, I wanted the latest and greatest version of Maven. Secondly, I plan to run a CI server (Jenkins) that needs access to Maven, and I wanted M2Eclipse and Jenkins to both use the same Maven install. If you don't plan on installing Maven separetely, skip to step 10.

Step 5: Download Maven. Download the lastest version of Maven at the Main Apache Maven download page choosing the binary .zip file for Windows.


Step 6: Install Maven. Unpack the Maven .zip file and move the entire folder 'apache-maven.x.x.x' to the 'Programs' folder on the hard drive.

Step 7: Set environment variables (adjust according to your setup):

JAVA_HOME C:\Program Files\Java\jdk1.6.0_25
M2_HOME C:\Program Files\apache-maven-3.0.4
M2 %M2_HOME%\bin

Path ;%M2%;%JAVA_HOME%\bin

Step 8: Verify Maven install with 'mvn -- version' at the command line. Note, if you had the command line open before changing the environment variables, close it and reopen it.

>mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: C:\Program Files\apache-maven-3.0.4
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_25\jre
Default locale: de_DE, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

Step 9: Configure m2eclipse. In Eclipse, go to Window --> Preferences

9.a. Under Maven --> Installations, specify an external Maven install by clicking 'Add...' next to 'Select the installation used to launch Maven'. By default m2eclipse selects it's own embedded Maven version.


9.b. Under Maven --> User Settings, specify a settings.xml by clicking 'Browse...'. I chose to put the settings.xml file inside Mave's conf folder.


9.c. Edit the settings.xml file and specify the location of the local Maven repository. I chose 'C:\Program Files\apache-maven-3.0.4\repository'. After editing settings.xml click the 'Update Settings' in 'User Settings' from the previous step.


A repository in Maven is used to hold build artifacts and dependencies of varying types. The local repository refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.

Hello World

Step 10: Create Maven Project in Eclipse.

10.a. In Eclipse open the New Project Wizard by clicking File --> New --> Project. Under 'Maven' select 'Maven Project' and click 'Next'.


10.b. On the next page of the wizard, check 'Create a simple project' and click 'Next'.


10.c. Give a 'Group Id' and an 'Artifact Id' and click 'Finish'.



Step 11: Create a class with a main method in src/main/java. In this case I created MelloMavenMain.java.


Step 12: In the main method, log a message using slf4j. I chose to use a 3rd party library to do this to demonstrated how to depend on 3rd party libraries in the next step.


Logger logger = LoggerFactory.getLogger(HelloMavenMain.class);
logger.info("Hello Maven!");

Step 13: Add the slf4j dependency in the project's pom.xml file.



  
    org.slf4j
    slf4j-simple
    1.6.4
  

Step 14: Package your program. Right-click on the project and choose Run As... --> 'Maven Install'. This will create a jar file for your project in the 'target' directory.

Piece of Cake!!!

Helpful Links

Maven Documentation
m2eclipse
Maven Eclipse Plugin

Tuesday, April 24, 2012

Fixing Unsatisfiedlinkerror - Can't Find Dependent Libraries

After I finally got my Java JNI code working on my Windows development machine that sets the local time of the computer from Java, I discovered it was not working on the production machine. I got the dreaded Unsatisfiedlinkerror - Can't Find Dependent Libraries runtime error. Following is an explanation of why it was happening and what I did to fix it...

The Cause - Visual Studio C++ 2010 Express

In order to find what dependent library was missing on the production machine that was present on my development machine, I downloaded and installed Dependency Walker. After analyzing my generated DLL I saw something unexpected.


What the heck is MSVCR100.DLL?? It turns out it's added by Visual Studio C++ during the build of the DLL, and it is NOT necessary.

The Solution

There seem to be several solutions to this problem, but the most elegant in my opinion to to configure Visual Studio to not make the built DLL dependent on it in the first place. Here's how to do that: Right click on the DLL project and open up the properties dialog. Under Configuration Properties -> C/C++ -> Code Generation -> Runtime Library select "Multi-threaded (/MT)". source


Rebuild your DLL and verify with Dependency Walker that the rogue dependency is gone.


Piece of Cake!!!

Monday, April 23, 2012

Uninstall Stubborn Java JRE or JDK from Windows

Having trouble uninstalling or fixing Java from a Windows 7 machine like I did? I somehow managed to mess up my Java installation and was receiving an error 1723 along the way while trying to uninstall the 64-bit version of Java 6 (in my case version 6.0.31). It was complaining about not being able to find a certain file which it apparently needed for the uninstall. What I did was the following:

Step 1: Locate the following folder:

C:\Users\<User>\AppData\LocalLow\Sun\Java\jdk1.7.0_x64

and make a copy of it in

C:\Users\<User>\AppData\LocalLow\Sun\Java

If it isn't there, you need to downlaod and install Java jdk1.7.0_x64.

Step 2: Rename the copied folder to

C:\Users\<User>\AppData\LocalLow\Sun\Java\jdk1.6.0_31_64

Step 3: Rename

C:\Users\<User>\AppData\LocalLow\Sun\Java\jdk1.6.0_31_64\jdk1.7.0.msi

to

C:\Users\<User>\AppData\LocalLow\Sun\Java\jdk1.6.0_31_64\jdk1.6.0_31.msi

Following those steps combined with some restarting of Windows allowed me to Uninstall my corrupt Java 6 version. Make sure you replace <User> above with your Windows user name.  I hope that helps someone!