2

I am building a nuget package from a c# class library project.

The class library itself has files that are marked as build action=content and I can't change this.

I don't want these content files in the nuget package. If I am using an empty files tag, these content files are not included.

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    ...
  </metadata>
  <files />
</package>

However, if I add at least on other file to my package like this

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    ...
  </metadata>
  <files>
    <file src="source/path.dll" target="lib\net45\" />
  </files>
</package>

every file marked as content in my project is included in my package, too.

Is there a way to avoid this?

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • 1
    If you really can't change the build action of the files (why not, out of interest?) then you'll probably have to define the whole nuget package by hand in the config instead of building it from the csproj file. Otherwise it'll follow the build rules of the project. – ADyson Feb 15 '18 at 10:44
  • @ADyson `why not, out of interest` Content files are just by setup of the main exe to determine which files to include and by unit tests. They are not needed for the package. It's strange that I can't find any reference in the official docs that content files are included in the first place. – Jürgen Steinblock Feb 15 '18 at 16:42
  • Can you please post your full nuspec? Editing out PII data ofc. Not clear what you mean here. What do you mean by content? The content concept in NuGet? $root/content/cs/net45/mycontent ? – imps Feb 16 '18 at 00:43
  • 1
    @imps I am building a nuget package from a csproj file in combination with a nuspec file for metadata, dependencies and additional files., see https://stackoverflow.com/a/14808085/98491 Files marked as `Content` in Visual Studio are included in the nuget `content` directory. I want to disable that, which is possible with and empty `` tag but still add other files that I define explicitly. – Jürgen Steinblock Feb 16 '18 at 07:13
  • When you build a nuget from a csproj it automatically includes whatever would be included in the build of that project. So probably that's documented along with MSBuild or something, but that's the behaviour I've observed having built quite a lot of packages this way. If your content files are only for testing, then presumably you can exclude them from a build when in "Release" mode (or you could create another build configuration specially for your package) and then create the package from the Release (or custom) build? – ADyson Feb 16 '18 at 07:59
  • See https://stackoverflow.com/questions/29154483/include-exclude-files-per-visual-studio-configuration for how to do that – ADyson Feb 16 '18 at 08:00

1 Answers1

0

Put this to the end of the .csproj file:

<ItemGroup>
  <Content Update="@(Content)" Pack="false" />
</ItemGroup>

This will make every "build action=content" get excluded from the nuget.
Those "build action=content" entries will be actually <Content> Items in the MsBuild project - you could see them in the old .csproj format. (In the new SDK style format, by default, the project files are automatically included based on file extension; so probably you won't see the <Content> entries).

The code above updates every <Content> entry - which already exist when reaching that code (thus it should be at the end of the .csproj) - to be excluded from nuget pack

nvirth
  • 1,599
  • 1
  • 13
  • 21