Is there an alternative to contentFiles with projects that use packages.config?

夙愿已清 提交于 2021-02-04 19:51:05

问题


I have a nuget package with content that I want to be copied to the build output when users install my package. There is support for this: NuGet ContentFiles Demystified in NuGet v3.3. However, it only works in projects that use project.json. The contentFiles are not copied to my build output when I have a project that uses packages.config.

Is there an alternative or workaround I could use in order to make my NuGet package work on projects that use either a project.json or packages.config?


回答1:


A quick search on StackOverflow reveals the following question which I think covers what you are asking for:

Set content files to "copy local : always" in a nuget package

You can put your files inside a Content directory inside the NuGet package.

In your .nuspec file:

<file src="css\mobile\*.css" target="content\css\mobile" />

When you install that into your project it will add the css\mobile directory to your project and the files inside that directory.

However that only adds the files to the project. In order to get them to be copied to your output directory you would either need to use a PowerShell script to modify the project item's copy local information.

An alternative, possibly a better way, would be to use a custom MSBuild .targets file. This will be added as an import to your project and then inside your .targets file you can add the files you want and specify the copy to output information directly as though it was part of your project. NuGet .nupkg file content:

\build
    \Net45
        \MyPackage.targets
        \Foo.txt

MyPackage is the id of the NuGet package above.

Inside the .targets file you specify the files (e.g. Foo.txt).

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="Foo.txt">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>


来源:https://stackoverflow.com/questions/37493187/is-there-an-alternative-to-contentfiles-with-projects-that-use-packages-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!