Link to home
Start Free TrialLog in
Avatar of sfun28
sfun28

asked on

MsBuild: target parameters

Hello Experts,

how can I write a target such that it takes an input parameter.  For example, lets say I have a target MyTarget that calls MyTask.  I want to call MyTarget twice.  Once with setting the Property MyTask.MyProperty = true and anohter time with MyTask.MyProperty = false.  I don't want to have to write two tasks where I hardcode MyProperty.

is this possible?


Avatar of _Katka_
_Katka_
Flag of Czechia image

Hi, check the example below.

Then you use it in your target like this:

  <MyTask  MyProperty="true">
  </MyTask>

OR (obviously :)

  <MyTask  MyProperty="false">
  </MyTask>

regards,
Kate
public class MyTask : Task
{
    private bool myProperty;
    
    public bool MyProperty
    {
        set { myProperty = value; }
    }
 
    public override bool Execute()
    {
        // your value used in code
        if (myProperty)
        {
            ...
        }
    }
}

Open in new window

Avatar of sfun28
sfun28

ASKER

hi Kate,

I think there's a misunderstanding about my question.  Let me provide an example of what i'm trying to accomplish.
I'm trying to reuse MyTarget, but provide a parameter value to MyTarget.  In the example below, I only need to specify MyTarget once, then I can call it with MyProperty = true and MyProperty = false.  Is this possible?


<Target Name="All" DependsOnTargets="$(AllDependsOn)">
</Target>

<PropertyGroup>
         <AllDependsOn>
                  MyTarget  (set MyProperty = false)
                  MyTarget  (set MyProperty = true)
          </AllDependsOn>
</PropertyGroup>

<MyTarget>
      <MyTask  MyProperty = [PARAMETER HERE] >
       ...
</MyTarget>
ASKER CERTIFIED SOLUTION
Avatar of _Katka_
_Katka_
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sfun28

ASKER

and there's no easier way to do this?  this seems a bit complex just to reuse a <Target>
Well, it doesn't sound that complicated to me anymore. :D

The task is being reused many times over so it payed itself (for us that is).

And you only put:

<MyTarget>
      <MyTask  MyProperty = "$(MySwitch)" />
</MyTarget>

Which isn't that crazy to follow.

Anyway I'm not aware (at this moment) of any other way, but then again, I'm not exactly a MsBuild guru.

regards,
Kate
Avatar of sfun28

ASKER

i was hoping not to use environment vars.
anyways, thanks for helping!
You're welcome, I'm sorry but I honestly don't know any other solution to this problem. I'm working at a pretty big company, yet still nobody was able to come with better solution to this problem (local gurus included). I can give you only my confidence that this is the final solution of the huge international company. So you're not alone ;)

regards,
Kate