【问题标题】:Nuget Library - Copy DLL of Library to current project's output directoryNuget Library - 将库的 DLL 复制到当前项目的输出目录
【发布时间】:2019-01-06 17:50:54
【问题描述】:

所以最好解释一下我想做什么:

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

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

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

谢谢!

【问题讨论】:

    标签: c# .net visual-studio nuget nuget-package


    【解决方案1】:

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

    当你下载包NUnit3TestAdapter并用NuGet包资源管理器打开它(从微软商店获取),你会发现以下内容:

    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文件会被复制到输出文件夹中。

    如果需要,也可以使用此方法。

    希望这会有所帮助。

    【讨论】:

    • 谢谢!那么这需要放在我的项目目录中的什么位置,我需要告诉 Visual Studio 以某种方式查看它还是会自动获取它?
    • @user8159708,实际上,您不需要添加构建事件来复制 .props 文件。当您设置此文件 target="build" 时,NuGet 将自动导入到使用该包的项目中。查看文档docs.microsoft.com/en-us/nuget/create-packages/native-packages
    【解决方案2】:

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

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

    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 文件将在成功构建时自动推送我的文件。

    我必须确保我调用道具文件的名称与我的项目 ID 相同。

    【讨论】:

    • 感谢您在这里分享您的解决方案,您可以接受它作为答案。这可能对阅读此主题的其他社区成员有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2014-10-10
    • 2015-01-02
    • 2018-10-13
    相关资源
    最近更新 更多