Copy built assemblies (including PDB, .config and XML comment files) to folder post build

 ̄綄美尐妖づ 提交于 2019-11-28 06:38:14

问题


Is there a generic way I can get a post-build event to copy the built assembly, and any .config and any .xml comments files to a folder (usually solution relative) without having to write a post-build event on each project in a solution?

The goal is to have a folder that contains the last successful build of an entire solution.

It would be nice to use the same build solution over multiple solutions too, possibly enabling/ disabling certain projects (so don't copy unit tests etc).

Thanks,
Kieron


回答1:


You can set common OutputPath to build all projects in Sln in one temp dir and copy required files to the latest build folder. In copy action you can set a filter to copy all dlls without "test" in its name.

msbuild.exe 1.sln /p:Configuration=Release;Platform=AnyCPU;OutputPath=..\latest-temp

There exists more complicated and more flexible solution. You can setup a hook for build process using CustomAfterMicrosoftCommonTargets. See this post for example. Sample targets file can be like that:

 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <BuildDependsOn>
       $(BuildDependsOn);
       PublishToLatest
     </BuildDependsOn>
   </PropertyGroup>

   <Target Name="PreparePublishingToLatest">
     <PropertyGroup>
       <TargetAssembly>$(TargetPath)</TargetAssembly>
       <TargetAssemblyPdb>$(TargetDir)$(TargetName).pdb</TargetAssemblyPdb>
       <TargetAssemblyXml>$(TargetDir)$(TargetName).xml</TargetAssemblyXml>
       <TargetAssemblyConfig>$(TargetDir)$(TargetName).config</TargetAssemblyConfig>
       <TargetAssemblyManifest>$(TargetDir)$(TargetName).manifest</TargetAssemblyManifest>
       <IsTestAssembly>$(TargetName.ToUpper().Contains("TEST"))</IsTestAssembly>
     </PropertyGroup>
     <ItemGroup>
       <PublishToLatestFiles Include="$(TargetAssembly)" Condition="Exists('$(TargetAssembly)')" />
       <PublishToLatestFiles Include="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" />
       <PublishToLatestFiles Include="$(TargetAssemblyXml)" Condition="Exists('$(TargetAssemblyXml)')" />
       <PublishToLatestFiles Include="$(TargetAssemblyConfig)" Condition="Exists('$(TargetAssemblyConfig)')" />
       <PublishToLatestFiles Include="$(TargetAssemblyManifest)" Condition="Exists('$(TargetAssemblyManifest)')" />
     </ItemGroup>   
   </Target>

   <Target Name="PublishToLatest" 
           Condition="Exists('$(LatestDir)') AND '$(IsTestAssembly)' == 'False' AND  '@(PublishToLatestFiles)' != ''" 
           DependsOnTargets="PreparePublishingToLatest">

     <Copy SourceFiles="@(PublishToLatestFiles)" DestinationFolder="$(LatestDir)" SkipUnchangedFiles="true" />
   </Target>
 </Project>

In that targets file you can specify any actions you want.

You can place it here "C:\Program Files\MSBuild\v4.0\Custom.After.Microsoft.Common.targets" or here "C:\Program Files\MSBuild\4.0\Microsoft.Common.targets\ImportAfter\PublishToLatest.targets".

And third variant is to add to every project you want to publish import of custom targets. See How to: Use the Same Target in Multiple Project Files



来源:https://stackoverflow.com/questions/5246015/copy-built-assemblies-including-pdb-config-and-xml-comment-files-to-folder-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!