一个去年写的小tips,一个利用CruiseControl.NET做baseline的技巧, in english.
2007-04-16 10:35
351 查看
Tips: .NET Code Makes Nant Build More Powerful
Introduction
Many projects have started using Cruisecontrol .NET and Nant to build the baselines. We can do a lot with the functions provided by them. But still, in some cases, it will be hard or too tedious to implement some specific feature that is required by us. Luckily, Nant has provided the support of embedded .NET code (in C# or VB.NET). I'd like to cover this topic and provide some examples in the article.How-To
When setting up the baseline for my project, we faced the issue of modifying the assemblies’ version number, based on the build number we have. Normally, the version info locates at the AssemblyInfo.cs (at least for our project J). But unfortunately, we can not do the version modification manually because it is so frequent.In the previous baseline which is not Cruisecontrol based, there is some complex perl language for this purpose (The word “complex” is from my point of view, because I do not know much about it). It will be hard for me to do it this way and I tried to look for other solutions. Fortunately, I got one.
The solution comes from the fact that Nant can support some languages as script, compile them at runtime and call them as target tasks. The languages can be: VB.NET, C#, etc. Of course, when we want to use the language, we should have the compiler or interpreter on the baseline machine.
With no doubt, I chose the C# because it’s what I’m familiar with. For .NET, we have to reference some assemblies and add some namespaces in our code. Luckily, Nant has included some default assemblies and references for us, which is enough in most cases, at least in my case. They are:
System
System.Collections
System.Collections.Specialized
System.IO
System.Text
System.Text.RegularExpressions
NAnt.Core
If you want to add some extra assemblies and references, you can do like this:
<script language="C#" >
<references>
<include name="System.Data.dll" />
</references>
<imports>
<import namespace="System.Data.SqlClient" />
</imports>
<code>
<![CDATA[
//Add code here.
]]>
</code>
</script>
The sample code above adds the reference “System.Data.Dll” and the namespace “System.Data.SqlClient”.
With the knowledge we have now, we can start to write code for some special tasks. No matter what kind of work we want our code to do, we should define a class and have an entry method called ScriptMethod, which t
4000
akes one Project parameter. The Project is defined by Nant, for accessing the properties we defined in other Nant script.
Following is the code that I used to build the task called “SetVersion”. Forgive me because of the hidden issues
<target name="SetVersion">
<script language="C#" mainclass="VersionSetter">
<code>
<![CDATA[
//The following code use c# script to modify the assembly version.
class VersionSetter
{
public static void ScriptMain(Project project)
{
string version2003 = "1.0.*"; //For version of VS2005;
string version2005 = "1.0.0.0"; //For version of VS2003;
//We can access some predefined properties through project
string newVersion = project.Properties["WellScanVersion"];
string srcDir = project.Properties["src.dir"];
string[] aiFiles = Directory.GetFiles(srcDir, "*AssemblyInfo.cs",
SearchOption.AllDirectories);
//Parse the AssemblyInfo files one by one
foreach (string aiFile in aiFiles)
{
FileInfo asmFile = new FileInfo(aiFile);
string contents = "";
using (StreamReader reader = asmFile.OpenText())
contents = reader.ReadToEnd();
//Set new version.
contents = contents.Replace(version2003, newVersion);
contents = contents.Replace(version2005, newVersion);
if ((asmFile.Attributes & FileAttributes.ReadOnly)
== FileAttributes.ReadOnly)
asmFile.Attributes = asmFile.Attributes ^ leAttributes.ReadOnly;
asmFile.Delete();
//Write as the new AssemblyInfo file.
using (FileStream stream = asmFile.Create())
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(contents);
writer.Flush();
}
}
}
}
]]>
</code>
</script>
<echo message="Set version to ${MYPROJECT}"/>
</target>
At the end of the article, I’d like to mention that because we can not debug the code which locates in the Nant script, we’d better to make our code work in the VS IDE first, which can help to reduce the time effort.
For more information, please read the doc at NantDir/doc/help/tasks/script.html.
相关文章推荐
- How to Hook Up a VS.NET 2005 Solution With CruiseControl.NET in a Few Minutes
- FxCop in CruiseControl.NET
- How to use For loop in CruiseControl.net
- CruiseControl.net相关
- 利用 HttpModule,基于输出,统一控制、干预、处理(例如: 过滤关键字、AntiXSS) ASP.Net WebForm Control 展现属性的方案原型
- 在 Widnows Server 2008R2 下安装 CruiseControl.NET 1.5
- 项目管理实践【三】每日构建【Daily Build Using CruiseControl.NET and MSBuild】
- Cannot construct net.sourceforge.cruisecontrol.BuildLoopInformation as it does not have a no-args co
- [转]SVN + CruiseControl.NET + NANT 自动编译提交的项目最小DEMO
- 转:如何用CruiseControl.Net来进行持续化集成
- [转]如何用CruiseControl.Net来进行持续化集成
- Using the ReportViewer ASP.NET Control in Windows Azure
- 利用 CSS Friendly Control Adapters 将 ASP.NET 控件转为符合WEB标准标签控制
- CC 异常: net.sourceforge.cruisecontrol.CruiseControlException: Encountered an IO exception while attempting to execute Solution
- 转: Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About (一些简单的、你可能已经知道或者不知道的ASP.NET 2.0技巧)
- [ASP.NET AJAX]Important notes when we create web service methods for some extenders in AjaxControlToolkit package
- 在CruiseControl.net 中用 devenv.com 自动生成 *.msi 安装文件
- Build User rights control system in Asp.net.(Windows Form Authentication)
- A Beginner's Tutorial - Understanding ControlState in ASP.NET
- Nine tips for a healthy "in production" ASP.NET application