views:

1156

answers:

4

Does anyone know how to get the name of the TARGET (/t) called from the MSBuild command line? There are a few types of targets that can be called and I want to use that property in a notification to users.

Example:

msbuild Project.proj /t:ApplicationDeployment /p:Environment=DEV

I want access to the target words ApplicationDeployment in my .Proj file.

Is there a property I can access? Any clue how to do this?

EDIT: I do not want to have to also pass in a property to get this.

UPDATE: This is based on deployment scripts using MSBuild scripts. My build server is not used for deploying code, only for building. The build server itself has build notifications that can be opted into.

+6  A: 

I'm not sure how to do exactly what you ask, but could you pass that string using the /p option?

msbuild Project.proj /t:ApplicationDeployment /p:Environment=DEV;MyValue=ApplicationDeployment


The only other way I can see to do it is to use a conditional property in each target, and thus establish the first target to be invoked.

<Target Name="ApplicationDeployment">
<PropertyGroup>
  <InvokedTarget Condition="'${InvokedTarget}'==''">ApplicationDeployment</InvokedTarget>
</PropertyGroup>

...
</Target>
apathetic
I could, but I would like to know how to access the target itself.
ferventcoder
Thanks for the down vote. I did read the MSBuild documentation to check, and I don't believe MSBuild offers this information. Workaround offered.
apathetic
I appreciate the workaround
Lars Truijens
The workaround could work. Changed down vote to plus vote. :D
ferventcoder
Property Group doesn't work inside of a target. How is that for awesome?
ferventcoder
Hmm, there are plenty of examples in Microsoft.TeamFoundation.Build.targets of PropertyGroup being used inside target. Perhaps this is a TeamBuild-only thing.
apathetic
Perhaps this is a MSBuild 2008 thing?
ferventcoder
+1  A: 

There's no way to do this (that I am aware of). MSBuild doesn't have a property for the list of targets requested to build.

However, if you find a way, keep in mind that it might not be a single target, but instead a list of targets to build.

Franci Penov
Right. I understand. :D
ferventcoder
A: 

I'd recommend using a server like CCNET to handle build executions and notification. Sure, you can do things to your MSBuild script to send out notificatioms, but that domain belongs to the build server.

kodefuguru
Think deployment scripts.
ferventcoder
I edited for clarity. Thanks for pointing that out
ferventcoder
+6  A: 

I found the answer!

<Target Name="ApplicationDeployment" >
    <CreateProperty Value="$(MSBuildProjectName) - $(Environment) - Application Deployment Complete">
      <Output TaskParameter="Value" PropertyName="DeploymentCompleteNotifySubject" />
    </CreateProperty>

I would like to give partial credit to apathetic. Not sure how to do that.

ferventcoder
How is that an answer to your question? This will not give you the list of the targets invoked on the command line, it will create the new property when particular task is executed. Was your question wrong?
Franci Penov
In essence this achieves the same end result. This will let you set a property that can tell you what the called target was (in my case AppDeploy vs DBDeploy). In each instance of getting to each target you could set this same property. I elaborated some more by adding things to the value I create.
ferventcoder