The content folder in my nuget package isn't a

2019-05-14 02:46发布

问题:

I am running .net core 1.1 and nuget 3.5.

Here is my nuspec:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>ClassLibrary1</id>
    <version>1.0.0.4</version>
    <title>Class Library Test Content</title>
    <authors>name</authors>
    <owners>name</owners>        
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>package to test config file transfer</description>
    <copyright>Copyright 2017</copyright>
    <tags>TagUno TagDos</tags>
    <contentFiles>
      <files include="appsettings.json" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>

  <files>
    <file src="bin\Debug\net462\classlibrary1.dll" target="lib\net462\classlibrary1.dll" />
    <file src="appsettings.json" target="content\net462\appsettings.json" />
  </files>

</package>

It produces this package:

As we can see, the appsettings.json file is located in the content folder in the nuget package.

However, when I install the package to my project, the appsettings.json file is not copied to the project root.

What am I missing? Or is this done differently in .net core?

回答1:

The content subdirectory is only used for packages.config projects.

Projects using the PackageReference style of NuGet references use the contentFiles section instead which allows to specify the build action to use. Those files aren't copied to the project, but are included as items in the project and optionally set to copy to the consuming project's build output:

<package>
  <metadata>
    ...
    <contentFiles>
        <files include="appsettings.json" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>...</files>
</package>

See the contentFiles documentation for more detail.

Note that those files aren't copied to the project but included logically. Only packages.config projects support this since there was no other way to contribute files - the documentation also states that these files should have been considered immutable anyway.