Question

In my project, I have two import statements:

<Import Project="<TransformConfigLocation>" />
<Import Project="<PackageLocation>" />

Each target has a property group defined like so:

<PropertyGroup>
    <BuildDependsOn>
        $(BuildDependsOn);
        TransformConfig
    </BuildDependsOn>
</PropertyGroup>

<PropertyGroup>
    <BuildDependsOn>
        $(BuildDependsOn);
        Package
    </BuildDependsOn>
</PropertyGroup>

I would like to guarantee that TransformConfig always runs before Package. I do not control either of the dependent targets and would prefer not to have to edit them explicitly because those edits would be wiped any time I updated those targets via NuGet. The Package target consistently runs first which is undesirable.

Is this possible?

Was it helpful?

Solution 2

The simple answer is that the order of the Imports does matter. For more detail on why, see @Seva Titov's answer and our follow up conversation.

I highly recommend http://amzn.com/0735645248 for a thorough analysis on the topic.

OTHER TIPS

Within the project a build order is guaranteed to follow MSBuild target build order rules. There is no race conditions here, everything is serialized. A quote from the link above:

<Target Name="Serve" DependsOnTargets="Chop;Cook" />

tells MSBuild that the Serve target depends on the Chop target and the Cook target. MSBuild runs the Chop target, and then runs the Cook target before it runs the Serve target.

Note that if some other target that gets executed before "Serve" has any of the Chop/Cook targets as their dependencies or had listed them in BeforeTargets/AfterTargets, that might affect the order. For example, if you have this set of targets:

 <Target Name="One" DependsOnTargets="Two;Three" />
 <Target Name="Two" DependsOnTargets="TargetB" />
 <Target Name="Three" DependsOnTargets="TargetA;TargetB;" />

and you build "One", then the build order would be as follows:

TargetB
Two
TargetA
// skip TargetB because it was already run
Three
One

So, in your case, assuming TargetB is not listed as dependency of any previous targets, $(BuildDependsOn) list gets executed in the order targets are listed there, which would be ...;TargetA;TargetB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top