Ruby Rake: Task[:javac]

I write a :javac task for rake. PLEASE try it.
The file structure is like ANT, the complied classe will be stored under build/classes.

USAGE:
1.copy the following source into java.rb
2.put
"require 'path/to/java.rb'" into your rakefile.rb
3.type
"rake javac"
then this will complie all java file under the rake base dir,
and copy all classes into 'build/classes'

SOURCE:java.rb
#!/usr/bin/env ruby

# The 'rake/java.rb' file defines the [:javac] task and its helper
# file.
#
# [:javac] complie all .java file in the working directory into
# CLASSDIR(defaults is "build/classes")
#
# version 0.1
# fhope@2006/9/3 03:01:35pm
require 'rake'
require 'rake/clean.rb'
def java_package_of(javafile)
file=File.new(javafile)
file.each do |line|
return line.sub(/package /,'').sub(/;/,'').sub(/\s/,'') if line =~ /^package/
end
return '' # empty string mean no package
end
def java_path(javafile)
return java_package_of(javafile).sub(/\./, '/')
end

JAVAC="javac"
JAVA=FileList["**/*.java"]
JARS=FileList["**/*.jar"]
CLASSDIR="build/classes"
CLASSPATH=JARS.to_a + [CLASSDIR]
CLASS = JAVA.collect do |fn|
puts fn
dir=CLASSDIR
package_path=java_path(fn)
if package_path != '' then dir="#{CLASSDIR}/#{java_path(fn)}" end
"#{dir}/#{fn.pathmap('%n')}.class"
end

def find_java(classfile)
base = File.basename(classfile, '.class')
JAVA.find { |s| File.basename(s, '.java') == base }
end

rule '.class' => lambda{ |classfile| find_java(classfile)} do |t|
classpath = CLASSPATH.join(';')
basedir=Rake.original_dir
tdir = basedir+"/"+ CLASSDIR
tf=basedir+"/"+t.source
sh "#{JAVAC} -classpath #{classpath} -d #{tdir} #{tf}"
end

desc "complie java file"
directory CLASSDIR
task :javac => [CLASSDIR] + CLASS

CLOBBER.add("#{CLASSDIR}/**/*.class")

留言

這個網誌中的熱門文章

浴室水龍頭切換拉桿維修

【麵】的倉頡碼

投資現況