简体   繁体   English

以编程方式使用msbuild.exe构建我的.NET解决方案并输出日志

[英]Build my .NET Solution using msbuild.exe programmatically and output the logs

Due to my project requirements, I need to create a application which accepts the Solution File path, target framework and other input parameters and invoke the msbuild.exe on a custom path which I gave. 由于我的项目要求,我需要创建一个接受解决方案文件路径,目标框架和其他输入参数的应用程序,并在我提供的自定义路径上调用msbuild.exe。

I'm doing that as below. 我正在这样做,如下所示。

var buildPath = @"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe";
        var solutionPath = @"D:\Siva\Sandbox\Samples\SandboxProject\SandboxProject.sln";
        var buildLogs = "/flp:Summary;Verbosity=minimal;LogFile=msbuild.sum";
        var process = new Process();
        process.StartInfo = new ProcessStartInfo(buildPath);
        process.StartInfo.Arguments = " " + solutionPath + " " + buildLogs;
        process.Start();
I've followed the below approach.
1) Created a Process which will invoke standalone msbuild.exe and passing the Solution Name, Dependencies as command arguments.

Custom XML Build File:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>    
    <PackageDir>$(BuildPackageDir)..\packages</PackageDir>
    </PropertyGroup>
    <Target Name="RestorePackages">
    <RemoveDir Directories="$(PackageDir)" />
    <MakeDir Directories="$(PackageDir)" />    
    <Exec WorkingDirectory="$(BuildDir)" Command="NuGet.exe restore $(MSBuildSolutionDirectory) -OutputDir $(PackageDir)" />
    </Target>
    <Target Name="Build">
      <CallTarget Targets="RestorePackages"/>
    <MSBuild Projects="$(MSBuildSolutionDirectory)" Targets="Rebuild"/>
    </Target>
    </Project>

My Command Arguments:

    inputParameters.Append(" ");
    inputParameters.Append(buildSourcePath);
    inputParameters.Append(" ");
    inputParameters.Append(Constants.PROPERTYCOMMAND);
    inputParameters.Append("BuildSolutionDirectory=");
    inputParameters.Append('"');
    inputParameters.Append(buildSolutionName);
    inputParameters.Append('"');
    inputParameters.Append(" ");
    input.Parameteters.Append(Constants.PROPERTYCOMMAND);
    inputParameters.Append("BuildPackageDir=");
    inputParameters.Append('"');
    inputParameters.Append(MSBuildSolutionDirectory);
    inputParameters.Append('"');
    inputParameters.Append(" ");
    inputParameters.Append(buildLogs);
    inputParameters.Append(" ");
    inputParameters.Append(Constants.PROPERTYCOMMAND);
    inputParameters.Append("BuildDir=");
    inputParameters.Append('"');
    inputParameters.Append(buildDirectory);
    inputParameters.Append('"');

And then executes the process.

    var process = new Process();
    process.StartInfo = new ProcessStartInfo(msBuildPath);
    var frameworkType = Helpers.CheckProjectFramework(projectDirectory);
    process.StartInfo.Arguments =     
    Helpers.BuildInputParameters(projectDirectory, frameworkType);
    process.Start();

buildSourcePath -> .xml path
MSBuildSolutionDirectory -> Root path of the .sln which we are going to build
buildDirectory -> Location of the nuget.exe folder.

And in parallel creating a Log files to get the build output. I'm able to build my application.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正确的MSBuild.exe位置,以使用命令行构建.net 4.5应用程序 - Correct location of MSBuild.exe to build .net 4.5 application using command line 发布后事件不与msbuild.exe一起使用 - Post build event not working with msbuild.exe Visual Studio SSDT 数据库项目 (.sqlproj) 构建 - 使用没有 MSBUILD (msbuild.exe) 的 CLI 生成.dacpac - Visual Studio SSDT Database project (.sqlproj) build - generate .dacpac using CLI without MSBUILD (msbuild.exe) 使用MSBuild.exe时相当于dotnet build --version-suffix - Equivalent of dotnet build --version-suffix when using MSBuild.exe 使用 MSBuild.exe 发布 net5.0 控制台应用程序,因为 dotnet 无法正常工作 - Using MSBuild.exe to release a net5.0 console application because dotnet not working Bamboo随机运行不同版本的msbuild.exe并生成失败 - Bamboo randomly runs different version of msbuild.exe and build fails 在 Jenkins 中构建后无法识别 msbuild.exe 命令 - msbuild.exe is not recognized command after build in Jenkins 在Dev和Jenkins机器之间使用不同版本的MSBUILD.exe - Using different version of MSBUILD.exe between Dev and Jenkins machine VS2019 MSBuild.exe-使用PublishProfile时ASP .Net MVC项目无法发布,但使用OutDir参数时可以使用 - VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parameter 使用BuildTools_Full.exe安装时,Windows中的MSBuild.exe安装在哪里? - Where is MSBuild.exe installed in Windows when installed using BuildTools_Full.exe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM