views:

4591

answers:

13

I am aware there are other nant and msbuild related questions on stack overflow, but I could not find a direct comparison between the two and so here is the question.

When should one choose Nant over MSBuild? Which one is better for what? Is Nant more suitable for home/open source projects and MSBuild for work projects? What is your experience with any of the two?

+9  A: 

Personally, I use both - for the same project.

MSBuild is great at building Visual Studio solutions and projects - that's what it's made for.

NAnt is more easily hand-editable, in my opinion - particularly if you already know Ant. NAnt can call MSBuild very easily with NAntContrib. So, I hand-craft a NAnt script to do things like copying built files, cleaning up etc - and call MSBuild to do the actual "turn my C# source code into assemblies" part.

If you want an example of that, look at my Protocol Buffers build file. (I wouldn't claim it's a fabulous NAnt script, but it does the job.)

Jon Skeet
And it does support Mono. Mono's MSBuild support is crappy.
Mehrdad Afshari
@Mehrdad: good point, I never thought about that.
petr k.
@Mehrdad: Yes, at some point I really must try to get Protocol Buffers to compile on Mono... currently there's a gmcs bug which stops it from working :( The idea was always that it would eventually work on Mono though.
Jon Skeet
+1  A: 

I have switched from NAnt to MSBuild recently because of its ability to build VS solutions. I still use NAnt occasionally, though.

You may also want to check out MSBuild Community Tasks which is like NAntContrib.

petr k.
+3  A: 

While I'm not very familiar with MsBuild, I'm under the impression that some of key differences on both sides can be supplemented by additions:

I recently had to build a Silverlight project in Nant. I discovered that life would be easier if I just did this with MsBuild - I ended up calling a MsBuild task from within a Nant script so I suppose it's not too out of the ordinary to mix and match the two.

Beyond that, I suppose it's going to be a question of personal preference - obviously you can manage some/most of MsBuild's functionality from within Visual Studio, if that's your thing. Nant seems more flexible and better suited if you prefer to write scripts by hand, and if you come from the Java world you'll likely be right at home with it.

mmacaulay
Good point. Both solutions can easily be extended and customized in any way desired.
Clever Human
+29  A: 

One of the major draws of MSBuild for me (on Windows platforms) is that it comes as part of .NET itself. That means that any Windows machine that is up-to-date with Windows Update will have MSBuild available. Add to this the fact that C# compiler is also part of .NET itself and you have a platform that can build projects on clean machines. No need to install Visual Studio behemoth. NAnt, on the other hand, has to be explicitly installed before a build can be triggered.

Just for the record, I've used NMake, Make, Ant, Rake, NAnt and MSBuild on non-trivial builds in the past (in that order). My favourite is MSBuild, hands down (and I do not favour it because "that's what Visual Studio uses"). IMHO, it is a very under-appreciated build tool.

I would compare NAnt vs. MSBuild to the difference between procedural and functional programming. NAnt is quite straightforward and you-get-what-you-see. MSBuild on the other hand requires a bit more thought. The learning curve is steeper. But once you "get it", you can do some amazing things with it.

So I would recommend looking at MSBuild if you also gravitate towards functional or logical style programming - if you are willing to invest a bit of time and effort before seeing tangible results (of course, I also strongly believe that the investment eventually pays off and you can do more powerful things more efficiently).

Milan Gardian
Wow! Didn't know that MSBuild came shipped with the runtime. That's pretty cool and I can definitely see benefits there. However, I don't understand the comparison between functional/procedural. It would be interesting to hear what make MSBuild so much more powerful once one "gets it".
Scott Saad
+33  A: 

I've done a similar investigation this week. Here's what I've been able to determine:

NANT:

  • Cross-platform (supports Linux/Mono). May be handy for installing a web site to multiple targets (i.e.: Linux Apache and Windows IIS), for example.
  • 95% similar in syntax to Ant (easy for current Ant users or Java builders to pick up)
  • Integration with NUnit for running unit tests as part of the build, and with NDoc for producting documentation.

MSBuild:

  • Built-in to .NET.
  • Integrated with Visual Studio
  • Easy to get started with MSBuild in Visual Studio - it's all behind the scenes. If you want to get deeper, you can hand edit the files.

Subjective Differences: (YMMV)

  • NAnt documentation is a little more straight forward. E.g.: The MSBuild Task Reference lists "Csc Task - Describes the Csc task and its parameters. " (thanks for the "help"?), vs the NAnt Task Reference "csc - Compiles C# programs."
  • Not easy to figure out how to edit the build script source (.proj file) directly from within Visual Studio. With NAnt I just have Visual Studio treat the .build script as an xml file.
  • Apparently, in Visual Studio, Web Application Projects don't get a .proj file by default, so I had great difficulty figuring out how to even get MSBuild to run on mine to create a deployment script.
  • NAnt is not built-in to Visual Studio, and has to be added, either with an Add-In, or as an "External Tool". This is a bit of a pain to set up.
  • (Edit:) One of my coworkers brought this up--if you want to set up a build machine using CruiseControl for continuous integration, CruiseControl integrates with NAnt nicely out of the box. I'm not aware of a MSBuild plug-in for CruiseControl (that doesn't mean there isn't one).
Ogre Psalm33
you can edit a .proj file but after you unload the project (there should be an "Unload" option in the context menu for the project)
Yordan Pavlov
@Yordan: or just put your customizations into a separate .build file (or whatever) and import+execute that from your .csproj file. Then you only have to edit the .csproj once, and can add the .build file to your solution. Double-click and you're editing like any other file. Can map .build files to XML editor in your options.
Kent Boogaart
There exists an MSBuild CCNet task - The details can be found at: http://confluence.public.thoughtworks.org/display/CCNET/MsBuild+Task
Gavin Miller
@Kent that's a good tip!
Dave Van den Eynde
It is worth noting that you do not need to modify the .csproj files themselves. You can use a MyProject.csproj.user file to do those modifications, leaving your .csproj file clean and pristine. MSBuild knows to look for those .user files and will import them into the build process automatically. See: http://ahmed0192.blogspot.com/2006/11/customize-msbuild-without-modifying.html
Clever Human
Mono now supports MSbuild
Sharique
+7  A: 

Nant has more features out of the box, but MSBuild has a much better fundamental structure (item metadata rocks) which makes it much easier to build reusable MSBuild scripts.

MSBuild takes a while to understand, but once you do it's very nice.

Learning materials:

Koistya Navin
Hey, I've heard of those books :)
Sayed Ibrahim Hashimi
+1  A: 

I ended up using both. When redesigning our build system, I was facing a tricky problem. Namely, I couldn't get rid of .vcproj (and family) because we everybody was using VS to update the project files, settings, and configurations. So without a huge duplication and error prone process, we couldn't base our build system on a new set of files.

For this reason, I decided to keep the 'proj' files of VS and use MSBuild (they are MSBuild files, at least VS2005 and VS2008 use MSBuild project files). For everything else (custom configuration, unit testing, packaging, preparing documentation...) I used NAnt.

For continuous integration, I used CruiseControl. So we had CC scripts that triggered NAnt jobs, which for building used MSBuild.

One final note: MSBuild does NOT support Setup projects! So you're stuck with calling DevEnv.com or using Visual Studio directly. That's what I ended up doing, but I disabled the setup project by default from all solution configurations, since developers wouldn't normally need to build them, and if they do, they can manually select to build them.

Ash
+3  A: 

KISS = Use MSBuild.

Why add something else into the mix when you have something that will do a reasonable job out of the box. When msbuild arived, nant died. And Mono will have a msbuild implementation (xbuild).

DRY = Use MSBuild.

Ask yourself what do you want out of a build system? I want a build system that is also used by my ide rather than the maintaining two different configs.

Personally, I'd love to hear some real arguments for NAnt, because I just can't think of any that really hold water.

Squirrel
"When msbuild arived, nant died" -- I think this is the clincher, even without VS (which I don't use).
harpo
+5  A: 

I use both in that my NANT scripts call MSBuild. My main reason for staying with NANT is isolation. Let me explain why I feel this is important:

  1. Adding dependencies to your project. The NANT build file is alien to VS (in my case I consider this a pro) so VS does not attempt to do anything with it. MSBuild tasks are embedded so part of the solution and can refer to other MSBuild tasks. I've received source code from someone else only to find out I could not build because the MSBuild community tasks were not installed. What I find particularly frustrating is that VS just would not build and threw a bunch of errors that made me loose time debugging. This, despite the fact that the build being requested could have gone ahead (as a debug build for instance) without some of the extras of the MSBuild task. In short: I don't like adding dependencies to my project if I can avoid it.

  2. I don't trust VS as far as I could throw its dev team. This stems back to the early days of VS when it would massacre my HTML. I still do not use the designer for instance (at a conference recently I found colleagues did the same). I have found that VS can screw up dependencies and version nrs in the dll (I cannot replicate this but it did happen in a project consistently and caused a lot of grief and lost time). I have resorted to a build procedure that uses VS to build in debug mode only. For production I use NANT so that I control everything externally. VS just cannot interfere any longer if I build using NANT.

PS I'm a web developer. No Winforms development.

+1  A: 

The documentation and tutorials available for Nant make it easier to begin learning build scripts with Nant. Once I got the hang of Nant and creating build scripts I started translating that knowledge to MSBuild (I did X in Nant, how do I do X in MSBuild?). Microsoft's documentation usually assumes a pretty high level of knowledge before it is useful.

The reason for switching from Nant to MSBuild is because MSBuild is more current. Unfortunately the last release of Nant was in December 8 2007, while msbuild 4.0 (.net 4.0) isn't far off. It looks like the Nant project has died.

If you find good documentation for someone just beginning to learn creating build scripts using MSBuild, then skip Nant and go straight for MSBuild. If Nant ever comes out with a new release then I'd consider sticking with Nant, but they're lagging behind now.

mcdon
A: 

I like using NAnt with NContrib. Beats MSBUILD.

Richard N.
+4  A: 

One thing I noticed several posters mention was having to hand edit the .csproj (or.vbproj, etc...) files.

MSBuild allows customization of these .*proj files through the use of .user files. If I have a project named MyCreativelyNamedProject.csproj and want to customize the MSBuild tasks inside of it, I can create a file named MyCreativelyNamedProject.csproj.user and use the CustomBeforeMicrosoftCommonTargets and CustomAfterMicrosoftCommonTargets to customize those files.

Also, both NAnt and MSBuild can be customized to the heart's content through custom MSBuild tasks and through NantContrib extensions.

So, using NAnt or MSBuild really comes down to familiarity:

  • If you are already familiar with Ant, use NAnt, the learning curve will be very easy.
  • If you are not familiar with either tool, MSBuild is integrated with Visual Studio already and requires no additional tools.

It is also worth adding that MSBuild is pretty much guaranteed to work with all new versions of .Net and Visual Studio as soon as they are released, whereas NAnt may have some lag.

Clever Human
+1 for pointing out lag between .net releases and nant releases. I remember when .net 3.5 came out and having to use a hacked nant.exe.config from out on 'nets to get things working. Not fun.
mmacaulay
A: 

We use both - nAnt is responsible for all "scripting" stuff, like copying, deploying on IIS, creating packages and MSBuild is responsible for building solution. That we can avoid problems with non supported .Net 4.0 by new version of nAnt. nAnt is also more scalable, if we want to migrate the deployment scripts to Production servers, we only copy build file and install proper version of .Net, no VS's, problems with csproj files:)

Leszek Wachowicz