How to create a NuGet package with MSBuild tasks and a targets file

bloudraak picture bloudraak · Jun 24, 2014 · Viewed 7.9k times · Source

I'm using NuGet 2.8.50506.491. I created a couple of C# projects with custom Microsoft Build tasks and helper functions. There is a targets file. It looks something like this.

<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>MyTasks</title>
    <authors>Werner Strydom</authors>
    <owners>Werner Strydom</owners>
    <licenseUrl>http://example.org/LICENSE</licenseUrl>
    <projectUrl>http://example.org/</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Sample tasks.</description>
    <releaseNotes>Initial Release</releaseNotes>
    <copyright>Copyright 2014 Werner Strydom</copyright>
  </metadata>
  <files>
    <file src="MyTasks.targets" target="build/net40" />
    <file src="bin\$configuration$\*.dll" target="build/net40" />
    <file src="bin\$configuration$\*.pdb" target="build/net40" />
  </files>
</package>

In the Visual Studio project files, I set the BuildPackage property to true

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{96535580-0000-0000-0000-2843D7EC2767}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>MyTasks</RootNamespace>
    <AssemblyName>MyTasks</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <BuildPackage Condition="'$(BuildPackage)' == ''">true</BuildPackage>
</PropertyGroup>

When I built the project, a nupkg is created. I changed the extension to zip and looked at the contents of the file. The outputs of the project is in the lib/net40 folder. So when I add this package to another project, it also adds references to those assemblies, which is not desired, since this package is purely for used for builds.

How do I prevent the project outputs from ending up in the lib folder?

Answer

Matt Ward picture Matt Ward · Jun 26, 2014

The BuildPackage property is defined in the NuGet.targets file. Looking at the NuGet.targets file when BuildPackage is true the following command line is run:

NuGet.exe pack YourProject.csproj

If you are using NuGet.exe pack YourProject.csproj then the output assembly of your project is always added to the lib directory. Looking at the NuGet source code you cannot change this behaviour, at least not without modifying NuGet.

Whilst using NuGet pack YourProject.csproj will use any YourProject.nuspec file it finds for extra metadata that you cannot define in the project itself, it seems that you cannot stop your output assembly from getting added to the lib directory.

The alternative will be to use just the .nuspec file when creating the package. With the .nuspec file you have full control over what goes into the NuGet package.

NuGet.exe pack YourManifest.nuspec