microsoft / MSBuildLocator

An API to locate MSBuild assemblies from an installed Visual Studio location. Use this to ensure that calling the MSBuild API will use the same toolset that a build from Visual Studio or msbuild.exe would.
Other
212 stars 83 forks source link

How To Build Release #166

Closed Gao996 closed 2 years ago

Gao996 commented 2 years ago

It seems that all builds are debug versions, how to do something like this dotnet msbuild /p:Configuration=Release Generate release version

Forgind commented 2 years ago

dotnet msbuild --configuration Release worked for me.

Gao996 commented 2 years ago

What if you use code

public void MethodThatDoesNotDirectlyCallMSBuild()
        {
            MSBuildLocator.RegisterDefaults();
            Build();
        }

        private static void Build()
        {
            var p = new BuildParameters
            {
                MaxNodeCount = 4,
                Loggers = new ILogger[] { new Microsoft.Build.Logging.ConsoleLogger(LoggerVerbosity.Normal) },
            };

            var req = new BuildRequestData(BuildPath,
                new Dictionary<string, string>(),
                null,
                new[] { "Build" },
                null,
                BuildRequestDataFlags.None);

            var result = BuildManager.DefaultBuildManager.Build(p, req);

            if (result.OverallResult.Equals(BuildResultCode.Failure))
            {
                throw new System.Exception();
            }
        }
Gao996 commented 2 years ago

If setting the parameters like this will generate an empty Release folder, the build will still fail

var req = new BuildRequestData(BuildPath,
                new Dictionary<string, string>() { { "configuration","Release"} },
                null,
                new[] { "Build" },
                null,
                BuildRequestDataFlags.None);
Gao996 commented 2 years ago

When I use vs to generate once, these seem to work well, is the last successful version automatically generated?