Marnix
Marnix

Reputation: 6547

Use MSBuild Targets to copy files recursively from NuGet package

I have a .targets file in my NuGet package build folder which is then automatically included in the project that consumes the NuGet package.

I want this .targets file to copy some folders on post-build. The following script shows how that is done, but the output I get is wrong because %(RecursiveDir) starts at the first wild-card which I used for the version number of the package.

My question: How can I specify the version of MyPackage in the .targets file dynamically so that I can remove the first wildcard?

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="AfterBuild">
    <ItemGroup>
      <!-- MyPackage.* should be replaced by MyPackage.1.0.0.4534. But the version is set by NuGet.exe pack -Version -->
      <FilesToCopy Include="$(SolutionDir)packages\MyPackage.*\myfolder\**\*.*"/>
    </ItemGroup>
    <Copy SourceFiles="@(FilesToCopy)" DestinationFolder="$(SolutionDir)bin\$(Configuration)\myfolder\%(RecursiveDir)"/>
  </Target>
</Project>

Upvotes: 2

Views: 3244

Answers (1)

Matt Ward
Matt Ward

Reputation: 47987

To avoid having to the package version you could use the MSBuildThisFileDirectory property instead.

The MSBuildThisFileDirectory property gives you the directory where the .targets file is so you can change the <FilesToCopy> element to use a path relative to that directory and you do not have to use the version number.

<FilesToCopy Include="$(MSBuildThisFileDirectory)myfolder\**\*.*"/>

Upvotes: 2

Related Questions