Mitchel Vreman
Mitchel Vreman

Reputation: 128

MSBuild properties as variables in VSTS build definitions

Is there a way to get my MSBuild properties (from the .props file) as variables in a build definitions in Visual Studio Team Services?

I would like it if it has a built-in way but I am ok with an extension.

Upvotes: 2

Views: 1144

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100751

This can be done by creating an msbuild project (or target in an existing project) that logs messages containing the needed properties in the form of VSTS logging commands. However MSBuild can not "enumerate" defined properties, so this only works for well-known properties - which is probably good to avoid accidental collisions (even environment variables are "properties" inside MSBuild).

Given a sample some.props file:

<Project>
  <PropertyGroup>
    <PackageId>a.package.id</PackageId>
    <Version>1.2.3</Version>
    <Description>I am your test project</Description>
  </PropertyGroup>
</Project>

And an example emitvars.proj:

<Project>
  <Import Project="some.props" />
  <Target Name="Build">
    <Message Importance="high" Text="##vso[task.setvariable variable=PackageId]$(PackageId)" />
    <Message Importance="high" Text="##vso[task.setvariable variable=Version]$(Version)" />
    <Message Importance="high" Text="##vso[task.setvariable variable=Description]$(Description)" />
  </Target>
</Project>

This project file can then be "built" in an MSBuild task (or dotnet msbuild on linux machines using .NET Core tooling):

MSBuild task

For demonstration purposes, I added a PowerShell task that uses these variables:

example PowerShell task

The build then uses the variables in the script as expected. note that the log lines setting the variables might not be displayed in the build log.

build log

Upvotes: 4

Related Questions