Monday, November 7, 2016

Delivering files using NuGet 2 so they're copied to the output directory

I recently had a situation where a NuGet package I was creating had to deliver some supporting DLLs which needed to be copied to the output directory (actually into a sub-directory named /x86). We're using NuGet 2 at work because our internal NuGet feed is managed via ProGet, which doesn't yet support NuGet 3.x. While NuGet 3.3+ gives the copyToOutput option to have content files delivered to the output directory, there's no such support in NuGet 2. In researching how to do this I came across two very helpful posts on StackOverflow which gave me the direction I needed. The posts were and

The approach I took was to have the files I needed to distribute be included in the NuGet package in the /build folder, as opposed to /content. In fact, I put them in a sub-folder named /x86 which was the desired target location in the output directory. I then used an MSBuild targets file to mark all the files in that sub-folder as always needing to be copied to the output directory. I had not worked with MSBuild targets (or properties) files before - so this approach was totally new to me.

This blog post seeks to expand on using a MSBuild targets file in a NuGet package. The NuGet documentation talks about this technique - but I thought a more step-by-step set of instructions might help others.

The sample code I'm providing in this post is a little different from what I did at work in that there's no sub-folder created in the target directory. I am using a sub-folder in the NuGet package's /build folder, though, so don't have to act on each file individually.

The sample scenario

The contrived example for this post is to create a package which delivers files to the output directory of any project that installs my NeededFiles package; an image and a readme file.

What does an MSBuild targets file do?

If you were to open a Visual Studio project file (e.g. csproj or vbproj) in a text editor you would see it's an XML format which defines properties and actions (i.e. "targets"). I found it pretty similar to a NANT script. What a targets file lets us do is define a target that can be imported into the project file later. The NuGet documentation explains that when a package includes a targets file with the same name as the package in its /build folder the NuGet installation will add an import element to the project file.

What's in the NeededFiles package?

The NeededFiles.nuspec file looks like this:
1. <?xml version="1.0"?>
2. <package >
3.   <metadata>
4.     <id>NeededFiles</id>
5.     <version>1.0.0</version>
6.     <authors>@Rob_Hale_VT</authors>
7.     <requireLicenseAcceptance>false</requireLicenseAcceptance>
8.     <description>
9.   An example of delivering files to the consuming project's output via build targets
10.  </description>
11.     <releaseNotes>Created</releaseNotes>
12.     <copyright>Copyright 2016</copyright>
13.     <tags>Demo</tags>
14.   </metadata>
15.   <files>
16.  <!-- In this instance, the file(s) to be delivered are in the sub-folder named "howdy" -->
17.  <file src="howdy\*.*" target="build\howdy" />
19.  <!-- The targets file must have the same name as this package -->
20.  <!-- More info: -->
21.  <file src="NeededFiles.targets" target="build" />
22 .  </files>
23. </package>

The key lines for what I'm doing are 15 - 22, where the files element is defined. On line 17 I'm added all the contents of a folder called "howdy" to the /build/howdy target folder. This folder contains the image and readme file I want to have delivered

Additionally, line 21 adds the NeededFiles.targets file to the /build target folder.

What does the NeededFiles.targets file do?

Now that the NeededFiles package includes the /build/NeededFiles.targets file we can take a look at what it does.
1. <Project xmlns="">
2.   <ItemGroup>
3.     <NativeLibs Include="$(MSBuildThisFileDirectory)howdy\*.*" />
4.     <None Include="@(NativeLibs)">
5.       <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
6.       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
7.     </None>
8.   </ItemGroup>
9. </Project>

This file is imported by the consuming project. This means when the project is built line 3 declares an element named NativeLibs which is comprised of the contents of the howdy folder (delivered as part of the NeededFiles package). Lines 4 - 7 are acting like a loop, instructing MSBuild to mark each file in NativeLibs so they are always copied to the output directory.

If I wanted the files to all appear in a sub-folder of the output directory I could modify line 5 to be:


At this point the package will deliver the contents of the Howdy folder to the output directory of any project that installs the NeededFiles package. Unlike files marked as content, the files in the build target folder don't even appear in the Solution Explorer in Visual Studio. "It just works."

The source for my contrived example, NeededFiles, can be found at

Thursday, March 31, 2016

Seven year navel gazing...

The first post of this blog documented that I was preparing to start a new job. That was December of 2008. Seven years seems like a long ago.

On Monday I start another new job.

So I feel like this is a good opportunity to reflect on what these past seven years has meant to me and my career. A chance to thank the universe for what I've experienced, to thank the folks I've met, and to recognize where I think I'm going.

The month after that initial post I started working at MyWebGrocer. I had been hired to work on the eCommerce product and introduce some improvements to the development processes. I had played a similar role at my prior employer by implementing an automated build system and a few other bits of tooling. At MyWebGrocer, however, I was able to expand on that prior work. I led the introduction of a continuous integration server, static code analysis, automated unit testing, some scrum practices, and more. The team I was on kicked ass; our product was pretty stable, we were delivering new functionality, refactoring to reduce complexity and lines of code, and enjoying each others' company. After a few years I took a management position. While I eventually resumed my technical career - for those two years I had the privilege of managing the best team with whom I've had the pleasure of working. When I decided to leave management the MWG executive team was supportive; allowing me to resume my technical career without leaving the company. Not every employer would have done that; I'm appreciative.

My tenure at MWG coincides with my increased participation in the development community around Burlington, VT. I increased my involved with the VT .NET user group which led to co-founding the VT Code Camp. When MyWebGrocer founded the HackVT event they invited me to be on the planning team. I've continued to serve on the organizing team for HackVT. I've met amazing members of our technical community, too. Organizers of other local user groups, students, presenters, sponsors, employers, journalists, writers, etc. My participation in this community will continue regardless of where I work, and I'm confident that MWG's support of the local development community will continue, too. They've been reliable sponsors for VT Code Camp, HackVT, and other local development events.

As I transition into my next role MWG is six times larger than on my first day. I look at how the development team works and I see my fingerprints on a lot of it. It's not been a perfect experience. I've made mistakes and there are elements of what I would have like to have contributed to the MWG experience which are unfulfilled. This is the real world, after all. But these past seven years have (so far) been the highlight of my professional career.

Come Monday, when I start at Renewable NRG Systems I will be a significantly better, and more well rounded, developer than I was seven years ago. I'm eager to dive into a different industry for an organization which has a different corporate culture. New people, new knowledge - it's going to be great!

So thank you to the MWG team for helping me grow professionally. And for your friendships. Thank you to RNRG for the opportunity to challenge myself in an industry I feel is important.

Thanks universe.