Search code examples
c#visual-studionugetproject-referencepackagereference

A Nuget package copies the static files from its reference project when installed


I have a project called "A" project, and it contain some static files (like dll or others).

And it's csproj is as follow

<ItemGroup>
    <ContentWithTargetPath Include="files\MyDll.dll">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        <TargetPath>MyDll.dll</TargetPath>
    </ContentWithTargetPath>
</ItemGroup>

<ItemGroup>
    <Content Include="files\MyDll.dll">
        <PackageCopyToOutput>true</PackageCopyToOutput>
        <PackagePath>contentFiles\any\any\;content\any\any</PackagePath>
    </Content>
</ItemGroup>

So according to the content, no mater I use project reference or package reference to refer A, the static files will always be copied to output folder.

Then I have a project called "B", and it uses project reference to refer A.

The part of the csproj of B is as follow:

<ItemGroup>
    <ProjectReference Include="..\..\A\A.csproj"/>
</ItemGroup>

Now I make B be a Nuget package and be referred by C project (Package Reference).

But the static files from A seems not appear in the output folder of C.

Does have any way to deal with it?

Thanks!


Solution

  • The contentFiles node cannot work from a nuget dependency A into the main project C. And this is quite an issue under my test several times.

    It seems that contentfFiles node cannot transfer dependencies from nuget to the main project.

    One of the workaround is to install the nuget A into Project C additionally.

    ==========================

    And the workaround might not be so convenient or you could add these on Project B so that we should not install nuget A additionally:

    1) add these on Project B.csproj file:

    <ItemGroup>
            <None Include="$(TargetDir)\MyDll.dll">
                <Pack>true</Pack>
                <PackagePath>contentFiles\any\any\;content\any\any</PackagePath>
                <PackageCopyToOutput>true</PackageCopyToOutput>         
            </None>     
    </ItemGroup>
    

    Then, repack your project B.