baruchiro
baruchiro

Reputation: 5801

MSBuild target for one framework only

I have a project with multiframework target- <TargetFrameworks>netstandard2.0;net471</TargetFrameworks>.

I want to build the solution for netframework and netstandard separately.

Currently I use this MSBuild command:

MSBuild MySln.sln /t:Build /p:Configuration=Release /p:Platform="Any CPU" /m /nr:False

I tried tu run this command:

MSBuild CxAudit.sln /t:Build /p:Configuration=Release /p:Platform="Any CPU" /p:TargetFramework=netstandard2.0 /m /nr:False (with /p:TargetFramework=netstandard2.0)

But it failed, even the first command pass and build the netstandard output.

Upvotes: 3

Views: 2534

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100543

I suggest setting a custom property and conditioning on these properties. This way, you won't affect other projects or references:

<TargetFrameworks Condition="'$(BuildNetStdOnly)' == 'true'">netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition="'$(BuildNetFxOnly)' == 'true'">net471</TargetFrameworks>
<TargetFrameworks Condition="'$(TargetFrameworks)' == ''">netstandard2.0;net471</TargetFrameworks>

This way you can build using

msbuild -p:BuildNetStdOnly=true -p:Configuration=Release -m -nr:false
msbuild -p:BuildNetFxOnly=true -p:Configuration=Release -m -nr:false

Note that this is setting the plural version only because TargetFramework needs to be set as global properties for the inner builds to work if the project was restored for both target frameworks. If you want to set the singular TargetFramework, you also need to restore again for each invocation, by passing the -restore argument to msbuild as well.

Upvotes: 5

Related Questions