Reputation: 14813
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.
Upvotes: 6
Views: 2592
Reputation: 14813
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.
Upvotes: 2