zekeyeehaw
zekeyeehaw

Reputation: 141

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

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?

Upvotes: 13

Views: 18604

Answers (3)

seva titov
seva titov

Reputation: 11880

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>

Upvotes: 37

BryanJ
BryanJ

Reputation: 8563

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

Upvotes: 2

BryanJ
BryanJ

Reputation: 8563

If I understand you correctly, you want to open a file when building in visual studio but not from command line with MSBuild?

If that is the case, specify a PreBuild or PostBuild in Visual Studio.

  1. Right click on the project in the solution explorer and select Properties
  2. Select the Events tab
  3. Add either a Pre or Post Build event to open the desired file

Upvotes: 0

Related Questions