Search code examples
xmlnuget

Add custom files to the Nuget package so that they will appear in root directory?


I'm using NuGet Version: 4.9.4.5839 .

I've already created a nuget package which I pack & push via those commands :

  • dotnet pack myproj.csproj -c Release -o .
  • nuget push myproj.1.0.0.nupkg <key> ....

All Ok.

I'm able to consume that nuget package in my other projects.

But now I want to add custom files to be included .

So if someone fetches the nuget , I want that a the following directory and files will be included at the consumer's solution :

This is what I've tried:

I've created a folder and a file:

enter image description here

I've also created the aaa-health-check-net-core.nuspec file :

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2019</copyright>
    <tags>Tag1 Tag2</tags>
    <contentFiles>
      <files include="any/any/*.txt" buildAction="none" copyToOutput="true"/>
    </contentFiles>
  </metadata>

  <files>
    <file src="aaa\*.txt" target="contentFiles\any\any" />
  </files>
</package>

Ok let's pack and push .

As you can see , I do see an update. and I click it.

enter image description here

The result is :

enter image description here

Question:

I don't want the custom file to appear where it currently appear.
I want it to appear at the root of the project

How can I do that ?


Solution

  • You can't*.

    Your project uses the new SDK style project, which only supports NuGet via PackageReference, and PackageReference packages can only affect build output, not project files.

    A project using a traditional project file, and reference NuGet packages with packages.config, will copy files in the content directory of the package into the project on package install (but not on restore, so those files need to be checked into source control). But SDK style projects don't support packages.config.

    *if you're feeling a little bit evil, you could include MSBuild targets which creates/copies/modifies files in the project when the target runs. But there's no target that runs on install (and even if there was, people not using Visual Studio and editing the project file with a text editor wouldn't trigger the target anyway), so the most likely target is to run on build. Say the person using your package doesn't want your text file in their project, so they delete it, but then every time they build the project, it gets recreated.

    Also FYI, the dotnet cli supports pushing with dotnet nuget push, so you may not need to download nuget.exe just to push.