C# 如何在构建后自动执行重复性任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15774/
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
How do I automate repetitive tasks post-build?
提问by MaseBase
I run an ASP.NET website solution with a few other projects in it. I've known that MSBuild projects are capable of this, but is it the best way? Are they easy to create? Is nAnt, CruiseControl.NET or any other solution better?
我运行一个 ASP.NET 网站解决方案,其中包含一些其他项目。我知道 MSBuild 项目能够做到这一点,但这是最好的方法吗?它们容易创建吗?nAnt、CruiseControl.NET 或任何其他解决方案更好吗?
When I build the site (using Web Deployment Projects), can I automate part of the build so that it does not copy certain folders from the project into the Release folder? For instance, I have folders with local search indexes, images and other content part of the folder, but I never need or upload those when deploying the project.
当我构建站点时(使用Web 部署项目),我可以自动化构建的一部分,以便它不会将项目中的某些文件夹复制到发布文件夹中吗?例如,我有包含本地搜索索引、图像和文件夹其他内容部分的文件夹,但在部署项目时我从不需要或上传这些。
I'm also looking toward this type of solution to automatically increment build and version numbers.
我也在寻找这种类型的解决方案来自动增加构建和版本号。
采纳答案by Chris
Here's an example of a Web Deployment Project scripting this sort of task in the .wdproj file:
以下是在 .wdproj 文件中编写此类任务脚本的 Web 部署项目示例:
<Target Name="AfterBuild">
<!-- ============================ Script Compression============================ -->
<MakeDir Directories="$(OutputPath)\compressed" />
<Exec Command="java -jar c:\yuicompressor-2.2.5\build\yuicompressor-2.2.5.jar --charset UTF-8 styles.css -o compressed/styles.css" WorkingDirectory="$(OutputPath)" />
<Exec Command="move /Y .\compressed\* .\" WorkingDirectory="$(OutputPath)" />
<RemoveDir Directories="$(OutputPath)\sql" />
<Exec Command="c:zip-4.4.2za.exe a $(ZipName).zip $(OutputPath)\*" />
</Target>
This would allow you to delete a folder.
这将允许您删除文件夹。
(I suspect that if you wanted to not have the folder copy over at all, the solution file would be the place to specify that, though I haven't had to use that.)
(我怀疑,如果你想没有将该文件夹复制了所有的解决方案文件是指定的地方,虽然我还没有使用的。)
回答by xanadont
CruiseControl.NET solves a different problem (continuous integration) ... however, I've had great success with NAnt for specifically what you're asking. There's a learning curve, but once you get proficient you'll wonder how you ever got along w/o it.
CruiseControl.NET 解决了一个不同的问题(持续集成)……但是,我在 NAnt 上取得了巨大的成功,特别是您的要求。有一个学习曲线,但一旦你熟练了,你会想知道你是如何在没有它的情况下相处的。
回答by Fredrik Kalseth
You can set the Build Action/Copy to Output Directory property on individual files (select the file and hit F4 to open the properties window) to control what happens to them during build, but not for folders. This could probably be automated with a (pre) build task if you don't want to do it manually.
您可以在单个文件上设置 Build Action/Copy to Output Directory 属性(选择文件并按 F4 打开属性窗口)以控制在构建期间对它们进行的操作,但不适用于文件夹。如果您不想手动完成,这可能可以通过(预)构建任务自动完成。
Alternatively, you can exclude these folders from the project (right click and 'exclude from project'); they'll still be there ("show all files" in solution explorer), but they won't be included when building the project.
或者,您可以从项目中排除这些文件夹(右键单击并“从项目中排除”);它们仍然存在(在解决方案资源管理器中“显示所有文件”),但在构建项目时不会包含它们。
回答by Mark Cidade
In addition to @Fredrik's tip about setting project items to "Copy to Output Directory", you can also specify a post-build action in the project's properties in the Build tab and include CMD commands like copy.exe and move.exe.
除了@Fredrik 关于将项目项设置为“复制到输出目录”的提示之外,您还可以在“构建”选项卡的项目属性中指定构建后操作,并包含 CMD 命令,如 copy.exe 和 move.exe。
回答by Michael Pryor
We use FinalBuilder to automate a bunch of post build / pre build tasks. There's also a web interface so you can kick off builds (or push websites) by logging in to the web site and clicking a button.
我们使用 FinalBuilder 来自动化一系列后期构建/预构建任务。还有一个 Web 界面,因此您可以通过登录网站并单击按钮来启动构建(或推送网站)。
回答by Mark Cidade
Can't you edit the Web Deployment project's MSBuild file for it to do what you want?
您不能编辑 Web 部署项目的 MSBuild 文件以使其执行您想要的操作吗?
回答by icelava
MaseBase, you can use Web Deployment Projectsto build and package Web Sites. We do that all the time for projects with a web application aspect. After you assign a WDP to a Web Site, you can open up the .wdprojfile as plain-text XML file. At the end is a commented section of MSBuild targets that represent the sequence of events that fire during a build process.
MaseBase,您可以使用Web 部署项目来构建和打包网站。我们一直在为具有 Web 应用程序方面的项目这样做。将 WDP 分配给网站后,您可以将.wdproj文件作为纯文本 XML 文件打开。最后是 MSBuild 目标的注释部分,表示在构建过程中触发的事件序列。
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.WebDeployment.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="BeforeMerge">
</Target>
<Target Name="AfterMerge">
</Target>
<Target Name="AfterBuild">
</Target>
-->
You can uncomment the targets you want (e.g. "AfterBuild") and insert the necessary tasks there to carry out your repeated post-build activities.
您可以取消注释您想要的目标(例如“AfterBuild”)并在那里插入必要的任务以执行重复的构建后活动。