如何在解决方案上有选择地使用dotnet pack [英] How to use dotnet pack selectively on a solution
问题描述
我有一个解决方案 foo.sln .其中的所有库都是SDK项目.但是,其中只有一个应包装
I have a solution foo.sln. All libraries within are SDK projects. However only one of them should be packed
默认情况下
dotnet pack
尝试打包所有项目.没有排除过滤器或包含过滤器.推荐的过程是什么?
tries to pack all projects. There is no exclude filter or include filter for that matter. What is the recommended process?
推荐答案
您可以通过在 csproj
文件中设置属性来选择要打包为nuget包的项目,如下所示:
You select what project to pack as nuget package by setting property inside of csproj
file, like this:
< IsPackable> true</IsPackable>
-创建程序包
< IsPackable> false</IsPackable>
-不要创建包
如果不想在每个文件中指定它,则可以在目录中创建一个名为 Directory.Build.props
的文本文件,其内容如下:
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>
它会自动包含在此文件夹和所有嵌套文件夹的SDK项目文件(csproj)开头的所有内容中,因此您可以为这组项目指定通用的默认值,并且如果需要,可以在单个csproj中覆盖它们文件.
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.
如果您不希望它们被覆盖或需要使用csproj中定义的某些值,则应使用文件名 Directory.Build.targets
,该文件名将自动包含在csproj.
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.
对此的一个注意事项是,默认情况下,仅应用由 csproj
找到的第一个 Directory.Build.props
,要更改此行为,您需要添加此行在嵌套的 Directory.Build.props
文件的开头(在 Project
标记内部):
One note on this is that by default only first Directory.Build.props
that 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)../'))" />
有关所有这些的更多信息,请参见: https://docs.microsoft.com/zh-CN/visualstudio/msbuild/customize-your-build?view=vs-2017
More about all of this could be found here: https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2017
这篇关于如何在解决方案上有选择地使用dotnet pack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!