2

I have the NuGet package which managed library depends on a native library. I want to copy native dependency to the \\UnmanagedLibraries\Windows\x86\ folder and I have made the following file which is copied to the build folder:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="CopyNativeReferences">

    <Message Text="$(MSBuildThisFileDirectory) => $(OutDir)\UnmanagedLibraries\Windows\x86\"></Message>    

     <Copy SourceFiles="$(MSBuildThisFileDirectory)..\native\UnmanagedLibraries\Windows\x86\SIMDArrayInstructions.dll" 
      DestinationFolder="$(OutDir)\UnmanagedLibraries\Windows\x86\" 
      SkipUnchangedFiles="true" />

    </Target>

   <PropertyGroup>
     <AfterBuildDependsOn>
        CopyNativeReferences;
     </AfterBuildDependsOn>
    </PropertyGroup>

    <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildDependsOn)"/>
</Project>

It runs fine for one package, but I have multiple NuGet packages that need to copy their native references. After I install the second package, the native references for the first package are not copied. It seems that 'AfterBuild' is overwritten with the new package.

I also changed target name and AfterBuildDependsOn to ensure that other packages do not share same names of target variables but it did not help.

How can I resolve this?

JoshDM
  • 4,939
  • 7
  • 43
  • 72
dajuric
  • 2,373
  • 2
  • 20
  • 43
  • 1
    A better approach is to use [`Content` rather than `Copy`](http://stackoverflow.com/a/30316946/1730559). – kjbartel May 19 '15 at 05:02

1 Answers1

3

The defined property group should look like:

PropertyGroup>
 <AfterBuildDependsOn>
    $(AfterBuildDependsOn);
    CopyNativeReferences;
 </AfterBuildDependsOn>
</PropertyGroup>
dajuric
  • 2,373
  • 2
  • 20
  • 43
  • we have noticed that our target is not getting called sometimes, haven't been able to replicate but it is happening often even though nothing has chnaged between runs, have you had similar issues? – Bek Raupov Jul 16 '15 at 14:56
  • 1
    We ran into problems with it sometimes not running on our build environment. Turned out it was related to how we did package restores. If you use a MSBuild call to download/restore packages, the targets files won't execute that run (Imports have already ran, so MSBuild won't see the new file). We got around it by adding a step to restore packages _before_ MSBuild gets invoked. The new TFS templates do this automatically, but we were on old ones at the time. – ComradeCow Jan 14 '16 at 18:42