Have a NuGet package output a file

不想你离开。 提交于 2021-02-07 10:36:33

问题


I have a Visual Studio project that I have made into a NuGet package. The project has a file in it (called SwaggerIndex.html). I have set that file to have the "Copy to Output Directory" property to "Copy if newer". (I need this file to copy to the build folder on build.)

In that solution it works just fine. (I created another project to test it, and when it references my project that has the SwaggerIndex.html, it outputs it to the build folder just fine.)

However, when I package it into a NuGet and pull it into another solution/project, the SwaggerIndex.html file is not output on build.

I am making my NuGet package by going to the project's "Package" properties tab and selecting "Generate NuGet package on build". All projects involved are running .Net Core 3.1.

How can I get my NuGet Package to create my SwaggerIndex.html file on build (like it does when it is just a normal project)?


回答1:


Please try these:

I have two solutions for you to solve the issue.

Solution 1

If you just want to install this nuget package only in new sdk projects(Net Core and Net Standard projects), you could use this

1) add these node in your xxx.csproj file:

<ItemGroup>
    <None Include="xxx\SwaggerIndex.html(the path of SwaggerIndex.html file in your project)" Pack="true" PackagePath="contentFiles\any\any">
        <PackageCopyToOutput>true</PackageCopyToOutput>
    </None>
</ItemGroup>

2) then repack your nuget project, and before you install the new version, you should first clean nuget caches first or just delete all cache files under C:\Users\xxx(current user)\.nuget\packages

Solution 2

If you want this nuget package to copy SwaggerIndex.html file in both Net Framework and Net Core projects, you should use this:

1) add a file called <package_id>.props file in your nuget project. You should note that if your nuget package named as SwaggerIndex.1.0.0.nupkg, you should name the file as SwaggerIndex.props file so that it will work.

In my side, I put it into a folder called build.

2) then add these content in SwaggerIndex.props file,

<Project>
    <Target Name="CopyToOutputFolder" BeforeTargets="Build">
        <ItemGroup>
            <File Include="$(MSBuildThisFileDirectory)..\File\*.*"></File>
        </ItemGroup>
        <Copy SourceFiles="@(File)" DestinationFolder="$(TargetDir)"></Copy>
    </Target>
</Project>

3) add these in xxx.csproj file of your nuget project.

<ItemGroup>
        <None Include="SwaggerIndex.html" Pack="true" PackagePath="File"></None>
        <None Include="build\SwaggerIndex.props" Pack="true" PackagePath="build"></None>   
</ItemGroup>

4) then repack your nuget package, before installing this new version of the nuget package, you should first clean all nuget caches first.

When you finishing installing the new version of the nuget package, click Build and the file will be copied into the main project.

Besides, there is also a similar issue about this.



来源:https://stackoverflow.com/questions/64364415/have-a-nuget-package-output-a-file

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