2

I have project A which has dependency on project B but there is no reference from B to A. I want build and copy assemblies in bin folder of project B to bin folder of project A. how can I do this with post build event and dotnet msbuild.

I found this link but it works for VS 2015 and below and MS-Build:

Build another project by prebuild event without adding reference

Community
  • 1
  • 1
Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66

2 Answers2

9

Here is what worked for me. It comes from: https://github.com/dotnet/sdk/issues/677

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="if not exist $(OutDir)..\..\..\..\build  mkdir $(OutDir)..\..\..\..\build" />
    <Exec Command="copy $(OutDir)$(TargetName).dll $(OutDir)..\..\..\..\build\$(TargetName).dll /Y" />
    <Exec Command="copy $(OutDir)$(TargetName).pdb $(OutDir)..\..\..\..\build\$(TargetName).pdb /Y" />
</Target>
Will Tartak
  • 548
  • 7
  • 17
  • Great answer but there is a typo "if not exist $(OutDir)..\..\..\.\build" has a missing "." just before build. Should be "if not exist $(OutDir)..\..\..\..\build" – Andrew Harris Apr 25 '21 at 23:16
4

how can I do this with post build event and dotnet msbuild

You can add Build task and copy task in the post build event in project A to achieve your request:

"$(MSBuildBinPath)\MSBuild.exe" "$(SolutionDir)ProjectB\ProjectB.csproj"
 xcopy.exe "$(SolutionDir)ProjectB\bin\Debug\netcoreapp1.1\ProjectB.dll" "$(SolutionDir)ProjectA\bin\Debug\netcoreapp1.1"

If you have multiple assemblies in the bin folder of project B, you can also use the Wildcard to copy the assemblies, like

xcopy.exe "$(SolutionDir)ProjectB\bin\Debug\netcoreapp1.1\*.dll

Hope this can help you.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135