MSBuild - 如何从预先编写的命令行命令构建 .NET 解决方案文件(在 XML 任务脚本中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5119913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MSBuild - How to build a .NET solution file (in an XML task script) from pre-written command line commands
提问by D3vtr0n
I have been studying MSBuild as I have the need to automate my development shop's builds. I was able to easily write a .BAT file that invokes the VS command prompt and passes my MSBuild commands to it. This works rather well and is kinda nifty.
我一直在研究 MSBuild,因为我需要自动化我的开发商店的构建。我能够轻松编写一个 .BAT 文件来调用 VS 命令提示符并将我的 MSBuild 命令传递给它。这很有效,而且有点漂亮。
Here is the contents of my .BAT build file:
这是我的 .BAT 构建文件的内容:
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vcvars64.bat"
cd C:\Sandbox\Solution
msbuild MyTopSecretApplication.sln /p:OutputPath=c:\TESTMSBUILDOUTPUT /p:Configuration=Release,Platform=x86
pause
^ This works well but I now have the need to use the MSBuild task for TeamCity CI. I have tried to write a few MSBuild scripts but I cannot get them to work the same. What is the equivalent build script to the command I am using in my .BAT file? Any ideas?
^ 这很有效,但我现在需要为 TeamCity CI 使用 MSBuild 任务。我曾尝试编写一些 MSBuild 脚本,但无法让它们正常工作。我在 .BAT 文件中使用的命令的等效构建脚本是什么?有任何想法吗?
I have tried using something like this, but no success (I know this is wrong):
我试过使用这样的东西,但没有成功(我知道这是错误的):
<?xml version="1.0"?>
<project name="Hello Build World" default="run" basedir=".">
<target name="build">
<mkdir dir="mybin" />
<echo>Made mybin directory!</echo>
<csc target="exe" output="c:\TESTMSBUILDOUTPUT">
<sources>
<include name="MyTopSecretApplication.sln"/>
</sources>
</csc>
<echo>MyTopSecretApplication.exe was built!</echo>
</target>
<target name="clean">
<delete dir="mybin" failonerror="false"/>
</target>
<target name="run" depends="build">
<exec program="mybin\MyTopSecretApplication.exe"/>
</target>
What I simply need is an MSBuild XML build script that compiles a single solution for Release mode to a specified output directory. Any help?
我只需要一个 MSBuild XML 构建脚本,它将发布模式的单个解决方案编译到指定的输出目录。有什么帮助吗?
回答by Brian Walker
Use the MSBuild task to build the solution passing the properties you need.
使用 MSBuild 任务构建传递您需要的属性的解决方案。
<?xml version="1.0" encoding="utf-8"?>
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
DefaultTargets="Build">
<PropertyGroup>
<OutputDir>c:\TESTMSBUILDOUTPUT</OutputDir>
</PropertyGroup>
<ItemGroup>
<ProjectToBuild Include="MySecretApplication.sln">
<Properties>OutputPath=$(OutputDir);Configuration=Release</Properties>
</ProjectToBuild>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="@(ProjectToBuild)"/>
</Target>
</Project>
回答by D3vtr0n
Here is my final MSBuild script:
这是我的最终 MSBuild 脚本:
<?xml version="1.0" encoding="utf-8"?>
<PropertyGroup>
<OutputDir>C:\TESTMSBUILDOUTPUT</OutputDir>
</PropertyGroup>
<ItemGroup>
<ProjectToBuild Include="MyTopSecretApplication.sln" >
<Properties>OutputPath=$(OutputDir);Configuration=MSBuildRelease;Platform=x86</Properties>
</ProjectToBuild>
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="@(ProjectToBuild)"/>
</Target>
As you can see, the main difference from Brian Walker's answer is the "Configuration" setting. Here it is set to "MSBuildRelease" instead of "Release", which I customized from within the .NET IDE. Follow the steps Brian suggested (right-click on the solution node and choose Configuration Manager in Visual Studio, add "NEW" configuration and remove/uncheck projects you want excluded.)
如您所见,与 Brian Walker 的答案的主要区别在于“配置”设置。这里它设置为“MSBuildRelease”而不是“Release”,这是我在 .NET IDE 中自定义的。按照 Brian 建议的步骤操作(右键单击解决方案节点并在 Visual Studio 中选择配置管理器,添加“新”配置并删除/取消选中要排除的项目。)
I have since uploaded this to my TEAMCITY server, and have automated my .NET build process with your help. Thanks so much to Brian Walker (a fellow Texan)...I'll buy you a beer!! CHEERS!!
我已将此上传到我的 TEAMCITY 服务器,并在您的帮助下自动化了我的 .NET 构建过程。非常感谢布赖恩沃克(德克萨斯人)......我给你买啤酒!!干杯!!
回答by Troy Hunt
I know this isn't a direct answer to your question, but why not just use the built in Visual Studio build runner and pick up the compiled output from the appropriate /obj path and save it as a build artefact? If you're effectively not doing anything beyond a simple compile against a specified build configuration then there's not much value added by having a dedicated build file and using build artefacts can make a life a whole lot easier (use them in other builds, download them easily, etc).
我知道这不是您问题的直接答案,但为什么不直接使用内置的 Visual Studio 构建运行程序并从适当的 /obj 路径中获取编译后的输出并将其保存为构建工件?如果除了针对指定的构建配置进行简单的编译之外,您实际上没有做任何事情,那么通过拥有专用的构建文件并使用构建工件可以使生活变得更加轻松(在其他构建中使用它们,下载它们)并没有增加太多价值容易等)。
Try the configuration described here if this isn't making sense: http://www.troyhunt.com/2010/11/you-deploying-it-wrong-teamcity_25.html
如果这没有意义,请尝试此处描述的配置:http: //www.troyhunt.com/2010/11/you-deploying-it-wrong-teamcity_25.html

