An MSBuild File To Work With .Net Core As Well
Onorio Catenacci
Posted on September 6, 2018
I sometimes take some extra time and effort to create some files from scratch simply because I don't want all the extraneous fluff that comes with letting automated utilities build a file. For example, if I create a new solution in Visual Studio there will be a .csproj file created as part of the process. And that .csproj will be filled with all sorts of extraneous settings that I don't want or care about. So every now and again I build an MSBuild .csproj from scratch.
I had an MSBuild file that worked fine for building a small/simple library that I created. But then I tried to build the library with dotnet core and I ran into an issue.
Digging into the issue a bit I found that basically MSBuild does some hidden wizardry to include some files without being told to include them. Specifically it includes mscorlib.dll without the file ever getting mentioned. But dotnet core doesn't have that implied behavior.
So without any further ado here's my modified build file that works with both MSBuild and dotnet build for whatever it's worth. I hope it helps others.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="ColorNumberGameEvent.cs" />
<Compile Include="GameStates.cs" />
<Compile Include="LegalColors.cs" />
<Compile Include="GameSolution.cs" />
</ItemGroup>
<ItemGroup>
<References Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client" /> <!-- Included for dotnet build -->
</ItemGroup>
<Target Name="Build">
<Csc TargetType="library" Sources="@(Compile)"
TreatWarningsAsErrors="true"
WarningLevel="4"
AdditionalLibPaths="@(References)"
References="mscorlib.dll"/> <!-- Included for dotnet build -->
</Target>
</Project>
A couple of notes:
1.) I would have used the ProgramFiles(x86) environment variable (rather than hard coding the path) but there's a bug with MSBuild and environment variables that contain "("
2.) You need both the AdditionalLibPaths and the References set in order for this to work.
Posted on September 6, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.