How to use dotnet pack selectively on a solution

Viewed 8618

I have a solution foo.sln. All libraries within are SDK projects. However only one of them should be packed

By default

dotnet pack

tries to pack all projects. There is no exclude filter or include filter for that matter. What is the recommended process?

1 Answers

You select what project to pack as nuget package by setting property inside of csproj file, like this:

<IsPackable>true</IsPackable> - create package

<IsPackable>false</IsPackable> - don't create package

If you don't want to specify it in each file you can create a text file named Directory.Build.props in the directory with the following content:

<Project>
  <PropertyGroup>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
</Project>

It would be automatically included in all on the beginning of the SDK project file (csproj) in this and all nested folders, so you can specify common default values for this group of projects and if needed they could be overridden in individual csproj files.

If you don't want them to be overwritten or need to use some values defined in csproj, you should use file name Directory.Build.targets, that would be automatically included at the end of csproj.

In our projects we use the following structure:

 \
   \src
      <actual projects>
      Directory.Build.props
   \tests
      <unit tests>
      Directory.Build.props
   Directory.Build.props
   MySolution.sln

In this way, we are able to specify different common properties for test and regular projects.

One note on this is that by default only first Directory.Build.propsthat found by csproj would be applied, to change this behavior you need to add this line in the beginning of nested Directory.Build.props files (inside of Project tag):

  <Import Project="$([MSBuild]::GetPathOfFileAbove('$(_DirectoryBuildPropsFile)', '$(MSBuildThisFileDirectory)../'))" />

More about all of this could be found here: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2017

Related