Nuget 库 - 将库的 DLL 复制到当前项目的输出目录

Nuget Library - Copy DLL of Library to current project's output directory

所以为了最好地解释我想做什么:

如果您将 nuget 包 NUnitTestAdapter 安装到您的项目,在构建您的项目时,您将在项目的输出目录中有一个 NUnit3.TestAdapter.dll。

我也想这样做。我有一个正在打包的库,但是当我将它安装到另一个项目并构建它时,我的 dll 没有像 NUnit 那样包含在内。

至于为什么我需要这样做,我正在编写一个测试报告器,它为所有测试运行 生成 JSON、XML、HTML 等报告。 我在 AppDomain.CurrentDomain.ProcessExit 事件中有这个 运行,但是这被限制在 2 秒左右,所以如果我的报告没有在 2 秒内生成,它就不会工作。因此,相反,在进程退出时,它将执行我的报告 DLL,它没有时间限制。

谢谢!

Nuget Library - Copy DLL of Library to current project's output directory

当您下载包 NUnit3TestAdapter 并使用 NuGet 包资源管理器(从 Microsoft Store 获取)打开它时,您会发现以下内容:

build 文件夹中有一个 NUnit3TestAdapter.props 和 属性 CopyToOutputDirectory:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Content Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll">
      <Link>NUnit3.TestAdapter.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Visible>False</Visible>
    </Content>
    <Content Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.pdb" Condition="Exists('$(MSBuildThisFileDirectory)NUnit3.TestAdapter.pdb')">
      <Link>NUnit3.TestAdapter.pdb</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Visible>False</Visible>
    </Content>
    <Content Include="$(MSBuildThisFileDirectory)nunit.engine.dll">
      <Link>nunit.engine.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Visible>False</Visible>
    </Content>
    <Content Include="$(MSBuildThisFileDirectory)nunit.engine.api.dll">
      <Link>nunit.engine.api.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Visible>False</Visible>
    </Content>
  </ItemGroup>
</Project>

所以,当我们将这个包安装到项目中时,这个文件NUnit3TestAdapter.props将导入到项目文件中,然后dll文件将被复制到输出文件夹中。

如果你想要,你也可以使用这个方法。

希望对您有所帮助。

所以在 Leo Liu-MSFT 的帮助下,我做到了。

首先我必须像 Leo 的回答一样创建一个 props 文件 然后我必须在项目上创建一个 post-build 事件,将道具文件复制到我的目标目录。

copy /Y "$(ProjectDir)$(TargetName).props" "$(TargetDir)$(TargetName).props"

然后我创建了一个 nuspec 文件,它将 props 和 dll 从我的目标目录复制到 nuget 包中的构建目录

<files>
        <file src="bin\Release\**\Report.props" target="build" />
        <file src="bin\Release\**\Report.dll" target="build" />
    </files>

然后通过nuget pack ###.nuspec

打包库

然后当通过 nuget 导入库时,props 文件将在成功构建时自动推送我的文件。

我必须确保我将 props 文件命名为与我的项目 ID 相同的名称。