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

时间:2016-11-08 13:20:54

标签: .net msbuild

由于我的项目要求,我需要创建一个接受解决方案文件路径,目标框架和其他输入参数的应用程序,并在我给出的自定义路径上调用msbuild.exe。

我是这样做的。

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();

1 个答案:

答案 0 :(得分:0)

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.