Using the Copy target in the target file to copy the required libraries will not copy these files to other projects that reference the project, resulting in a DllNotFoundException . This can be done with a much simpler target file using the None element, since MSBuild will copy all the None files into project links.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <NativeLibs Include="$(MSBuildThisFileDirectory)**\*.dll" /> <None Include="@(NativeLibs)"> <Link>%(RecursiveDir)%(FileName)%(Extension)</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup> </Project>
Add the target file to the build directory of the nuget package along with the required native libraries. The targets file will contain all dll files in all child directories of the build directory. Therefore, to add the x86 and x64 versions of the native library used by the assembler-controlled Any CPU , you will get a directory structure similar to the following:
- assembly
- x86
- NativeLib.dll
- NativeLibDependency.dll
- 64
- NativeLib.dll
- NativeLibDependency.dll
- MyNugetPackageID.targets
- Lib
The same x86 and x64 directories will be created in the project output directory during construction. If you do not need subdirectories, then you can delete ** and %(RecursiveDir) and instead include the necessary files in the build directory. Other required content files can also be added in the same way.
Files added as None to the targets file will not appear in the project when opened in Visual Studio. If you are wondering why I am not using the Content folder in nupkg, because there is no way to set the CopyToOutputDirectory element without using the powershell script (which will only be run inside Visual Studio, and not from the command line, on build servers or in other IDEs and is not supported in project.json / xproj DNX ), and I prefer to use Link for files rather than have an extra copy of the files in the project.
Update: Although this should also work with Content , not None , an error appears in msbuild, so the files will not be copied for links to projects deleted by more than one step (for example, proj1 → proj2 → proj3, proj3 won’t get the files from the proj1 NuGet package, but proj2 will be).
kjbartel May 19 '15 at 4:46 a.m. 2015-05-19 04:46
source share