buildr 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/java/java.rb ADDED
@@ -0,0 +1,117 @@
1
+ module Buildr
2
+ module Java
3
+
4
+ JAVA_OPTIONS = [ :verbose, :noop, :cp, :classpath, :name, :java_args ]
5
+
6
+ def self.java(*args)
7
+ options = Hash === args.last ? args.pop : {}
8
+ options[:verbose] ||= Rake.application.options.trace || false
9
+ fu_check_options options, *JAVA_OPTIONS
10
+
11
+ name = options[:name] || "java #{args.first}"
12
+ cmd_args = []
13
+ classpath = classpath_from(options)
14
+ cmd_args << "-cp" << classpath unless classpath.empty?
15
+ cmd_args += options[:java_args].flatten if options[:java_args]
16
+ cmd_args += args.flatten.compact
17
+ cmd_args << { :verbose=>options[:verbose] }
18
+ unless options[:noop]
19
+ verbose { puts "Running #{name}" }
20
+ sh(path_to_bin("java"), *cmd_args) { |ok, res| fail "Failed to execute #{name}, see errors above" unless ok }
21
+ end
22
+ end
23
+
24
+ def self.apt(*args)
25
+ options = Hash === args.last ? args.pop : {}
26
+ options[:verbose] ||= Rake.application.options.trace || false
27
+ fu_check_options options, :verbose, :noop, :compile, :source, :output, :cp, :classpath, :sourcepath
28
+
29
+ files = args.collect { |arg| File.directory?(arg) ? FileList[File.join(arg, "**", "*.java")] : arg }.flatten
30
+ args = [ options[:verbose] ? "-verbose" : "-nowarn" ]
31
+ if options[:compile]
32
+ args << "-d" << options[:output]
33
+ else
34
+ args << "-nocompile" << "-s" << options[:output]
35
+ end
36
+ args << "-source" << options[:source] if options[:source]
37
+ classpath = classpath_from(options)
38
+ args << "-cp" << classpath unless classpath.empty?
39
+ sourcepath = (options[:sourcepath] || []).collect.join(File::PATH_SEPARATOR)
40
+ args << "-sourcepath" << sourcepath unless sourcepath.empty?
41
+ args += files
42
+ args << { :verbose=>options[:verbose] }
43
+ unless options[:noop]
44
+ verbose { puts "Running apt" }
45
+ sh(path_to_bin("apt"), *args) { |ok, res| fail "Failed to execute apt, see errors above" unless ok }
46
+ end
47
+ end
48
+
49
+ class AptTask < Rake::FileTask
50
+
51
+ def initialize(*args)
52
+ super
53
+ enhance do |task|
54
+ Java.apt *task.prerequisites + [options.merge(:output=>task.name)]
55
+ end
56
+ end
57
+
58
+ def options()
59
+ @options ||= {}
60
+ end
61
+
62
+ def using(options)
63
+ self.options.merge!(options)
64
+ self
65
+ end
66
+
67
+ end
68
+
69
+ def self.apt_task(args)
70
+ output = args.keys.first
71
+ files = args.values.first.collect { |f| File.directory?(f) ? FileList[f + "/**/*.java"] : f }.flatten
72
+ AptTask.define_task(output=>files)
73
+ end
74
+
75
+ def self.javac(*args)
76
+ options = Hash === args.last ? args.pop : {}
77
+ options[:verbose] ||= Rake.application.options.trace || false
78
+ fu_check_options options, :verbose, :noop, :cp, :classpath, :sourcepath, :output, :javac_args, :name
79
+
80
+ files = args.flatten.each { |f| f.invoke if f.respond_to?(:invoke) }.map(&:to_s).
81
+ collect { |arg| File.directory?(arg) ? FileList[File.join(arg, "**", "*.java")] : arg }.flatten
82
+ name = options[:name] || Dir.pwd
83
+
84
+ cmd_args = []
85
+ classpath = classpath_from(options)
86
+ #classpath += options[:output] if options[:output]
87
+ cmd_args << "-cp" << classpath unless classpath.empty?
88
+ cmd_args << "-sourcepath" << options[:sourcepath].join(File::PATH_SEPARATOR) if options[:sourcepath]
89
+ cmd_args << "-d" << options[:output] if options[:output]
90
+ cmd_args += options[:javac_args].flatten if options[:javac_args]
91
+ cmd_args += files
92
+ cmd_args << { :verbose=>options[:verbose] }
93
+ unless options[:noop] || files.empty?
94
+ verbose { puts "Compiling #{files.size} source files in #{name}" }
95
+ sh(path_to_bin("javac"), *cmd_args) { |ok, res| fail "Failed to compile, see errors above" unless ok }
96
+ end
97
+ end
98
+
99
+ protected
100
+
101
+ def self.path_to_bin(name)
102
+ ENV["JAVA_HOME"] ? File.join(ENV["JAVA_HOME"], "bin", name) : name
103
+ end
104
+
105
+ def self.classpath_from(options)
106
+ classpath = (options[:classpath] || []).collect | (options[:cp] || []).collect
107
+ artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.
108
+ map(&:to_s).join(File::PATH_SEPARATOR)
109
+ end
110
+
111
+ end
112
+
113
+ def java(*args)
114
+ Java.java(*args)
115
+ end
116
+
117
+ end
@@ -0,0 +1,88 @@
1
+ module Buildr
2
+
3
+ module JavaCC
4
+
5
+ JAVACC = "net.java.dev.javacc:javacc:jar:4.0"
6
+ JJTREE = "net.java.dev.javacc:javacc:jar:4.0"
7
+
8
+ def self.javacc(*args)
9
+ options = Hash === args.last ? args.pop.clone : {}
10
+ options[:verbose] ||= Rake.application.options.trace || false
11
+ fu_check_options options, *Java::JAVA_OPTIONS + [:output]
12
+
13
+ (options[:classpath] ||= []) << JAVACC
14
+ java_args = ["javacc"]
15
+ java_args << "-OUTPUT_DIRECTORY=#{options[:output]}" if options[:output]
16
+ java_args += args.collect { |f| File.directory?(f) ? FileList[f + "/**/*.jj"] : f }.flatten
17
+ java_args << options.reject { |k, v| !Java::JAVA_OPTIONS.include?(k) }
18
+ Java.java(*java_args)
19
+ end
20
+
21
+ class JavaCCTask < Rake::FileTask
22
+
23
+ def initialize(*args)
24
+ super
25
+ enhance do |task|
26
+ JavaCC.javacc *(task.prerequisites + [task.options.merge(:output=>task.name)])
27
+ end
28
+ end
29
+
30
+ def options()
31
+ @options ||= {}
32
+ end
33
+
34
+ def using(options)
35
+ self.options.merge!(options)
36
+ self
37
+ end
38
+
39
+ end
40
+
41
+ def self.javacc_task(args)
42
+ output = args.keys.first
43
+ files = args.values.first.collect { |f| File.directory?(f) ? FileList[f + "/**/*.jj"] : f }.flatten
44
+ JavaCCTask.define_task(output=>files)
45
+ end
46
+
47
+ def self.jjtree(*args)
48
+ options = Hash === args.last ? args.pop.clone : {}
49
+ options[:verbose] ||= Rake.application.options.trace || false
50
+ fu_check_options options, *Java::JAVA_OPTIONS + [:output, :build_node_files]
51
+
52
+ (options[:classpath] ||= []) << JJTREE
53
+ java_args = ["jjtree"]
54
+ java_args << "-OUTPUT_DIRECTORY=#{options[:output]}" if options[:output]
55
+ java_args << "-BUILD_NODE_FILES=#{options[:build_node_files]}" if options.has_key?(:build_node_files)
56
+ java_args += args.collect { |f| File.directory?(f) ? FileList[f + "/**/*.jjt"] : f }.flatten
57
+ java_args << options.reject { |k, v| !Java::JAVA_OPTIONS.include?(k) }
58
+ Java.java(*java_args)
59
+ end
60
+
61
+ class JJTreeTask < Rake::FileTask
62
+
63
+ def initialize(*args)
64
+ super
65
+ enhance do |task|
66
+ JavaCC.jjtree *(task.prerequisites + [task.options.merge(:output=>task.name)])
67
+ end
68
+ end
69
+
70
+ def options()
71
+ @options ||= {}
72
+ end
73
+
74
+ def using(options)
75
+ self.options.merge!(options)
76
+ self
77
+ end
78
+
79
+ end
80
+
81
+ def self.jjtree_task(args)
82
+ output = args.keys.first
83
+ files = args.values.first.collect { |f| File.directory?(f) ? FileList[f + "/**/*.jjt"] : f }.flatten
84
+ JJTreeTask.define_task(output=>files)
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,52 @@
1
+ module Buildr
2
+
3
+ module OpenJPA
4
+
5
+ VERSION = "0.9.7-incubating-SNAPSHOT"
6
+
7
+ REQUIRES = [ "org.apache.openjpa:openjpa-all:jar:#{VERSION}",
8
+ "commons-collections:commons-collections:jar:3.1",
9
+ "commons-dbcp:commons-dbcp:jar:1.2.1",
10
+ "commons-lang:commons-lang:jar:2.1",
11
+ "commons-pool:commons-pool:jar:1.2",
12
+ "javax.persistence:persistence-api:jar:1.0",
13
+ "org.apache.geronimo.specs:geronimo-j2ee-connector_1.5_spec:jar:1.0",
14
+ "org.apache.geronimo.specs:geronimo-jta_1.0.1B_spec:jar:1.0",
15
+ "net.sourceforge.serp:serp:jar:1.11.0" ]
16
+
17
+ OPTIONS = [ :verbose, :noop, :cp, :classpath, :properties ]
18
+
19
+ def self.enhance(options)
20
+ options[:verbose] ||= Rake.application.options.trace || false
21
+ fu_check_options options, *OPTIONS + [:output]
22
+
23
+ runtool options.merge(:class=>"org.apache.openjpa.enhance.PCEnhancer", :name=>"Enhancer",
24
+ :args=>{ "-p"=>options[:properties], "-d"=>options[:output] })
25
+ end
26
+
27
+ def self.mapping_tool(options)
28
+ options[:verbose] ||= Rake.application.options.trace || false
29
+ fu_check_options options, *OPTIONS + [:action, :sql]
30
+
31
+ runtool options.merge(:class=>"org.apache.openjpa.jdbc.meta.MappingTool", :name=>"Mapping Tool",
32
+ :args=>{ "-p"=>options[:properties], "-sql"=>options[:sql], "-sa"=>options[:action] })
33
+ end
34
+
35
+ protected
36
+
37
+ def self.runtool(options)
38
+ classpath = REQUIRES + (options[:classpath] || []).collect | (options[:cp] || []).collect
39
+ classpath = artifacts(classpath).each { |t| t.invoke if t.respond_to?(:invoke) }.map(&:to_s)
40
+ cmd_args = ["-cp", classpath.join(File::PATH_SEPARATOR)]
41
+ cmd_args << options[:class]
42
+ cmd_args += options[:args].select { |n, v| v }.map { |n, v| [ n, v ] }.flatten
43
+ cmd_args << { :verbose=>options[:verbose] }
44
+ unless options[:noop]
45
+ verbose { puts "Running OpenJPA #{options[:name]}" }
46
+ sh(Java.path_to_bin("java"), *cmd_args) { |ok, res| fail "Failed to execute OpenJPA #{options[:name]}, see errors above" unless ok }
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,217 @@
1
+ module Buildr
2
+ module Java
3
+
4
+ module Packaging
5
+
6
+ MANIFEST_HEADER = "Manifest-Version: 1.0\nCreated-By: Ruby Build System"
7
+
8
+ class JarTask < ZipTask
9
+ attr_accessor :manifest
10
+
11
+ def initialize(*args)
12
+ super
13
+ @manifest = true
14
+ end
15
+
16
+ def []=(key, value)
17
+ if key.to_sym == :manifest
18
+ self.manifest = value
19
+ else
20
+ super key, value
21
+ end
22
+ value
23
+ end
24
+
25
+ protected
26
+
27
+ def create(zip)
28
+ if manifest
29
+ zip.mkdir "META-INF"
30
+ zip.file.open("META-INF/MANIFEST.MF", "w") do |output|
31
+ output.write MANIFEST_HEADER
32
+ case manifest
33
+ when Hash
34
+ output.write manifest.map { |pair| pair.join(": ") }.join("\n")
35
+ when Array
36
+ manifest.each do |section|
37
+ output.write "\n"
38
+ output.write section.map { |pair| pair.join(": ") }.join("\n")
39
+ end
40
+ when Proc, Method
41
+ output.write manifest.call
42
+ when String
43
+ output.write File.read(manifest)
44
+ when true # Default behavior
45
+ end
46
+ output.write "\n"
47
+ end
48
+ end
49
+ super zip
50
+ end
51
+
52
+ end
53
+
54
+ class WarTask < JarTask
55
+
56
+ def []=(key, value)
57
+ case key.to_sym
58
+ when :libs
59
+ self.include value, :path=>"WEB-INF/lib"
60
+ when :classes
61
+ self.include value, :path=>"WEB-INF/classes"
62
+ else
63
+ super key, value
64
+ end
65
+ value
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+
74
+ # Create a JAR from all the pre-requisites.
75
+ #
76
+ # For example:
77
+ # jar "lib-1.0.jar"=>["target/classes", "MANIFEST,MF"]
78
+ def jar(file)
79
+ Packaging::JarTask.define_task(file)
80
+ end
81
+
82
+
83
+ # Global task packages all projects.
84
+ desc "Package all projects"
85
+ LocalDirectoryTask.define_task "package"
86
+ desc "Install packages created by this project"
87
+ LocalDirectoryTask.define_task "install"
88
+ desc "Uninstall packages created by this project"
89
+ LocalDirectoryTask.define_task "uninstall"
90
+ desc "Deploy packages created by this project"
91
+ LocalDirectoryTask.define_task "deploy"
92
+
93
+
94
+ class Project
95
+
96
+ # Group used for packaging. Inherited from parent project.
97
+ inherited_attr :group
98
+ # Version used for packaging. Inherited from parent project.
99
+ inherited_attr :version
100
+
101
+ inherited_attr :webapp_src_dir do File.join(src_dir, "main", "webapp") end
102
+
103
+ def package(*args)
104
+ if Hash === args.last
105
+ options = args.pop.clone
106
+ else
107
+ options = {}
108
+ end
109
+ options[:type] = args.shift if Symbol === args.first
110
+ fail "No packaging type specified" unless options[:type]
111
+ options[:group] ||= self.group
112
+ options[:version] ||= self.version
113
+ options[:id] ||= self.id
114
+ if String === args.first
115
+ file = args.shift
116
+ else
117
+ file = options.delete(:file) || path_to(:target_dir, Artifact.hash_to_file_name(options))
118
+ end
119
+ fail "One argument too many; expecting at most type, file name, and hash of options" unless args.empty?
120
+
121
+ packager = method("package_as_#{options[:type]}") rescue
122
+ fail("Do not know how to create a package of type #{options[:type]}")
123
+ package = packager.call(file, options) or fail("Do not know how to create a package of type #{options[:type]}")
124
+
125
+ task("package").enhance [package]
126
+ package.enhance [task("build")]
127
+
128
+ task "install"=>[
129
+ file_create(File.dirname(repositories.locate(package))) { |task| mkpath task.name, :verbose=>false },
130
+ file(repositories.locate(package)=>package) { |task| cp package.name, task.name },
131
+ package.pom
132
+ ]
133
+
134
+ task "uninstall" do |task|
135
+ verbose(Rake.application.options.trace) do
136
+ rm repositories.locate(package)
137
+ rm repositories.locate(package.pom)
138
+ end
139
+ end
140
+
141
+ task "deploy"=>deploy(package, package.pom)
142
+
143
+ packages << package
144
+ Artifact.register package
145
+ package
146
+ end
147
+
148
+ def packages()
149
+ @packages ||= []
150
+ end
151
+
152
+ protected
153
+
154
+ def package_as_jar(file, options)
155
+ returning(Java::Packaging::JarTask.define_task(file)) do |task|
156
+ package_extend task, options
157
+ task.include path_to(:java_target_dir, "*")
158
+ task.include options[:include] if options[:include]
159
+ task.manifest = options[:manifest] if options[:manifest]
160
+ end
161
+ end
162
+
163
+ def package_as_war(file, options)
164
+ # Add anything we find in webapp sources directory.
165
+ returning(Java::Packaging::WarTask.define_task(file)) do |task|
166
+ package_extend task, options
167
+ task.include path_to(:webapp_src_dir, "*")
168
+ task.include options[:include] if options[:include]
169
+ # Add libraries in WEB-INF lib, and classes in WEB-INF classes
170
+ task.with :libs=>options[:libs].collect if options[:libs]
171
+ task.with :classes=>(options[:classes] || path_to(:java_target_dir, "**")) unless options[:classes] == false
172
+ end
173
+ end
174
+
175
+ def package_as_zip(file, options)
176
+ returning(ZipTask.define_task(file)) do |task|
177
+ package_extend task, options
178
+ task.include path_to(:java_target_dir, "*")
179
+ task.include options[:include] if options[:include]
180
+ end
181
+ end
182
+
183
+ def package_extend(task, spec)
184
+ task.extend ActsAsArtifact
185
+ task.apply_spec spec
186
+ task.pom.enhance do |task|
187
+ mkpath File.dirname(task.name), :verbose=>false
188
+ File.open(task.name, "w") do |file|
189
+ xml = Builder::XmlMarkup.new(:target=>file, :indent=>2)
190
+ xml.instruct!
191
+ xml.project do
192
+ xml.modelVersion "4.0.0."
193
+ xml.groupId spec[:group]
194
+ xml.artifactId spec[:id]
195
+ xml.version spec[:version]
196
+ xml.classifier spec[:classifier] if spec[:classifier]
197
+ end
198
+ end
199
+ end
200
+ end
201
+
202
+ end
203
+
204
+
205
+ Project.on_create do |project|
206
+ # Recursive tasks must be defined in before_create.
207
+ desc "Create packages in this project"
208
+ project.recursive_task "package"=>"build"
209
+ desc "Install packages created by this project"
210
+ project.recursive_task "install"=>"package"
211
+ desc "Uninstall packages created by this project"
212
+ project.recursive_task "uninstall"
213
+ desc "Deploy packages created by this project"
214
+ project.recursive_task "deploy"=>"package"
215
+ end
216
+
217
+ end