jbundler 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Build.md +32 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +35 -0
- data/Gemfile.lock- +33 -0
- data/Readme.md +88 -0
- data/lib/jbundler.jar +0 -0
- data/lib/jbundler.rb +25 -20
- data/lib/jbundler.rb~ +2 -0
- data/lib/jbundler/aether.rb +44 -15
- data/lib/jbundler/aether.rb~ +68 -0
- data/lib/jbundler/classpath_file.rb +2 -4
- data/lib/jbundler/classpath_file.rb~ +207 -0
- data/lib/jbundler/gemfile_lock.rb +4 -4
- data/lib/jbundler/gemfile_lock.rb~ +17 -0
- data/lib/jbundler/maven.rb~ +252 -0
- data/lib/jbundler/maven_gemify3.rb~ +337 -0
- data/lib/jbundler/{maven_util.rb → maven_util.rb~} +4 -14
- data/lib/jbundler/maven_version.rb~ +4 -0
- data/lib/jbundler/mavenfile.rb~ +9 -0
- data/lib/jbundler/pom.rb +2 -2
- data/lib/jbundler/pom.rb~ +251 -0
- data/spec/aether_spec.rb +78 -0
- data/spec/{mavenfile_spec.rb → aether_spec.rb~} +12 -14
- data/spec/classpath_file_spec.rb +26 -24
- data/spec/classpath_file_spec.rb~ +81 -0
- data/spec/{maven_util_spec.rb → maven_util_spec.rb~} +1 -12
- data/spec/mavenfile_spec.rb~ +40 -0
- data/spec/pom_spec.rb +2 -0
- data/spec/pom_spec.rb~ +40 -0
- data/spec/setup.rb +3 -0
- data/spec/setup.rb~ +3 -0
- metadata +82 -54
- data/lib/jbundler/mavenfile.rb +0 -77
@@ -0,0 +1,337 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rubygems/builder'
|
6
|
+
require 'rubygems/installer'
|
7
|
+
require 'set'
|
8
|
+
|
9
|
+
# A modified maven_gemify
|
10
|
+
module Gem
|
11
|
+
|
12
|
+
class Maven3NotFound < StandardError; end
|
13
|
+
|
14
|
+
#This is some of the worst stuff. I had to this because bundler will search
|
15
|
+
#the index of specifications based on the name given in the GemFile which
|
16
|
+
#includes the "mvn:<groupid>:<artifactid>" syntax. The rest of the ruby gem world
|
17
|
+
#doesn't work on this name so I have to constantly convert to the maven name when
|
18
|
+
#necessary for certain methods
|
19
|
+
class MavenSpec < Gem::Specification
|
20
|
+
alias_method :old_full_name, :full_name
|
21
|
+
alias_method :old_full_gem_path, :full_gem_path
|
22
|
+
|
23
|
+
alias_method :old_ruby, :to_ruby
|
24
|
+
|
25
|
+
def to_ruby
|
26
|
+
old_name=self.name
|
27
|
+
self.name=Maven::Gemify2.mname(old_name)
|
28
|
+
return_results = old_ruby
|
29
|
+
self.name=old_name
|
30
|
+
return_results
|
31
|
+
end
|
32
|
+
|
33
|
+
def full_name
|
34
|
+
if @full_name.nil?
|
35
|
+
#Needs to use the maven_name here not the gemname which has the "mvn:" stuff
|
36
|
+
old_name=self.name
|
37
|
+
self.name=Maven::Gemify2.mname(old_name)
|
38
|
+
return_path = old_full_name
|
39
|
+
self.name=old_name
|
40
|
+
@full_name=return_path
|
41
|
+
end
|
42
|
+
@full_name
|
43
|
+
end
|
44
|
+
|
45
|
+
def full_gem_path
|
46
|
+
if @full_gem_path.nil?
|
47
|
+
#Needs to use the maven_name here not the gemname which has the "mvn:" stuff
|
48
|
+
old_name=self.name
|
49
|
+
self.name=Maven::Gemify2.mname(old_name)
|
50
|
+
return_path = old_full_gem_path
|
51
|
+
self.name=old_name
|
52
|
+
@full_gem_path=return_path
|
53
|
+
end
|
54
|
+
@full_gem_path
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
module Maven
|
60
|
+
|
61
|
+
class Gemify2
|
62
|
+
attr_reader :repositories
|
63
|
+
|
64
|
+
#repositories should be an array of urls
|
65
|
+
def initialize(*repositories)
|
66
|
+
maven # ensure maven initialized
|
67
|
+
@repositories = Set.new
|
68
|
+
if repositories.length > 0
|
69
|
+
@repositories.merge([repositories].flatten)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
def add_repository(repository_url)
|
75
|
+
@repositories << repository_url
|
76
|
+
end
|
77
|
+
|
78
|
+
@@verbose = false
|
79
|
+
def self.verbose?
|
80
|
+
@@verbose || $DEBUG
|
81
|
+
end
|
82
|
+
def verbose?
|
83
|
+
self.class.verbose?
|
84
|
+
end
|
85
|
+
def self.verbose=(v)
|
86
|
+
@@verbose = v
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
def self.maven_config
|
91
|
+
@maven_config ||= Gem.configuration["maven"] || {}
|
92
|
+
end
|
93
|
+
def maven_config; self.class.maven_config; end
|
94
|
+
|
95
|
+
def self.java_imports
|
96
|
+
%w(
|
97
|
+
org.codehaus.plexus.classworlds.ClassWorld
|
98
|
+
org.codehaus.plexus.DefaultContainerConfiguration
|
99
|
+
org.codehaus.plexus.DefaultPlexusContainer
|
100
|
+
org.apache.maven.Maven
|
101
|
+
org.apache.maven.repository.RepositorySystem
|
102
|
+
org.apache.maven.execution.DefaultMavenExecutionRequest
|
103
|
+
org.apache.maven.artifact.repository.MavenArtifactRepository
|
104
|
+
org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout
|
105
|
+
org.apache.maven.artifact.repository.ArtifactRepositoryPolicy
|
106
|
+
javax.xml.stream.XMLStreamWriter
|
107
|
+
javax.xml.stream.XMLOutputFactory
|
108
|
+
javax.xml.stream.XMLStreamException
|
109
|
+
).each {|i| java_import i }
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.create_maven
|
113
|
+
require 'java' # done lazily, so we're not loading it all the time
|
114
|
+
bin = nil
|
115
|
+
if ENV['M2_HOME'] # use M2_HOME if set
|
116
|
+
bin = File.join(ENV['M2_HOME'], "bin")
|
117
|
+
else
|
118
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).detect do |path|
|
119
|
+
mvn = File.join(path, "mvn")
|
120
|
+
if File.exists?(mvn)
|
121
|
+
if File.symlink?(mvn)
|
122
|
+
link = File.readlink(mvn)
|
123
|
+
if link =~ /^\// # is absolute path
|
124
|
+
bin = File.dirname(File.expand_path(link))
|
125
|
+
else # is relative path so join with dir of the maven command
|
126
|
+
bin = File.dirname(File.expand_path(File.join(File.dirname(mvn), link)))
|
127
|
+
end
|
128
|
+
else # is no link so just expand it
|
129
|
+
bin = File.expand_path(path)
|
130
|
+
end
|
131
|
+
else
|
132
|
+
nil
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
bin = "/usr/share/maven2/bin" if bin.nil? # OK let's try debian default
|
137
|
+
if File.exists?(bin)
|
138
|
+
@mvn = File.join(bin, "mvn")
|
139
|
+
if Dir.glob(File.join(bin, "..", "lib", "maven-core-3.*jar")).size == 0
|
140
|
+
begin
|
141
|
+
gem 'ruby-maven', ">=0"
|
142
|
+
bin = File.dirname(Gem.bin_path('ruby-maven', "rmvn"))
|
143
|
+
@mvn = File.join(bin, "rmvn")
|
144
|
+
rescue LoadError
|
145
|
+
bin = nil
|
146
|
+
end
|
147
|
+
end
|
148
|
+
else
|
149
|
+
bin = nil
|
150
|
+
end
|
151
|
+
raise Gem::Maven3NotFound.new("can not find maven3 installation. install ruby-maven with\n\n\tjruby -S gem install ruby-maven\n\n") if bin.nil?
|
152
|
+
|
153
|
+
warn "Using Maven install at #{bin}" if verbose?
|
154
|
+
|
155
|
+
boot = File.join(bin, "..", "boot")
|
156
|
+
lib = File.join(bin, "..", "lib")
|
157
|
+
ext = File.join(bin, "..", "ext")
|
158
|
+
(Dir.glob(lib + "/*jar") + Dir.glob(boot + "/*jar")).each {|path| require path }
|
159
|
+
|
160
|
+
java.lang.System.setProperty("classworlds.conf", File.join(bin, "m2.conf"))
|
161
|
+
java.lang.System.setProperty("maven.home", File.join(bin, ".."))
|
162
|
+
java_imports
|
163
|
+
|
164
|
+
class_world = ClassWorld.new("plexus.core", java.lang.Thread.currentThread().getContextClassLoader());
|
165
|
+
config = DefaultContainerConfiguration.new
|
166
|
+
config.set_class_world class_world
|
167
|
+
config.set_name "ruby-tools"
|
168
|
+
container = DefaultPlexusContainer.new(config);
|
169
|
+
@@execution_request_populator = container.lookup(org.apache.maven.execution.MavenExecutionRequestPopulator.java_class)
|
170
|
+
|
171
|
+
@@settings_builder = container.lookup(org.apache.maven.settings.building.SettingsBuilder.java_class )
|
172
|
+
container.lookup(Maven.java_class)
|
173
|
+
end
|
174
|
+
|
175
|
+
def self.maven
|
176
|
+
@maven ||= create_maven
|
177
|
+
end
|
178
|
+
def maven; self.class.maven; end
|
179
|
+
|
180
|
+
def self.temp_dir
|
181
|
+
@temp_dir ||=
|
182
|
+
begin
|
183
|
+
d=Dir.mktmpdir
|
184
|
+
at_exit {FileUtils.rm_rf(d.dup)}
|
185
|
+
d
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def temp_dir
|
190
|
+
self.class.temp_dir
|
191
|
+
end
|
192
|
+
|
193
|
+
def execute(goals, pomFile,props = {})
|
194
|
+
request = DefaultMavenExecutionRequest.new
|
195
|
+
request.set_show_errors(true)
|
196
|
+
|
197
|
+
props.each do |k,v|
|
198
|
+
request.user_properties.put(k.to_s, v.to_s)
|
199
|
+
end
|
200
|
+
request.set_goals(goals)
|
201
|
+
request.set_logging_level 0
|
202
|
+
request.setPom(java.io.File.new(pomFile))
|
203
|
+
if verbose?
|
204
|
+
active_profiles = request.getActiveProfiles.collect{ |p| p.to_s }
|
205
|
+
puts "active profiles:\n\t[#{active_profiles.join(', ')}]"
|
206
|
+
puts "maven goals:"
|
207
|
+
request.goals.each { |g| puts "\t#{g}" }
|
208
|
+
puts "system properties:"
|
209
|
+
request.getUserProperties.map.each { |k,v| puts "\t#{k} => #{v}" }
|
210
|
+
puts
|
211
|
+
end
|
212
|
+
out = java.lang.System.out
|
213
|
+
string_io = java.io.ByteArrayOutputStream.new
|
214
|
+
java.lang.System.setOut(java.io.PrintStream.new(string_io))
|
215
|
+
result = maven.execute request
|
216
|
+
java.lang.System.out = out
|
217
|
+
has_exceptions = false
|
218
|
+
result.exceptions.each do |e|
|
219
|
+
has_exceptions = true
|
220
|
+
e.print_stack_trace
|
221
|
+
string_io.write(e.get_message.to_java_string.get_bytes)
|
222
|
+
end
|
223
|
+
raise string_io.to_s if has_exceptions
|
224
|
+
string_io.to_s
|
225
|
+
end
|
226
|
+
|
227
|
+
def writeElement(xmlWriter,element_name, text)
|
228
|
+
xmlWriter.writeStartElement(element_name.to_java)
|
229
|
+
xmlWriter.writeCharacters(text.to_java)
|
230
|
+
xmlWriter.writeEndElement
|
231
|
+
end
|
232
|
+
|
233
|
+
public
|
234
|
+
def maven_name(gemname)
|
235
|
+
self.class.mname(gemname)
|
236
|
+
end
|
237
|
+
#gemname==mvn:group_id:artifact_id
|
238
|
+
def self.mname(gemname)
|
239
|
+
gemname.gsub("mvn:","").gsub("-","_").gsub(".","_").gsub(":","_")
|
240
|
+
end
|
241
|
+
|
242
|
+
def get_versions(gemname)
|
243
|
+
[]
|
244
|
+
end
|
245
|
+
|
246
|
+
def generate_spec(gemname, version)
|
247
|
+
mname = maven_name(gemname)
|
248
|
+
MavenSpec.new do |s|
|
249
|
+
s.name = gemname
|
250
|
+
s.date = '2010-04-28'
|
251
|
+
s.summary = "Hola!"
|
252
|
+
s.description = "A simple hello world gem"
|
253
|
+
s.authors = ["Nick Quaranto"]
|
254
|
+
s.email = 'nick@quaran.to'
|
255
|
+
s.homepage = 'http://rubygems.org/gems/hola'
|
256
|
+
s.version = version
|
257
|
+
s.files = "lib/#{mname}.rb"
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def generate_gem(gemname, version)
|
262
|
+
mname = maven_name(gemname)
|
263
|
+
spec_file=generate_spec(gemname,version)
|
264
|
+
# spec_file.name=mname #So that the gem's name is correct
|
265
|
+
gemname=gemname.gsub("mvn:","")
|
266
|
+
maven_parts = gemname.split(":")
|
267
|
+
group_id = maven_parts[0]
|
268
|
+
artifact_id = maven_parts[1]
|
269
|
+
|
270
|
+
FileUtils.mkdir_p(File.join(temp_dir,"lib"))
|
271
|
+
#Generate a dummy POM file that we'll use to run maven against
|
272
|
+
#to resolve deps and generate a classpath
|
273
|
+
pomfile=File.join(temp_dir,"pom.xml")
|
274
|
+
puts "pomfile=#{pomfile}"
|
275
|
+
out = java.io.BufferedOutputStream.new(java.io.FileOutputStream.new(pomfile.to_java))
|
276
|
+
outputFactory = XMLOutputFactory.newFactory()
|
277
|
+
xmlStreamWriter = outputFactory.createXMLStreamWriter(out)
|
278
|
+
xmlStreamWriter.writeStartDocument
|
279
|
+
xmlStreamWriter.writeStartElement("project".to_java)
|
280
|
+
|
281
|
+
writeElement(xmlStreamWriter,"groupId","org.hokiesuns.mavengemify")
|
282
|
+
writeElement(xmlStreamWriter,"artifactId","mavengemify")
|
283
|
+
writeElement(xmlStreamWriter,"modelVersion","4.0.0")
|
284
|
+
writeElement(xmlStreamWriter,"version","1.0-SNAPSHOT")
|
285
|
+
|
286
|
+
#Repositories
|
287
|
+
if @repositories.length > 0
|
288
|
+
xmlStreamWriter.writeStartElement("repositories".to_java)
|
289
|
+
@repositories.each_with_index {|repo,i|
|
290
|
+
xmlStreamWriter.writeStartElement("repository".to_java)
|
291
|
+
writeElement(xmlStreamWriter,"id","repository_#{i}")
|
292
|
+
writeElement(xmlStreamWriter,"url",repo)
|
293
|
+
xmlStreamWriter.writeEndElement #repository
|
294
|
+
}
|
295
|
+
xmlStreamWriter.writeEndElement #repositories
|
296
|
+
end
|
297
|
+
xmlStreamWriter.writeStartElement("dependencies".to_java)
|
298
|
+
|
299
|
+
xmlStreamWriter.writeStartElement("dependency".to_java)
|
300
|
+
writeElement(xmlStreamWriter,"groupId",group_id)
|
301
|
+
writeElement(xmlStreamWriter,"artifactId",artifact_id)
|
302
|
+
writeElement(xmlStreamWriter,"version",version.to_s)
|
303
|
+
|
304
|
+
xmlStreamWriter.writeEndElement #dependency
|
305
|
+
|
306
|
+
xmlStreamWriter.writeEndElement #dependencies
|
307
|
+
|
308
|
+
xmlStreamWriter.writeEndElement #project
|
309
|
+
|
310
|
+
xmlStreamWriter.writeEndDocument
|
311
|
+
xmlStreamWriter.close
|
312
|
+
out.close
|
313
|
+
|
314
|
+
execute(["dependency:resolve","dependency:build-classpath"],pomfile,{"mdep.outputFile" => "cp.txt","mdep.fileSeparator"=>"/"})
|
315
|
+
|
316
|
+
ruby_file = File.new(File.join(temp_dir,"lib/#{mname}.rb"),"w")
|
317
|
+
cp_file = File.new(File.join(temp_dir,"cp.txt"),"r")
|
318
|
+
cp_line = cp_file.gets
|
319
|
+
cp_file.close
|
320
|
+
cp_entries = cp_line.split(";")
|
321
|
+
cp_entries.each{ |entry|
|
322
|
+
ruby_file.puts "require \"#{entry}\""
|
323
|
+
}
|
324
|
+
ruby_file.close
|
325
|
+
old_pwd = Dir.pwd
|
326
|
+
Dir.chdir(temp_dir)
|
327
|
+
gembuilder = Gem::Builder.new(spec_file)
|
328
|
+
gemfile=gembuilder.build
|
329
|
+
|
330
|
+
geminstaller = Gem::Installer.new(gemfile)
|
331
|
+
geminstaller.install
|
332
|
+
Dir.chdir(old_pwd)
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
@@ -1,18 +1,8 @@
|
|
1
1
|
module JBundler
|
2
|
-
module
|
2
|
+
module MavenVersion
|
3
3
|
|
4
|
-
def to_coordinate(line)
|
5
|
-
if line =~ /^\s*(jar|pom)\s/
|
6
|
-
|
7
|
-
group_id, artifact_id, version, second_version = line.sub(/\s*[a-z]+\s+/, '').sub(/#.*/,'').gsub(/\s+/,'').gsub(/['"],/, ':').gsub(/['"]/, '').split(/:/)
|
8
|
-
mversion = second_version ? to_version(version, second_version) : to_version(version)
|
9
|
-
extension = line.strip.sub(/\s+.*/, '')
|
10
|
-
"#{group_id}:#{artifact_id}:#{extension}:#{mversion}"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
4
|
def to_version(*args)
|
15
|
-
if args.size == 0
|
5
|
+
if args.size == 0
|
16
6
|
"[0,)"
|
17
7
|
else
|
18
8
|
low, high = convert(args[0])
|
@@ -40,14 +30,14 @@ module JBundler
|
|
40
30
|
[(nil || low), "#{val}]"]
|
41
31
|
# treat '!' the same way as '>' since maven can not describe such range
|
42
32
|
elsif arg =~ /[!>]/
|
43
|
-
val = arg.sub(
|
33
|
+
val = arg.sub(/>\s*/, '')
|
44
34
|
["(#{val}", (nil || high)]
|
45
35
|
elsif arg =~ /</
|
46
36
|
val = arg.sub(/<\s*/, '')
|
47
37
|
[(nil || low), "#{val})"]
|
48
38
|
elsif arg =~ /\=/
|
49
39
|
val = arg.sub(/=\s*/, '')
|
50
|
-
[
|
40
|
+
[val, val]
|
51
41
|
else
|
52
42
|
[arg, arg]
|
53
43
|
end
|
data/lib/jbundler/pom.rb
CHANGED
@@ -0,0 +1,251 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'set'
|
5
|
+
require 'jbundler/gemfile_lock'
|
6
|
+
require 'java'
|
7
|
+
|
8
|
+
# A modified maven_gemify2 taken from https://github.com/ANithian/bundler/blob/a29d4550dfb2f24372bf6e60f00e633ff92d5d64/lib/bundler/maven_gemify2.rb
|
9
|
+
module JBundler
|
10
|
+
|
11
|
+
class Maven3NotFound < StandardError; end
|
12
|
+
|
13
|
+
class Maven
|
14
|
+
attr_reader :repositories
|
15
|
+
|
16
|
+
#repositories should be an array of urls
|
17
|
+
def initialize(*repositories)
|
18
|
+
maven # ensure maven initialized
|
19
|
+
@repositories = Set.new
|
20
|
+
if repositories.length > 0
|
21
|
+
@repositories.merge([repositories].flatten)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_repository(repository_url)
|
27
|
+
@repositories << repository_url
|
28
|
+
end
|
29
|
+
|
30
|
+
@@verbose = false
|
31
|
+
def self.verbose?
|
32
|
+
@@verbose || $DEBUG
|
33
|
+
end
|
34
|
+
def verbose?
|
35
|
+
self.class.verbose?
|
36
|
+
end
|
37
|
+
def self.verbose=(v)
|
38
|
+
@@verbose = v
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def self.maven_config
|
43
|
+
@maven_config ||= Gem.configuration["maven"] || {}
|
44
|
+
end
|
45
|
+
def maven_config; self.class.maven_config; end
|
46
|
+
|
47
|
+
def self.java_imports
|
48
|
+
%w(
|
49
|
+
org.codehaus.plexus.classworlds.ClassWorld
|
50
|
+
org.codehaus.plexus.DefaultContainerConfiguration
|
51
|
+
org.codehaus.plexus.DefaultPlexusContainer
|
52
|
+
org.apache.maven.Maven
|
53
|
+
org.apache.maven.repository.RepositorySystem
|
54
|
+
org.apache.maven.execution.DefaultMavenExecutionRequest
|
55
|
+
org.apache.maven.artifact.repository.MavenArtifactRepository
|
56
|
+
org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout
|
57
|
+
org.apache.maven.artifact.repository.ArtifactRepositoryPolicy
|
58
|
+
javax.xml.stream.XMLStreamWriter
|
59
|
+
javax.xml.stream.XMLOutputFactory
|
60
|
+
javax.xml.stream.XMLStreamException
|
61
|
+
).each {|i| java_import i }
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.create_maven
|
65
|
+
require 'java' # done lazily, so we're not loading it all the time
|
66
|
+
bin = nil
|
67
|
+
if ENV['M2_HOME'] # use M2_HOME if set
|
68
|
+
bin = File.join(ENV['M2_HOME'], "bin")
|
69
|
+
else
|
70
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).detect do |path|
|
71
|
+
mvn = File.join(path, "mvn")
|
72
|
+
if File.exists?(mvn)
|
73
|
+
if File.symlink?(mvn)
|
74
|
+
link = File.readlink(mvn)
|
75
|
+
if link =~ /^\// # is absolute path
|
76
|
+
bin = File.dirname(File.expand_path(link))
|
77
|
+
else # is relative path so join with dir of the maven command
|
78
|
+
bin = File.dirname(File.expand_path(File.join(File.dirname(mvn), link)))
|
79
|
+
end
|
80
|
+
else # is no link so just expand it
|
81
|
+
bin = File.expand_path(path)
|
82
|
+
end
|
83
|
+
else
|
84
|
+
nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
bin = "/usr/share/maven2/bin" if bin.nil? # OK let's try debian default
|
89
|
+
if File.exists?(bin)
|
90
|
+
@mvn = File.join(bin, "mvn")
|
91
|
+
if Dir.glob(File.join(bin, "..", "lib", "maven-core-3.*jar")).size == 0
|
92
|
+
begin
|
93
|
+
gem 'ruby-maven', ">=0"
|
94
|
+
bin = File.dirname(Gem.bin_path('ruby-maven', "rmvn"))
|
95
|
+
@mvn = File.join(bin, "rmvn")
|
96
|
+
rescue LoadError
|
97
|
+
bin = nil
|
98
|
+
end
|
99
|
+
end
|
100
|
+
else
|
101
|
+
bin = nil
|
102
|
+
end
|
103
|
+
raise Maven3NotFound.new("can not find maven3 installation. install ruby-maven with\n\n\tjruby -S gem install ruby-maven\n\n") if bin.nil?
|
104
|
+
|
105
|
+
warn "Using Maven install at #{bin}" if verbose?
|
106
|
+
|
107
|
+
boot = File.join(bin, "..", "boot")
|
108
|
+
lib = File.join(bin, "..", "lib")
|
109
|
+
ext = File.join(bin, "..", "ext")
|
110
|
+
(Dir.glob(lib + "/*jar") + Dir.glob(boot + "/*jar")).each {|path| require path }
|
111
|
+
|
112
|
+
java.lang.System.setProperty("classworlds.conf", File.join(bin, "m2.conf"))
|
113
|
+
java.lang.System.setProperty("maven.home", File.join(bin, ".."))
|
114
|
+
java_imports
|
115
|
+
|
116
|
+
class_world = ClassWorld.new("plexus.core", java.lang.Thread.currentThread().getContextClassLoader());
|
117
|
+
config = DefaultContainerConfiguration.new
|
118
|
+
config.set_class_world class_world
|
119
|
+
config.set_name "ruby-tools"
|
120
|
+
container = DefaultPlexusContainer.new(config);
|
121
|
+
@@execution_request_populator = container.lookup(org.apache.maven.execution.MavenExecutionRequestPopulator.java_class)
|
122
|
+
|
123
|
+
@@settings_builder = container.lookup(org.apache.maven.settings.building.SettingsBuilder.java_class )
|
124
|
+
container.lookup(Maven.java_class)
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.maven
|
128
|
+
@maven ||= create_maven
|
129
|
+
end
|
130
|
+
def maven; self.class.maven; end
|
131
|
+
|
132
|
+
def self.temp_dir
|
133
|
+
@temp_dir ||=
|
134
|
+
begin
|
135
|
+
d=Dir.mktmpdir
|
136
|
+
at_exit {FileUtils.rm_rf(d.dup)}
|
137
|
+
d
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def temp_dir
|
142
|
+
self.class.temp_dir
|
143
|
+
end
|
144
|
+
|
145
|
+
def execute(goals, pomFile, props = {})
|
146
|
+
request = DefaultMavenExecutionRequest.new
|
147
|
+
request.set_show_errors(true)
|
148
|
+
|
149
|
+
props.each do |k,v|
|
150
|
+
request.user_properties.put(k.to_s, v.to_s)
|
151
|
+
end
|
152
|
+
request.set_goals(goals)
|
153
|
+
request.set_logging_level 0
|
154
|
+
request.setPom(java.io.File.new(pomFile))
|
155
|
+
if verbose?
|
156
|
+
active_profiles = request.getActiveProfiles.collect{ |p| p.to_s }
|
157
|
+
puts "active profiles:\n\t[#{active_profiles.join(', ')}]"
|
158
|
+
puts "maven goals:"
|
159
|
+
request.goals.each { |g| puts "\t#{g}" }
|
160
|
+
puts "system properties:"
|
161
|
+
request.getUserProperties.map.each { |k,v| puts "\t#{k} => #{v}" }
|
162
|
+
puts
|
163
|
+
end
|
164
|
+
out = java.lang.System.out
|
165
|
+
string_io = java.io.ByteArrayOutputStream.new
|
166
|
+
java.lang.System.setOut(java.io.PrintStream.new(string_io))
|
167
|
+
result = maven.execute request
|
168
|
+
java.lang.System.out = out
|
169
|
+
has_exceptions = false
|
170
|
+
result.exceptions.each do |e|
|
171
|
+
has_exceptions = true
|
172
|
+
e.print_stack_trace
|
173
|
+
string_io.write(e.get_message.to_java_string.get_bytes)
|
174
|
+
end
|
175
|
+
raise string_io.to_s if has_exceptions
|
176
|
+
string_io.to_s
|
177
|
+
end
|
178
|
+
|
179
|
+
def writeElement(xmlWriter,element_name, text)
|
180
|
+
xmlWriter.writeStartElement(element_name.to_java)
|
181
|
+
xmlWriter.writeCharacters(text.to_java)
|
182
|
+
xmlWriter.writeEndElement
|
183
|
+
end
|
184
|
+
|
185
|
+
public
|
186
|
+
|
187
|
+
def generate_classpath(mavenfile = 'Mvnfile', classpathfile = '.jbundler/classpath.rb')
|
188
|
+
|
189
|
+
#to resolve deps and generate a classpath
|
190
|
+
pomfile=File.join(temp_dir,"pom.xml")
|
191
|
+
puts "pomfile=#{pomfile}"
|
192
|
+
out = java.io.BufferedOutputStream.new(java.io.FileOutputStream.new(pomfile.to_java))
|
193
|
+
outputFactory = XMLOutputFactory.newFactory()
|
194
|
+
xmlStreamWriter = outputFactory.createXMLStreamWriter(out)
|
195
|
+
xmlStreamWriter.writeStartDocument
|
196
|
+
xmlStreamWriter.writeStartElement("project".to_java)
|
197
|
+
|
198
|
+
writeElement(xmlStreamWriter,"groupId","org.hokiesuns.mavengemify")
|
199
|
+
writeElement(xmlStreamWriter,"artifactId","mavengemify")
|
200
|
+
writeElement(xmlStreamWriter,"modelVersion","4.0.0")
|
201
|
+
writeElement(xmlStreamWriter,"version","1.0-SNAPSHOT")
|
202
|
+
|
203
|
+
#Repositories
|
204
|
+
if @repositories.length > 0
|
205
|
+
xmlStreamWriter.writeStartElement("repositories".to_java)
|
206
|
+
@repositories.each_with_index {|repo,i|
|
207
|
+
xmlStreamWriter.writeStartElement("repository".to_java)
|
208
|
+
writeElement(xmlStreamWriter,"id","repository_#{i}")
|
209
|
+
writeElement(xmlStreamWriter,"url",repo)
|
210
|
+
xmlStreamWriter.writeEndElement #repository
|
211
|
+
}
|
212
|
+
xmlStreamWriter.writeEndElement #repositories
|
213
|
+
end
|
214
|
+
xmlStreamWriter.writeStartElement("dependencies".to_java)
|
215
|
+
|
216
|
+
(File.read(mavenfile).split(/\n/) + GemfileLock.new.jar_deps).each do |line|
|
217
|
+
if line =~ /^\s*jar\s+/
|
218
|
+
group_id, artifact_id, version = line.sub(/\s*jar\s+/, '').sub(/#.*/,'').gsub(/[',]/,'').split(/[\s:]+/)
|
219
|
+
|
220
|
+
xmlStreamWriter.writeStartElement("dependency".to_java)
|
221
|
+
writeElement(xmlStreamWriter,"groupId",group_id)
|
222
|
+
writeElement(xmlStreamWriter,"artifactId",artifact_id)
|
223
|
+
# default to complete version range
|
224
|
+
writeElement(xmlStreamWriter,"version", (version || "[0,)").to_s)
|
225
|
+
xmlStreamWriter.writeEndElement #dependency
|
226
|
+
end
|
227
|
+
end
|
228
|
+
xmlStreamWriter.writeEndElement #dependencies
|
229
|
+
|
230
|
+
xmlStreamWriter.writeEndElement #project
|
231
|
+
|
232
|
+
xmlStreamWriter.writeEndDocument
|
233
|
+
xmlStreamWriter.close
|
234
|
+
out.close
|
235
|
+
|
236
|
+
execute(["dependency:resolve","dependency:build-classpath"],pomfile,{"mdep.outputFile" => "cp.txt","mdep.fileSeparator"=>"/"})
|
237
|
+
|
238
|
+
FileUtils.mkdir_p(File.dirname(classpathfile))
|
239
|
+
File.open(classpathfile, 'w') do |f|
|
240
|
+
path_separator = java.lang.System.getProperty("path.separator").to_s
|
241
|
+
File.read(File.join(temp_dir,"cp.txt")).each do |line|
|
242
|
+
line.split(/#{path_separator}/).each do |path|
|
243
|
+
f.puts "require '#{path}'"
|
244
|
+
end
|
245
|
+
end
|
246
|
+
f.close
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
end
|