tags:

views:

709

answers:

4

I have a post-build target in MSBuild to copy some build outputs.

This is linked in as a dependency to the AfterBuild target (exposed by Microsoft.CSharp.targets):

<Target Name="AfterBuild" DependsOnTargets="InstallUtil;CopyPostBuildFiles" />

Is there any way to avoid the files being copied if the build didn't actually re-build?

For example, when the MSBuild dependency analysis asserts that the project doesn't need to be built because none of its source files have been updated, it doesn't build, but still executes my copy target. Is there any way to prevent this?

A: 

Build target definition is:

<Target Name = "Build" DependsOnTargets="BeforeBuild;CoreBuild;AfterBuild"/>

I think that CoreBuild is not executed if your files have not changed, so you could try to use AfterCompile instead of AfterBuild.

<Target Name="AfterCompile" DependsOnTargets="InstallUtil;CopyPostBuildFiles" />
madgnome
Good idea, but it doesn't seem to make much difference. The copy still executes every time, even after I change AfterBuild to AfterCompile.
Daniel Fortunov
Oh sorry, my test case must be crappy ^^.
madgnome
A: 

What about this.

  • BuildBreak. Set to true when a compilation error occurs, otherwise false. Can be used in build break targets to determine whether they are executing after a compilation error or executing normally.

http://blogs.msdn.com/aaronhallberg/archive/2008/02/12/team-build-2008-property-reference.aspx

I haven't tried it, but it sounds pretty promising.

Mark
Hi Mark, this property is a good find, however I'm after something that will let me suppress copy tasks after a "nop build", where nothing actually gets built because all the outputs are deemed to be up to date.
Daniel Fortunov
+5  A: 

What about using Inputs and Outputs target's properties like it's done in compile target.

<PropertyGroup>
  <PostBuildDir>CopiedFilesDirectory</PostBuildDir>
</PropertyGroup>

<Target Name="AfterBuild" DependsOnTargets="InstallUtil;CopyPostBuildFiles" />

<Target Name="CopyPostBuildFiles"
        Inputs="@(PostBuildFiles)"
        Outputs="@(PostBuildFiles -> '$(PostBuildDir)%(Filename)%(Extension)'">
    <Copy SourceFiles="@(PostBuildFiles)"
          DestinationFolder="PostBuildDir"/>
</Target>

You could used that on CopyPostBuildFiles and InstallUtil or directly on AfterBuild.

See this page for more info on Targets Inputs Outputs

madgnome
+2  A: 

Since you are overriding the AfterBuild taret it will always execute after the build occurs. This is the case for a rebuild or a normal build. If you want to perform some actions after the rebuild (and not build) then you should extend the dependency property for the Rebuild target. So in your case to inject the targets after a rebuild occurs your project file should look something like:

<Project ...>
   <!-- some content here -->

   <Import Project="... Microsoft.Csharp.targets" />


    <PropertyGroup>
        <RebuildDependsOn>
            $(RebuildDependsOn);
            InstallUtil;
            CopyPostBuildFiles
        </RebuildDependsOn>
    </PropertyGroup>
</Project>

This method extends the property which the Rebuild targets uses to declare what targets it depends on. I've detailed this in the article Inside MSBuild, see section Extending the build process.

Also what you are trying to accomplish may be acheived by the AfterBuild target if you can specify what what files would "*trigger*" an update to occur. In other words you can specify a set of "inputs" into the target and a set of "outputs" which are both files. If all outputs were created after all inputs then the target would be considerd up to date and skipped. this concept is known as Incremental Building and I've coverd it in the article MSBuild Best Practices Part 2. Also I have detailed this in my book, Inside the Microsoft Build Engine: Using MSBuild and Team Foundation Build.

EDIT: Adding BuildDependsOn example

If you want the target to only execute when the files are actually built and not just when the Rebuild target is executed. Then you should create your project to be like the following:

<Project ...>
   <!-- some content here -->

   <Import Project="... Microsoft.Csharp.targets" />


    <PropertyGroup>
        <BuildDependsOn>
            $(BuildDependsOn);
            CustomAfterBuild;
        </BuildDependsOn>
    </PropertyGroup>
   <Target Name="CustomAfterBuild" Inputs="$(MSBuildAllProjects);
            @(Compile);                               
            @(_CoreCompileResourceInputs);
            $(ApplicationIcon);
            $(AssemblyOriginatorKeyFile);
            @(ReferencePath);
            @(CompiledLicenseFile);
            @(EmbeddedDocumentation); 
            $(Win32Resource);
            $(Win32Manifest);
            @(CustomAdditionalCompileInputs)"
    Outputs="@(DocFileItem);
             @(IntermediateAssembly);
             @(_DebugSymbolsIntermediatePath);                 
             $(NonExistentFile);
             @(CustomAdditionalCompileOutputs)">

   <!-- Content here -->

    </Target>

</Project>

I just copied the inputs and outputs from the CoreCompile target inside of Microsoft.CSharp.targets (*I'm assuming your are using C# here*) and pasted it here. What this means is that the target will be skipped whenever the CoreCompile target is executed. Also since I extended the BuildDependsOn we know that MSBuild will try and execute it whenever the project is built.

Sayed Ibrahim Hashimi
It appears that updating the "RebuildDependsOn" targets will only cause the extra steps to trigger if you explicitly invoke the "Rebuild" target. If you invoke "Build" they will not trigger.
Daniel Fortunov
I've just edited it to cover this case. I was confused about what you wanted. Does this work?
Sayed Ibrahim Hashimi