Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use BuildManager to build .Net Core project or solution on Visual Studio 2017 (MsBuild 15)

I want to build a simple project or solution (VS 2017, .NET Standard/.Net Core):

ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "15.0";            
ILogger logger = new ConsoleLogger();
pc.Loggers.Add(logger);
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
BuildRequestData buildRequest = new BuildRequestData(fileName, globalProperty, null, new[] { target }, null);
BuildParameters buildParameters = new BuildParameters(pc)
{
    DefaultToolsVersion = "15.0",
    OnlyLogCriticalEvents = false,
    DetailedSummary = true,
    Loggers = new List<Microsoft.Build.Framework.ILogger> { logger }.AsEnumerable()
};
var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
return result.OverallResult == BuildResultCode.Success;

But the build fails with the following error

MSBUILD : warning MSB4196: The "*.overridetasks" files could not be successfully loaded from their expected location "C:\Program Files\dotnet". Default tasks will not be overridden.
MSBUILD : warning MSB4010: The "*.tasks" files could not be successfully loaded from their expected location "C:\Program Files\dotnet". Default tasks will not be available.
D:\Build\workspace\Lx\Lx.sln.metaproj : error MSB4036: The "Message" task was not found. 
Check the following: 
1.) The name of the task in the project file is the same as the name of the task class. 
2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 
3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Program Files\dotnet" directory.

Seems it is unable to find the right directory or such ... What should I do to fix this ?

Note: I don't want to copy files from other directories.

like image 581
Fab Avatar asked Apr 19 '17 12:04

Fab


People also ask

Can I use MSBuild for .NET core?

NET Core applications are typically built using MSBuild or dotnet.exe . MSBuild and dotnet.exe don't require Visual Studio to be installed, but in order to use them, the following prerequisites are required on a build server: Visual Studio Build Tools installed. Ensure that ".

How do you create a solution using MSBuild?

At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute. Specify the target after the -target: switch in the format <ProjectName>:<TargetName>.

Which tasks in MSBuild can be used by developers in code with MSBuild?

MSBuild includes common tasks that you can modify to suit your requirements. Examples are Copy, which copies files, MakeDir, which creates directories, and Csc, which compiles Visual C# source code files.


1 Answers

It seems in order to use BuildManager with .Net Core/Visual Studio 2017/MsBuild 15, you must set several environment variables:

var msbuildRoot = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild";
var msbuildExe = Path.Combine(msbuildRoot, @"15.0\Bin\MsBuild.exe");
var sdkPath = Path.Combine(msbuildRoot, "Sdks"); 
Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", msbuildExe);
Environment.SetEnvironmentVariable("MSBUILDSDKSPATH", sdkPath);
Environment.SetEnvironmentVariable("MSBuildExtensionsPath", msbuildRoot);

Note that the first two should be set before any access to MsBuild classes. It's because they are read by BuildEnvironmentHelper in a static initialization (Singleton pattern). Check method TryFromEnvironmentVariable for more details.

However the last one MSBuildExtensionsPath, could be setup by global properties as such :

globalProperties.Add("MSBuildExtensionsPath", msbuildRoot);

Update 28/02/2020:

Have found the Buildalyzer repository. Doesn't seem to be able to do full build however maybe there are some workarounds to avoid messing with environment.

like image 71
Fab Avatar answered Oct 23 '22 14:10

Fab