在用NAnt的过程中一直有个问题没弄清,那就是al.exe在NAnt中的用法。我们知道,al的用途是将不同的资源文件(如图标、本地化文件)或程序集模块等连续成一个具有清单(manifest)的.NET程序集(Assembly),但是在NAnt中具体该怎样写build文件来调用al呢?
下面为了说明方便,我先给出一个简单的例子:
file1: Client.cs
using System;
public class Client
{
public static void
{
// make sure two numbers provided
if (args.Length!=2) {
Console.WriteLine("please provide two numbers");
return;
}
// Call Helper.Add() function to make a simple calculate
int x=Convert.ToInt32(args[0]);
int y=Convert.ToInt32(args[1]);
int total=Helper.Add(x, y);
Console.WriteLine("{0} + {1} = {2}.", x, y, total);
}
}
file2: Helper.cs
public class Helper
{
public static int Add(int x, int y)
{
return x+y;
}
}
上面是一个很简单的加法运算的例子,为了用到al.exe,我把客户端调用(Client)和业务逻辑(Helper)分离在两个单独的.cs文件中。传统的.NET SDK编译方法是:
1. 编译Helper.cs
csc /t:module Helper.cs
2. 编译Client.cs
csc /t:module /addmodule: Helper.netmodule Client.cs
3. 连接
al Helper.netmodule Client.netmodule /t:exe /out:HelperDemo.exe /main:Client.Main
这样三步下来就可以使用生成的HelperDemo.exe了。不过上面这些步骤在NAnt中该怎样实现呢?我查看了NAnt安装目录下的文档,其中在
这个问题的确让我很费脑筋。我想自己可能钻牛角尖了,因为NAnt一定会提供一个非常方便、非常直接的连接机制的。今天上午,当我再次用NAnt撰写build文件时忽然想起了什么——在
于是我写成了下面的build文件:
<project name="HelperDemoBuild" default="build" >
<property name="file1" value="Client" />
<property name="file2" value="Helper" />
<property name="file3" value="HelperDemo" />
<target name="build">
<csc target="exe" output="${file3}.exe" main="Client" verbose="true">
<sources>
<includes name="${file1}.cs" />
<includes name="${file2}.cs" />
</sources>
</csc>
</target>
</project>
调用NAnt.exe,结果运行成功!
总结:从底层上来看,NAnt的所有操作都是在间接地调用.NET SDK(当然这仅限于编译环节,NAnt在测试、生成文档等环节将会与NUnit、NDoc合作),因此这使得NAnt的build文件结构与.NET SDK中相应工具(如csc.exe、al.exe等)的命令行操作方式非常相近,但也应注意到两者之间有着很多的差异,因此遇到问题时不能想当然地按照.NET SDK的思路来,而应认真地研读NAnt的文档,多动手实践。
打印 | 张贴于 2004-01-20 12:37:00 | Tag:技术
留言反馈