Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MSBUILD, how can you specify a condition that check whether command line or VS launched it?

Tags:

msbuild

I have a csproj that I would like to have trigger the opening of a particular file in Visual Studio, only if the target was executed from within Visual Studio, but not from the MSBUILD command line. How do I do this?

like image 419
zekeyeehaw Avatar asked Mar 06 '12 20:03

zekeyeehaw


People also ask

How use MSBuild command line?

To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.

Which is the initial step needed when using the MSBuild at the command prompt?

Build the targettargets file. Run MSBuild from the Developer Command Prompt for Visual Studio to build the HelloWorld target defined above. Use the -target or -t command-line switch to select the target.

What is the difference between MSBuild and Visual Studio build?

Visual Studio determines the build order and calls into MSBuild separately (as needed), all completely under Visual Studio's control. Another difference arises when MSBuild is invoked with a solution file, MSBuild parses the solution file, creates a standard XML input file, evaluates it, and executes it as a project.

What is switch in MSBuild?

When you use MSBuild.exe to build a project or solution file, you can include several switches to specify various aspects of the process. Every switch is available in two forms: -switch and /switch. The documentation only shows the -switch form. Switches are not case-sensitive.


2 Answers

Quote from MSDN page:

When building inside Visual Studio, the property $(BuildingInsideVisualStudio) is set to true. This can be used in your project or .targets files to cause the build to behave differently.

Example how it could be used in your .*proj or .targets file:

<PropertyGroup>
  <MyProperty Condition="'$(BuildingInsideVisualStudio)' == 'true'">This build is done by VS</MyProperty>
  <MyProperty Condition="'$(BuildingInsideVisualStudio)' != 'true'">This build is done from command line of by TFS</MyProperty>
</PropertyGroup>
like image 168
seva titov Avatar answered Sep 18 '22 15:09

seva titov


Add a property to the .csproj project file, example:

<PropertyGroup>
    <FromMSBuild>false</FromMSBuild>
</PropertyGroup>

Then in the task you want to run, put a condition that evaluates that property. For example, i f you want to open notepad.exe whenever the build is executed from command line and NOT visual studio:

  <Target Name="BeforeBuild">
<Exec Command="C:\Windows\Notepad.exe" Condition="$(FromMSBuild)" />
  </Target>

Of course, this is dependent on setting the $(FromMSBuild) property correctly when you run the build via command line, like so:

MSBuild myProject.csproj /p:FromMSBuild=true
like image 35
BryanJ Avatar answered Sep 18 '22 15:09

BryanJ