
The below goals will be solved in this guide:
- Get the source code from the Subversion repository
- Link change logs to the repository browser using ViewVC
- Build the project using MBuild
- Run the tests using NUnit and display the results together with a trend graph
- Publish artifacts from the build (nightly builds)
- Run FxCop on an assembly and display warnings (linked with source code) and a trend graph
- Search the source code for TODO, FIXME comments and display the open tasks with links to the source code
Initial downloads
The following files are needed besides Java (at least 1.5). Get the latest version of all files and notice that the Hudson file has the extension .war and plugins .hpi. This guide assumes that MSBuild, NUnit and FxCop are already installed and working.
- Hudson server application (download)
- MSBuild plugin (download)
- NUnit plugin (download)
- Violations plugin (FxCop support among other such as Simian, CPD, PMD and PyLint) (download)
- Open Tasks plugin (download)
Installation steps
I'm going to install Hudson into c:\Program Files\Hudson.
- Copy the hudson.war file to c:\Program Files\Hudson
- Start Hudson through "java -DHUDSON_HOME=data -jar hudson.war". Verify that you can access Hudson through http://localhost:8080
- Copy the plugins to c:\Program Files\Hudson\data\plugins
- Stop Hudson by pressing Ctrl+C in the command prompt where you started Hudson.
- Start Hudson again and you should be set to go.
Hudson system configuration
Follow the following steps to configure the tools that Hudson will use in building MediaPortal.
- Go to the System configuration at http://localhost:8080/configure.
- MSBuild Builder - Set the path to the MSBuild tool to C:\Windows\Microsoft.NET\Framework\v2.0.50727\msbuild.exe

MediaPortal job configuration
- Click the "New job" link at the home page.
- Enter the name "MediaPortal", check the "Build a free-style software project" and press OK.

Source code management
MediaPortal uses a Subversion SCM. Hudson supports CVS and SVN out of the box, but there are many plugins for other SCMs. After checking out the files from the repository Hudson will show the new change sets since the previous build in the Build page. A detailed view of change sets can be seen in the Changes page as the name of the developer, files that were changed and the comment for the change. Each change set is linked to the MediaPortal subversion repository browser, so it is easy to browse the actual file that changed.
- Press the Subversion radio button to configure the SCM.
- Repository URL=https://mediaportal.svn.sourceforge.net/svnroot/mediaportal/trunk/mediaportal
- Local module directory=.
- Press the Advanced button
- Repository Browser=ViewSVN, URL=http://mediaportal.svn.sourceforge.net/viewvc/mediaportal/

To test the configuration, press Save and then Build. The source code will be downloaded from the repository and put into the Workspace. If there were any changes in the SCM repository they can be viewed in the Changes page.

When the build is completed verify that it has checked out the code by going to the Workspace page. Using the Workspace page you can browse and view the files that has been checked out and it doesn't matter if the files are on the master or on a distributed slave!

Building the project
Im going to build a Release version of MediaPortal using MSBuild and the mediaportal.sln solution file. Hudson also supports NAnt and many other build tools through plugins.
- Click the "Add build step" and select "Build a Visual Studio project or solution using MSBuild."
- MsBuild Build File=mediaportal.sln
- Command Line Arguments=/p:Configuration=Release

To test the configuration, press Save and then Build. Now the source code should be updated if there any changes and then built using MSBuild. While the build is running you can check the Console log that is updated as the build continues.

Running unit tests and showing the result
Hudson can read NUnit XML reports and display the results of them for each build. If a test fails, then it will be displayed in the Status page. Hudson will also show how many builds ago a test failed (it's age); that way it is simple to see if a test failed in the current build. If a build has at least one failing unit tests then the build will become Unstable (yellow); otherwise it is marked as Successful (blue). After two builds you will get a trend graph showing the test results over time. To run the NUnit tests, I'm running nunit-console from Hudson after the MSBuild has compiled all files.
- Click the "Add build step" and select "Execute Windows batch command"
- Command=
"c:\program files\nunit\bin\nunit-console.exe" MediaPortal.Tests\bin\RELEASE\MediaPortal.Tests.dll /xml=nunit-result.xml /config=test.config
exit 0 - Check the "Publish NUnit test result report" and enter "nunit-result.xml"

Start a new build, now the unit tests will be run and their results are collected at the end of the build. When the build is completed, you can see how many tests there were, and how many failed, in the build in the Status page.

More information on namespace detail can be shown in the Test result page.

The test results are then displayed as a trend report. (This trend graph is copied from my python project, as it shows a variation over time)

Archiving build artifacts
It is good to archive the outputs (artifacts) from the build, so they can be retrieved later. This way it is possible to get a version of MediaPortal that was built a week ago, and compare the functionality to one built today. Hudson can be configured to store any number of files for each build, it can also be configured so only the latest successful build's artifacts are stored. As I'm not really sure what the output from MediaPortal is, I'm going to show how to archive the files for the MPInstaller.
- Click the "Archive the artifacts".
- Files to archive = MPInstaller\bin\Release\*

In the next build, Hudson will store all files in the MPInstaller release folder so it can be retrieved later even if there are newer builds available.

Analyzing code with FxCop
Hudson can collect output from several quality metric tools and show them for each build. Similar to the unit tests, they will be displayed in a trend graph. For .NET projects, it is good to use FxCop. FxCop analyzes managed code assemblies and can be very good to use but it may also generate too many warnings at start, in this example I will only use the security rules and analyze the Core.dll assembly.
- Click the "Add build step" and select "Execute Windows batch command"
- Command =
"c:\Program Files\Microsoft FxCop 1.35\fxcopcmd.exe" /file:Core\bin\Release\Core.dll /rule:SecurityRules.dll /out:fxcop-result.xml
exit 0 - Check the "Violations" check box, and set fxcop=fxcop-result.xml

Next build will take considerate longer time, as FxCop goes through the assembly and analyzes it to find potential problems. When the build finishes, you will see a nice summary on how many violations there are in the current build.

Finding source code comments
A nice plugin is the Tasks plugin as it will go through the sources, and find specific comments that should be watched. In this example I'm going to search for TODO or FIXME comments,
- Click the "Scan workspace for open tasks".
- Files to scan = **/*.cs
- High priority=FIXME
- Normal priority=TODO

If the plugin finds any comments in the source they will be displayed in the build page.

There will also be a trend graph so it is possible to monitor that the tasks are decreasing over time. Together with the summary on what comments were found, the full source code can be viewed.


Summary
I hope this was a useful guide to start building .NET projects with Hudson, one of the many CI servers. As shown above it does not take much configuration to start building a project using Hudson. The best way to start using continuous integration is to start building the project. Then focus on running unit tests and other quality metrics so you can monitor the quality progress on your project.
Currently there are some plugins in the pipeline that could be interesting for .NET developers:
- A SCM implementation for Microsofts Team System
- Support for CodePlex issues and Wiki words in change set comment. And a very simple configuration to check out code from a CodePlex project
- Support for displaying coverage stats using NCover and MS own coverage tool

29 comments:
I must say this is a great post and tutorial!
Thank you very much for sharing this...
Slobo
This works perfectly for me. I am using NAnt instead of MSBuild and it all worked fine.
Hudson is infinitely better than cc.net
Great tutorial, thank you
Hello. I am trying to implement an example using xUnit.net (new tool) but I am having some issues. Does Hudson support (or will hudson support) xUnit.net?
Thanks
Patricio (pmosse@cubika.com)
Good Job!: )
Thanks for the great post! This was very helpful.
Thanks for the tutorial. If you want your build to fail if tests do not pass, simply replace the "exit 0" in the nunit command to:
"exit %ERRORLEVEL%"
.Net programming language is hard to study.All the people trying to know the .net.
----------------------
peer
Blaze Infotech
Great article. You really shortened our infrastructure set-up time.
Awesome tips for hudson, thanks
Thanks for a great article. However I'm getting the "Process leaked file descriptors" errors alot when I'm running the MSBuild commands.
Does anyone have a solution for this, when using ms build?
This post was very helpful to me in configuring Hudson to build and test our .NET application.
I used nant instead of msbuild directly. Since I was already using nant scripts for my build, it wasn't hard to adapt.
For the SVN integration, everyone should be aware that, if the update fails or has conflicts, Hudson will blow away the workspace and checkout a new copy. This means that if you've made modifications to your Web.config or App.config, they'll be lost. To compensate for, made copies of those files outside of the workspace and added a build step to copy them back into place.
For using nant, I discovered that if you have two separate nant build steps, Hudson will overwrite the second one with a copy of the first one when it saves the config file. I've taken to editing the config.xml in emacs and then re-loading it, instead. I'll probably move to integrate all of my build targets into one step, but since we had separated tests into a separate .build, that is going to take some re-organization of my build process.
For running the nunit tests, the image of your command was very helpful. I've customized it a bit to put the tests results from each run into a separate directory using %BUILD_TAG%. You have to add a build step to create the directory before the nunit build step.
Two questions:
Have you used the Hudson environment variables? I've been able to incorporate them into batch commands, but wasn't able to get them to work with nant or the nunit history plugin.
I haven't been able to get the nunit history plugin to read my nunit output xml files (with or without using Hudson environment variables). Any suggestions? I've tried both straight file names and xml configurations based on the documention on file set type which is linked to from the plugin config.
@joy - Its best if you ask those questions on the Hudson user mailing list, as there you will get quicker answers and perhaps more correct than I can give you. List can be found at https://hudson.dev.java.net/servlets/ProjectMailingListList, or through nabble http://www.nabble.com/Hudson-users-f16872.html
I found this to be a wonderful post. I'm a new joinee and spiking Hudson. This was exactly the thing I was looking for to get started. Thanks a lot for sharing this.
Almost exactly the same boat as Tan, I am spiking hudson for out continuous integration system, and this article has proven invaluable.
Now I just need to spike Hudson and VB6.. god help me.
Wow, so far Hudson is great. Very easy to set up. Does anyone have a SandCastle documentation plugin?
Excellent information - thanks so much for providing such detail for getting up and running with Hudson and .NET.
Thanks for a great article.
It was helpful.
one question though. do you know how to set up husdon to deploy the built project to a different server?
@asha - What do you want to do? Share the complete Hudson project on another Hudson server, or just share the artifacts from a build to another server (if so, there is ftp, scp and others). I am not sure, so I suggest you send your question to users@hudson.dev.java.net
Good Job!
One question: Hudson is used for the desktop System.
Thank You,
Flávia Morais
This guide was invaluable to me as well. I've nearly implemented everything mentioned here and plan to do more. I didn't see a wiki page on the Hudson wiki specifically for this, but I think there needs to be one so we can expand upon this knowledge there.
@trinition - Actually the Hudson Wiki points at this blog post, but maybe the content should be copied to the Hudson wiki so people can extend the guide.
There is now an NCover plugin in the Hudson repository written by Mike Rooney at Genius.com. Mike wrote a blog post covering how to install and use the NCover Hudson plugin.
This helped enormously as I set up my first Hudson installation. Thanks.
Richard
This helped enormously as I set up my first Hudson installation. Thanks.
Richard
When I run the NUnit tests, I get this error - "The project "MediaPortal.Tests" is not selected for building in solution configuration 'Release|Any CPU' "
The project "MediaPortal.Tests" not getting compiled and hence folder bin\release is empty.
@redsolo - When I run the NUnit tests, I get this error - "The project "MediaPortal.Tests" is not selected for building in solution configuration 'Release|Any CPU' "
The project "MediaPortal.Tests" not getting compiled and hence folder bin\release is empty.
msbuild ver used - 2.0.50727
mediaportal ver used - 1.0
Your guide is great to follow but when I try it out locally this error pops up. I downloaded straight from the url which you mentioned. I dont have VS and hence can't add the tests to the solution file. Help required badly to proceed further with Nunit tests.
Very helpful post indeed.. But somebody can you help me out in creating a zip file of specific dlls along with exe that is generated after a successful build using Post build action/ Create artifact? Are we supposed to write a build.xml file like one for Java? If yes, where should we. Please help , I have spent a lot of time in this.
Post a Comment