2

I have a web application in VS2010 which has a number of DLLs that need to be copied into the bin directory after doing a publish in VS2010.

I've tried putting the following into my .csproj file (which sits in the root folder of the web applications) but it doesn't seem to work:

<Target Name="AfterBuild">
  <ItemGroup>
    <_CircularDependencies Include="DLLs\Circular\Dependencies\*.dll" />
  </ItemGroup>
  <Copy
    SourceFiles="@(_CircularDependencies)" 
    DestinationFiles="@(_CircularDependencies->'bin\%(Filename)%(Extension)')" 
    SkipUnchangedFiles="true" />
</Target>

For bonus points, I have another set of DLLs copied to be copied post-publish, but I want to use one set when doing a debug publish (for Win32) and a different set when doing a release publish (x86).

Thanks!

Mick Byrne
  • 14,394
  • 16
  • 76
  • 91
  • It looks correct, so try adding a message to diagnose, between your item group and copy task, as to see if there are any files. Also try running with a diagnostic logger from the command line with "/fl /flp:v=diag;logfile=out.txt" and search to see what the copy task says about the operation. – Brian Kretzler Mar 14 '11 at 04:52
  • Thanks for the tip about the message. I've confirmed that the target is getting hit and the files referenced are correct. In fact I think this is actually working and the files are being copied into the bin directory - however, they're not then being picked up and copied to the bin directory in the directory where the site is published. – Mick Byrne Mar 16 '11 at 02:45
  • I've seen something like this before when an item list used to copy files (in your case the site binaries when published) is being 'calculated' prior to your target being run. If that is the case, it won't matter what additional files you add, the item group is already set and won't be reevaluated. You may need to find an earlier target to wire yourself to; consider using the BeforeTargets and AfterTargets to pick a precise spot if needed. You may also be able to take the output of the Copy task (which will be an item list) and merge it into whatever item list is being used to publish. – Brian Kretzler Mar 16 '11 at 15:38

3 Answers3

5

OK, I've managed to get this working fully. Thanks to the answers provided above, I've been able to add some MS Build commands to the .csproj file to copy the appropriate DLLs from various folders into the bin folder based on the current build configuration. However as these are unmanaged DLLs (i.e. not .NET) I can't create normal references to them and they fail to be copied during the publish. I got around this by dynamically adding the files to the project as 'content'.

The solution came in three parts. Firstly, create an item group for the files near the top of the .csproj file (I've tried to use generic filenames here to make it clearer), with conditions based on the current build configuration:

<ItemGroup Condition="'$(Configuration)' == 'Debug'">
  <_UnmanagedDLLs Include="Win32DLLs\*.dll" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
  <_UnmanagedDLLs Include="x64DLLs\*.dll" />
</ItemGroup>

Then add another item group to include these DLLs (as content, not references) in the build:

<ItemGroup>
  <Content Include="@(_UnmanagedDLLs->'bin\%(Filename)%(Extension)')" />
</ItemGroup>

Finally, at the bottom of the .csproj file, I do the copy on the AfterBuild target:

<Target Name="AfterBuild">
  <Copy SourceFiles="@(_UnmanagedDLLs)" DestinationFiles="@(_UnmanagedDLLs->'bin\%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
</Target>

It means I can do a debug publish for my windows 32 staging box and a release publish for my x64 production box while keeping my bin folder out of SVN.

Mick Byrne
  • 14,394
  • 16
  • 76
  • 91
  • This link helped me get the closest to getting it to work when publishing.. but still no luck: http://blogs.msdn.com/b/mwade/archive/2008/06/29/how-to-publish-files-which-are-not-in-the-project.aspx .. Using this method I was able to get the files to publish in the correct locations but the published project could not find the dlls. – Soenhay Apr 18 '13 at 21:32
1

Once you get the copy working, separate sets for debug/release is easy with a condition:

<ItemGroup Condition="'$(Configuration)' == 'Release'">
  <_CircularDependencies Include="DLLs\Circular\Dependencies\*.dll" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
  <_CircularDependencies Include="DebugDLLs\Circular\Dependencies\*.dll" />
  <_CircularDependencies Include="DebugDLLs\Circular\Dependencies\*.pdb" />
</ItemGroup>
Brian Kretzler
  • 9,748
  • 1
  • 31
  • 28
0

If you want your copy to happen after publish, not after build you need to change your target from:

<Target Name="AfterBuild">

to

<Target Name="AfterPublish">
heavyd
  • 17,303
  • 5
  • 56
  • 74
  • This doesn't seem to be working... I've put in a element within the ... element but nothing shows up in the output window. – Mick Byrne Mar 16 '11 at 02:46
  • Actually I just read in http://stackoverflow.com/questions/3561689/afterpublish-target-not-working that the `"After Publish"` target is not called when you're publishing a web application (which I am). I'm still trying to figure out what the appropriate target is... – Mick Byrne Mar 16 '11 at 03:05
  • Where are you putting this? It should be after the ` – heavyd Mar 16 '11 at 03:06