2016-11-08 34 views
0

由於我的項目需求,我需要創建一個應用程序接受解決方案文件路徑,目標框架和其他輸入參數,並調用msbuild.exe我給的定製路徑。使用msbuild.exe編程我的.NET解決方案並輸出日誌

我這樣做如下。

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

感謝, 西瓦

+0

*方案文件路徑,目標框架和其他輸入參數和調用的MSBuild *聽起來像一個批處理文件,即你真的需要爲一個應用程序?此外,如果您以編程方式搜索「msbuild」,您會發現所有需要的信息。 – stijn

+0

謝謝Stijn。我編寫了代碼來調用msbuild.exe並傳遞解決方案名稱。 Build正在成功。但我無法將日誌寫入日誌文件。相應地更新我的問題。 –

+0

無法重現。這段代碼肯定會產生一個日誌文件。可能你只是在錯誤的地方尋找它:因爲你沒有指定完整的路徑(你應該),日誌文件進入當前的工作目錄.. – stijn

回答

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.