[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2402
  • Last Modified:

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?


0
sfun28
Asked:
sfun28
  • 4
  • 3
1 Solution
 
_Katka_Commented:
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

0
 
sfun28Author Commented:
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>
0
 
_Katka_Commented:
Hi, I'm usually setting the environment variables for that.

<see the stripped code below>

Then I'll include the task in my target like this:

<UsingTask AssemblyFile="$(MyPath)MyTasks.dll" TaskName="MyNamespace.SetEnvVar" />

Then you have to set variable in some target; make your target dependent on the one setting the variable.

<SetEnvVar Variable="MySwitch" Value="true" />

And then when called:

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

regards,
Kate
using System;
using System.Security;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
 
namespace Sitronics.Tasks
{
    public class SetEnvVar : Task
    {
        private String variable;
        private String variableValue;
 
        [Required]
        public String Variable
        {
            get { return variable; }
            set { variable = value; }
        }
 
        [Required]
        public String Value
        {
            get { return variableValue; }
            set { variableValue = value; }
        }
 
        public override Boolean Execute()
        {
            try
            {
                Environment.SetEnvironmentVariable(variable, variableValue, EnvironmentVariableTarget.Process);
                return true;
            }
            catch (ArgumentNullException anex)
            {
                // I stripped it here; but feel free to throw corresponding error
            }
            catch (ArgumentException aex)
            {
                // I stripped it here; but feel free to throw corresponding error
            }
            catch (SecurityException sex)
            {
                // I stripped it here; but feel free to throw corresponding error
            }
 
            return false;
        }
    }
}

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sfun28Author Commented:
and there's no easier way to do this?  this seems a bit complex just to reuse a <Target>
0
 
_Katka_Commented:
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
0
 
sfun28Author Commented:
i was hoping not to use environment vars.
anyways, thanks for helping!
0
 
_Katka_Commented:
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
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now