jpierson
jpierson

Reputation: 17374

How to reference an assembly installed via NuGet package in custom MSBuild task?

I have a custom MSBuild target that I've created in which I would like to make use of custom task implemented within an assembly that is pulled into any project via NuGet. The targets file is also delivered via the same NuGet package under the build folder as per the NuGet convention for custom targets and props. Below is how I attempted to resolve the DLL for the custom targets assembly.

<UsingTask 
    TaskName="TfsVersionTask"
    AssemblyFile="$(SolutionDir)\packages\MyPackage.1.0.0.0\lib\MyProjectReferencedAssembly.dll"/>

The problem is that if I use $(ProjectDir) or $(SolutionDir) that I'm making assumptions of whether the project is being built as part of a solution or whether the packages folder may exist. Are there any alternatives such as a variable along the lines of $(NuGetPackagesDir)?

Upvotes: 1

Views: 660

Answers (1)

jpierson
jpierson

Reputation: 17374

I've found a reasonable solution that is at least working for me initially almost immediately after I posted the question by using a relative directory lookup.

<UsingTask 
    TaskName="TfsVersionTask"
    AssemblyFile="..\lib\net40\MyProjectReferencedAssembly.dll"/>

This works by assuming that my targets are resolving to the build directory under my packages directory and using relative lookup from there to the lib folder where my packaged assemblies exist. There may be some unforeseen drawbacks to this approach or a better more conventional approach but this seems to work for now.

Upvotes: 1

Related Questions