Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting properties' values in MSBuild

Let's consider the below example.

There, I have:

  1. target MAIN calls target t and then calls target tt.
  2. target t calls target ttt, and target tt calls target tttt.
  3. target t defines property aa, and target ttt modifies aa.
  4. target tttt tries to print property aa's value.
  5. In short, we have: MAIN -> {t -> {ttt->modifies aa, defines aa}, tt -> tttt -> prints aa}

But in target tttt, we can't "see" aa's updated value (by ttt)! How do I make that value visible to target tttt?

The whole script is as below:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MAIN" >
  <Target Name="MAIN" >
    <CallTarget Targets="t" />
    <CallTarget Targets="tt" />
  </Target>

  <Target Name="t">
    <Message Text="t" />
    <PropertyGroup>
      <aa>1</aa>
    </PropertyGroup>
    <CallTarget Targets="ttt" />
  </Target>

  <Target Name="tt">
    <Message Text="tt" />
    <CallTarget Targets="tttt" />
  </Target>

  <Target Name="ttt">
    <PropertyGroup>
      <aa>122</aa>
    </PropertyGroup>
    <Message Text="ttt" />
  </Target>

  <Target Name="tttt">
    <Message Text="tttt" />
    <Message Text="tttt:$(aa)" />
  </Target>

</Project>
like image 596
Nam G VU Avatar asked May 08 '26 23:05

Nam G VU


1 Answers

As already said in an answer to another post you should model your MSBuild project with dependencies between your Targets rather than calling Targets one after another.

<Project DefaultTargets="tttt" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="t">
        <Message Text="t" />
            <PropertyGroup>
                <aa>1</aa>
            </PropertyGroup>
     </Target>

     <Target Name="tt" DependsOnTargets="t">
         <Message Text="tt" />
     </Target>

     <Target Name="ttt" DependsOnTargets="t;tt">
         <PropertyGroup>
             <aa>122</aa>
         </PropertyGroup>
         <Message Text="ttt" />
     </Target>

     <Target Name="tttt" DependsOnTargets="t;tt;ttt">
         <Message Text="tttt" />
         <Message Text="tttt:$(aa)" />
     </Target>
</Project>

An approach I use, is to define a Target as my final goal, putting it into the projects DefaultTargets.

Then add all the things that need to happen to achieve this goal.

like image 138
Filburt Avatar answered May 11 '26 16:05

Filburt