什么是Rake?
Rake是一个Ruby的构建工具,也是一个用于项目构建的领域专用语言(DSL)。(可以阅读Martin Fowler《Using The Rake Build Language》)。
Ruby程序员喜欢用Rake作为构建工具,在Rake中你可以声明很多task,也可以声明这些task之间的相互依赖关系,语法简单明了。其实,Rake不仅仅可以用来作为Ruby的构建工具,它可以用于其他语言项目的构建。当然,我不是希望说服所有程序员在他们最擅长的语言下面都使用rake进行构建。但是,玩玩儿还是不错的。说不定还可能因此喜欢上rake了呢。下面分别以不同语言的最简程序为例:(https://github.com/kiwiwin/rake_build)
使用Rake构建C
hello.c
#include <stdio.h>
int main() {
printf("Hello World\n");
return 0;
}
毫无疑问,make是C/C++程序员常用的工具,编译这个C程序可以使用一条命令:gcc hello.c -o hello
但是为了说明如何使用rake进行C构建,这里把上面的命令拆成两条命令:gcc -c hello.c; gcc hello.o -o hello
使用Rakefile
file 'hello.o' => 'hello.c' do
`gcc -c hello.c`
end
task :make => 'hello.o' do
`gcc hello.o -o hello`
end
task :clean do
`rm -f *.o hello`
end
task :run => [:clean, :make]do
puts `./hello`
end
task :default => :run
在console中调用rake,就得到hello world了。
使用Rake构建Java
Hello.java
public class Hello {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Java程序员常用Ant(http://ant.apache.org/)构建。
<project name="Hello" default="dist" basedir=".">
<description>
simple example build Hello World
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/Hello.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
<target name="run" depends="dist"
description="run HelloWorld">
<java classname="Hello">
<classpath>
<pathelement location="${dist}/lib/Hello.jar" />
</classpath>
</java>
</target>
</project>
我们当然可以用类似构建C的方法构建Java,但好消息是,我们有更简单更直观的方法使用Rake构建Java。这需要我们首先安装一个jruby( http://www.jruby.org/)。jruby是运行在JVM上的ruby。jruby自己已经戴上了定义好的ant task。下面用rake构建一个Java的Hello World。
require 'ant'
PROJECT_NAME = 'Hello'
SRC_DIR = 'src'
BUILD_DIR = 'build'
DIST_DIR = 'dist'
task :default => :dist
task :init do
ant.tstamp
ant.mkdir :dir => BUILD_DIR
end
task :compile => :init do
ant.javac :srcdir => SRC_DIR, :destdir => BUILD_DIR
end
task :dist => :compile do
ant.mkdir :dir => "#{DIST_DIR}/lib"
ant.jar :jarfile => "#{DIST_DIR}/lib/#{PROJECT_NAME}.jar", :basedir => "#{BUILD_DIR}"
end
task :clean do
ant.delete :dir => BUILD_DIR
ant.delete :dir => DIST_DIR
end
task :run => :dist do
ant.java :classname => "#{PROJECT_NAME}" do
classpath :location => "#{DIST_DIR}/lib/#{PROJECT_NAME}.jar"
end
end
运行task:jruby -S rake [task_name]
我倒是觉得第二种方式看起来更简单,更明了。build.xml中想传达的意思很容易就被xml的< >干扰了,不如rake中的直观。
同时,不得不赞叹下jruby中的ant的强大,你不仅可以单独顶一个类似ant的rakefile,你还可以让rake调用ant,也可以让ant调用rake,如果很感兴趣,可以看下这篇文章 《Rake and Ant Together》使用Rake构建Ruby
ruby程序员应该都很熟悉rake了,就不啰嗦了
(未完待续,使用Rake构建 C#)