如何配置.Net Standard项目csproj将文件纳入NuGet build层级?或引用nuspec?
Great question! You have two solid options here—either configure your .csproj directly (the modern, recommended approach) or reference your existing .nuspec file. Let's break both down:
Option 1: Include files in the "build" layer via .csproj (No nuspec needed)
You don't need a separate nuspec to add files to the build folder of your NuGet package. MSBuild in .NET Core+ lets you define package contents directly in your project file using item groups.
Add this block inside your <Project> element:
<ItemGroup> <!-- Include all files from your Libs directory into the package's "build" folder --> <None Include="Libs\*.*" Pack="true" PackagePath="build" /> <!-- Optional: If you need framework-specific build files, use targeted paths --> <!-- <None Include="Libs\net462\*.*" Pack="true" PackagePath="build\net462" /> --> <!-- <None Include="Libs\netstandard2.0\*.*" Pack="true" PackagePath="build\netstandard2.0" /> --> </ItemGroup>
Pack="true"tells thedotnet packcommand to include these files in the package.PackagePath="build"maps directly to thetarget="build"setting in your nuspec example.
Run your usual dotnet pack MyProject.csproj command, and your Libs files will be placed in the build directory of the generated NuGet package.
Option 2: Reference your existing nuspec from .csproj
If you want to keep using your pre-written nuspec file, you can configure the csproj to use it instead of auto-generating a package manifest.
Add this property inside a <PropertyGroup> in your .csproj:
<PropertyGroup> <!-- Path to your custom nuspec file (adjust the path if it's in a different location) --> <NuspecFile>MyProject.nuspec</NuspecFile> <!-- Optional: Pass csproj values (like build configuration) to the nuspec --> <!-- <NuspecProperties>Configuration=$(Configuration);Version=$(Version)</NuspecProperties> --> </PropertyGroup>
Now when you run dotnet pack MyProject.csproj, it will use your existing nuspec file—including the <files> section that adds Libs\*.* to the build target.
Quick verification tip
To confirm your files are in the right place, rename the generated .nupkg file to .zip and open it. You should see your build folder containing the Libs files exactly as you intended.
内容的提问来源于stack exchange,提问作者user007

