Question

I have the following MSBuild targets file (below).

How would I add an additional step after Default to copy all the \bin\Debug or \bin\Release from one of the projects to a folder called binaries that is in the same folder as the .targets file was run?

If I could get it to be smart about if it uses Debug or Release even better.

UPDATE: I have updated my code that I have gotten so far. Still would be nice to be able to run the .bat file that kicks this msbuild off in two operating modes: Release and Debug and then have two different folders in the Binaries folder

  <ItemGroup>
    <SolutionFiles Include=".\**\*.sln"/>
  </ItemGroup>

  <ItemGroup>
    <DebugBinFiles Include=".\src\Sample.Core\Sample.Core.Infrastructure\bin\Debug\**\*.*"/>
  </ItemGroup>

  <ItemGroup>
    <ReleaseBinFiles Include=".\src\Sample.Core\Sample.Core.Infrastructure\bin\Release\**\*.*"/>
  </ItemGroup>

  <Target Name="Default" AfterTargets="AfterBuild">
    <MSBuild Projects="@(SolutionFiles)" Condition=""/>
  </Target>

  <Target Name="AfterBuild">
    <Copy SourceFiles="@(BinFiles)" DestinationFolder=".\binaries\Debug" />
  </Target>

</Project>
Was it helpful?

Solution

  1. Define in your targets file Configuration property default value
  2. Use this property value in a output path
  3. Instead of AfterTargets use DependsOnTargets, but as Default target define the last one, so targets will execute in the same order but just chained in reverse order
  4. Save XML below as final.targets or how would you like name it
  5. Run in command line msbuild final.targets /p:Configuration=RELEASE for release configuration build
<Project ToolsVersion="4.0"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
         DefaultTargets="CopyBinOutput">

   <PropertyGroup>
      <!-- define defaults -->
      <Configuration Condition="$(Configuration)==''">DEBUG</Configuration>
   </PropertyGroup>

   <Choose>
      <When Condition=" '$(Configuration)'=='DEBUG' ">
         <ItemGroup>
            <BinFiles Include=".\src\Sample.Core\Sample.Core.Infrastructure\bin\Debug\**\*.*" />
         </ItemGroup>
      </When>

      <When Condition=" '$(Configuration)'=='RELEASE' ">
         <ItemGroup>
           <BinFiles Include=".\src\Sample.Core\Sample.Core.Infrastructure\bin\Release\**\*.*"/>
          </ItemGroup>
      </When>
  </Choose>

  <Target Name="EntryPointTarget">
      <Message Text="EntryPointTarget started" />
      <!-- here is build your solution -->
  </Target>

  <Target Name="CopyBinOutput" DependsOnTargets="EntryPointTarget">
      <Message Text="CopyBinOutput started" />
     <Copy SkipUnchangedFiles="True" OverWriteReadOnlyFiles="true"
           SourceFiles="@(BinFiles)" 
           DestinationFiles="@(BinFiles->'.\binaries\$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')" />

      <Message Text="CopyBinOutput finished, destination is .\binaries\$(Configuration)" />
  </Target>

</Project>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top