visual-studio - 创建包含共享项目属性的 nuget - 自动引用

标签 visual-studio msbuild nuget csproj

我们想要创建一个包含 msbuild 属性 (.props) 文件的 nuget。我们通过创建一个 nuspec 来做到这一点,其如下 MIL(最重要的行):

  <files>
    <file src="SharedProperties.props" target="build\SharedProperties.props" />
  </files>

我们如何更改 .nuspec 定义,以便引用此 nuget 的项目 (.csproj) 自动包含属性文件(“类似于”此代码段中的第 3 行):

<?xml version="1.0" encoding="utf-8"?>
  <Project Sdk="Microsoft.NET.Sdk">
    <Import Project="..\Shared\SharedProperties.props" />

(这可能吗?)

最佳答案

这是 nuget 包设计的一个特点。而且nuget具有自动导入目标的机制。请参阅this document .

提示您应该将目标或 props 文件命名为 .props 或 Targets,然后将文件打包到 build 文件夹中。仅此而已。

举个例子,我创建了一个lib项目,然后使用这个nuspec文件来打包:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>test_A</id>
    <version>1.0.0</version>
    <title>me</title>
    <authors>me</authors>
      ......
  </metadata>
<files>
<file src="test_A.props" target="build" />
</files>
</package>

如果我的包裹名为 test_A.1.0.0.nupkg ,该文件应命名为 test_A.props文件。

然后,当您将nuget包安装到新项目中时,您可以查看<new project>.csproj文件,props文件是在nuget安装过程中自动添加的。

enter image description here

如果使用PackageReference nuget管理格式安装nuget包,该文件会添加到obj\xxx.csproj.nuget.g.props下或obj\xxx.csproj.nuget.g.targets文件:

enter image description here

对于 new-sdk 项目,这也有效。如果你创建一个new-sdk类库项目,你可以使用它到csproj文件中来打包它:

<None Include="<packages_id>.props" Pack="true" PackagePath="build">

完成后,将新包安装到new-sdk主工程中,你会发现obj\xxx.csproj.nuget.g.props下的props文件已经自动导入了文件。

关于visual-studio - 创建包含共享项目属性的 nuget - 自动引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67263924/

相关文章:

visual-studio - 防止 Visual Studio 在抛出异常时崩溃

msbuild - 如何在Wix中引用heat.exe生成的wxs文件中的File元素

msbuild - Microsoft.Sdc.Tasks.BizTalk2004.Host.Delete错误,但不使用Biztalk吗?

asp.net-core - 使用 aspnetcore 2.1 时出现 nuget hell?

c++ - 在 Visual C++ 2003 中删除调试符号

c - intmax_t C 中的数据类型

asp.net - VS 2017 更新后 Nuget 安装面板为空

visual-studio-2010 - 通过单个命令通过 Nuget 安装多个包

c++ - boost - 添加到 'corecrt.h' 的路径

visual-studio - 对文件读取强制执行 GetLatest