Farhan Nasim
Farhan Nasim
2 min read

Categories

Tags

NuGet packages are usually packaged with separate scripts after building is done and artifacts are ready. With MSBuild, however, it is posible to make packaging part of the build process: you have your package published on each successful build. This is done by making NuGet’s packaging functionality one of the project’s custom build targets.

To put in short, build targets are group of operations that MSBuild performs as part of the build process; they are defined in XML in the project file. A project file may consist of several targets, they may contain various activities, their order of execution may be specified, can be conditional, etc. Each time you create a new project in Visual Studio, you have some default targets, required for basic build operation, automatically generated for you. By defining custom targets, users can extend MSBuild’s functionality in diverse ways. NAnt, another major .NET build tool, uses the concept of targets in the same sense. To learn more about MSBuild and targets see Further Reading 1, 2, and 3 at the end of this article.

In order to add a custom target, your project file, that is your project’s .csproj file, needs to be modified; it can’t be done in Visual Studio while corresponding project is open in it: either from solution explorer first unload the project and choose Edit ProjectName.csproj from context menu (as the following figure shows) or do it outside with an editor while project is not open in Visual Studio.

edit-project-file

MSBuild project files are XML configuration files. To add a custom target, just add an XML element called Target with a Name attribute and add operations to be performed within it. Just adding a custom target doesn’t have it executed during build; though there are other ways to do it, for simplicity we will add it to the Project element’s DefaultTargets list to have it executed.

Assuming NuGet manifest file—.nuspec file—exists in the project directory, modifiy your project file to something like the following:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" InitialTargets="Build" DefaultTargets=" NuGetPack" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!--Project properties automatically 
    generated by IDE -->
  <Target Name="NuGetPack">
    <Exec Command="nuget pack YourProjectName.csproj" />
  </Target>
</Project>

Notice that I defined a target called NuGetPack on line 5 and inside it called the nuget pack command; easy to infer that any command can be added in targes the same way. Also note on line 2 that the target NuGetPack is added to the project’s DefaultTargets list; in case you had more than one default targets you would separate them with semicolons. Henceforth, on each subsequent successful build, you are supposed to get your .nupkg file ready in your project folder.

Release build artifacts are usually distributed as package; you may want your packaging target conditional on Release build. Edit line 5 as follows to make target NuGetPack conditional on release build:

  <Target Name="NuGetPack" Condition=" '$(Configuration)' == 'Release'">

The change above makes your package published only on successful release builds.

Further Reading

  1. MSBuild Targets at MSDN.
  2. How to: Specify Which Target to Build First at MSDN.
  3. MSBuild at MSDN.
  4. NuGet Primer in this pages.