简体   繁体   中英

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

So to best explain what I would like to do:

If you install the nuget package NUnitTestAdapter to your project, when building your project, you will have a NUnit3.TestAdapter.dll in your project's output directory.

I would like to do the same. I have a library that I'm packaging up, but when I install it to another project and build it, my dll isn't included like NUnit's is.

As for why I need to do this, I'm writing a test reporter, which produces JSONs, XMLs, HTML etc reports for all tests run. I'm having this run in the AppDomain.CurrentDomain.ProcessExit event, however this is limited to around 2 seconds, so if my reports aren't generated in 2 seconds, it won't work. So instead, on processexit, it'll execute my report DLL which won't have a time restraint on it.

Thanks!

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

When you download the package NUnit3TestAdapter and open it with NuGet Package explorer(Get it from Microsoft Store), you will find following content:

在此处输入图片说明

There is a NUnit3TestAdapter.props with property CopyToOutputDirectory in the build folder:

<?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>

So, when we install this package to the project, this file NUnit3TestAdapter.props will imported to the project file, then the dll files will be copied to the output folder.

If you want that, you can also use this method.

Hope this helps.

So with the help of Leo Liu-MSFT, I've managed this.

First I had to create a props file like in Leo's answer Then I had to create a post-build event on the project, copying the props file to my target directory.

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

Then I created a nuspec file, which copies the props and dll from my target directory, to a build directory in the nuget package

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

And then package up the library via nuget pack ###.nuspec

And then when importing the library through nuget, the props file would be automatically push my files through on a successful build.

I had to make sure I called the props file the same name as my project id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM