jbundler 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,8 +7,6 @@ module JBundler
7
7
  end
8
8
 
9
9
  def require_classpath
10
- p File.exists? @classpathfile
11
- p @classpathfile
12
10
  load File.expand_path @classpathfile
13
11
  end
14
12
 
@@ -20,8 +18,8 @@ p @classpathfile
20
18
  File.exists?(@classpathfile)
21
19
  end
22
20
 
23
- def needs_update?(mavenfile, gemfile_lock)
24
- !mavenfile.exists? || !exists? || !mavenfile.exists_lock? || (mavenfile.mtime > mtime) || (mavenfile.mtime_lock > mtime) || (gemfile_lock.mtime > mtime)
21
+ def needs_update?(jarfile, gemfile_lock)
22
+ !exists? || !jarfile.exists_lock? || (jarfile.exists? && (jarfile.mtime > mtime)) || (jarfile.exists_lock? && (jarfile.mtime_lock > mtime)) || (gemfile_lock.mtime > mtime)
25
23
  end
26
24
 
27
25
  def generate(classpath)
@@ -0,0 +1,207 @@
1
+ require 'uri'
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+ require 'set'
5
+ require 'jbundler/gemfile_lock'
6
+ require 'jbundler/pom'
7
+ require 'java'
8
+
9
+ # A modified maven_gemify2 taken from https://github.com/ANithian/bundler/blob/a29d4550dfb2f24372bf6e60f00e633ff92d5d64/lib/bundler/maven_gemify2.rb
10
+ module JBundler
11
+
12
+ class Maven3NotFound < StandardError; end
13
+
14
+ class Maven
15
+ attr_reader :repositories
16
+
17
+ #repositories should be an array of urls
18
+ def initialize(*repositories)
19
+ maven # ensure maven initialized
20
+ @repositories = Set.new
21
+ if repositories.length > 0
22
+ @repositories.merge([repositories].flatten)
23
+ end
24
+
25
+ end
26
+
27
+ def add_repository(repository_url)
28
+ @repositories << repository_url
29
+ end
30
+
31
+ @@verbose = false
32
+ def self.verbose?
33
+ @@verbose || $DEBUG
34
+ end
35
+ def verbose?
36
+ self.class.verbose?
37
+ end
38
+ def self.verbose=(v)
39
+ @@verbose = v
40
+ end
41
+
42
+ private
43
+ def self.maven_config
44
+ @maven_config ||= Gem.configuration["maven"] || {}
45
+ end
46
+ def maven_config; self.class.maven_config; end
47
+
48
+ def self.java_imports
49
+ %w(
50
+ org.codehaus.plexus.classworlds.ClassWorld
51
+ org.codehaus.plexus.DefaultContainerConfiguration
52
+ org.codehaus.plexus.DefaultPlexusContainer
53
+ org.apache.maven.Maven
54
+ org.apache.maven.repository.RepositorySystem
55
+ org.apache.maven.execution.DefaultMavenExecutionRequest
56
+ org.apache.maven.artifact.repository.MavenArtifactRepository
57
+ org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout
58
+ org.apache.maven.artifact.repository.ArtifactRepositoryPolicy
59
+ javax.xml.stream.XMLStreamWriter
60
+ javax.xml.stream.XMLOutputFactory
61
+ javax.xml.stream.XMLStreamException
62
+ ).each {|i| java_import i }
63
+ end
64
+
65
+ def self.create_maven
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
+ public
180
+
181
+ def generate_classpath(mavenfile = 'Mvnfile', classpathfile = '.jbundler/classpath.rb')
182
+
183
+ #to resolve deps and generate a classpath
184
+ pomfile=File.join(temp_dir,
185
+ "pom.xml")
186
+ Pom.new(pomfile, "mavengemify", "1.0-SNAPSHOT",
187
+ File.read(mavenfile).split(/\n/) + GemfileLock.new.jar_deps)
188
+
189
+ execute(["dependency:resolve","dependency:build-classpath"],pomfile,{"mdep.outputFile" => "cp.txt","mdep.fileSeparator"=>"/"})
190
+
191
+ FileUtils.mkdir_p(File.dirname(classpathfile))
192
+ File.open(classpathfile, 'w') do |f|
193
+ f.puts "JBUNDLER_CLASSPATH = []"
194
+ path_separator = java.lang.System.getProperty("path.separator").to_s
195
+ File.read(File.join(temp_dir,"cp.txt")).each do |line|
196
+ line.split(/#{path_separator}/).each do |path|
197
+ f.puts "JBUNDLER_CLASSPATH << '#{path}'" unless path =~ /pom$/
198
+ end
199
+ end
200
+ f.puts "JBUNDLER_CLASSPATH.freeze"
201
+ f.puts "JBUNDLER_CLASSPATH.each { |c| require c }"
202
+ f.close
203
+ end
204
+ end
205
+
206
+ end
207
+ end
@@ -5,8 +5,8 @@ module JBundler
5
5
 
6
6
  class GemfileLock
7
7
 
8
- def initialize(mavenfile, lockfile = 'Gemfile.lock')
9
- @mavenfile = mavenfile
8
+ def initialize(jarfile, lockfile = 'Gemfile.lock')
9
+ @jarfile = jarfile
10
10
  @lockfile = lockfile if File.exists?(lockfile)
11
11
  end
12
12
 
@@ -14,7 +14,7 @@ module JBundler
14
14
  File.mtime(@lockfile) if @lockfile
15
15
  end
16
16
 
17
- def populate_depedencies(aether)
17
+ def populate_dependencies(aether)
18
18
  if @lockfile
19
19
  # assuming we run in Bundler context here
20
20
  # at we have a Gemfile.lock :)
@@ -28,7 +28,7 @@ module JBundler
28
28
  unless jars.empty?
29
29
  pom = Pom.new(spec.name, spec.version, jars, "pom")
30
30
  aether.install(pom.coordinate, pom.file)
31
- unless @mavenfile.locked?(pom.coordinate)
31
+ unless @jarfile.locked?(pom.coordinate)
32
32
  aether.add_artifact(pom.coordinate)
33
33
  end
34
34
  end
@@ -0,0 +1,17 @@
1
+ module JBundler
2
+
3
+ class GemfileLock
4
+
5
+ def initialize
6
+ is_path = false
7
+ File.read('Gemfile.lock').each do |line|
8
+ if line =~ /^PATH/
9
+ is_path = true
10
+ elsif line =~ /^[A-Z]+/
11
+ is_path = false
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,252 @@
1
+ require 'uri'
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+ require 'set'
5
+ require 'java'
6
+
7
+ # A modified maven_gemify
8
+ module JBundler
9
+
10
+ class Maven3NotFound < StandardError; end
11
+
12
+ class Maven
13
+ attr_reader :repositories
14
+
15
+ #repositories should be an array of urls
16
+ def initialize(*repositories)
17
+ maven # ensure maven initialized
18
+ @repositories = Set.new
19
+ if repositories.length > 0
20
+ @repositories.merge([repositories].flatten)
21
+ end
22
+
23
+ end
24
+
25
+ def add_repository(repository_url)
26
+ @repositories << repository_url
27
+ end
28
+
29
+ @@verbose = false
30
+ def self.verbose?
31
+ @@verbose || $DEBUG
32
+ end
33
+ def verbose?
34
+ self.class.verbose?
35
+ end
36
+ def self.verbose=(v)
37
+ @@verbose = v
38
+ end
39
+
40
+ private
41
+ def self.maven_config
42
+ @maven_config ||= Gem.configuration["maven"] || {}
43
+ end
44
+ def maven_config; self.class.maven_config; end
45
+
46
+ def self.java_imports
47
+ %w(
48
+ org.codehaus.plexus.classworlds.ClassWorld
49
+ org.codehaus.plexus.DefaultContainerConfiguration
50
+ org.codehaus.plexus.DefaultPlexusContainer
51
+ org.apache.maven.Maven
52
+ org.apache.maven.repository.RepositorySystem
53
+ org.apache.maven.execution.DefaultMavenExecutionRequest
54
+ org.apache.maven.artifact.repository.MavenArtifactRepository
55
+ org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout
56
+ org.apache.maven.artifact.repository.ArtifactRepositoryPolicy
57
+ javax.xml.stream.XMLStreamWriter
58
+ javax.xml.stream.XMLOutputFactory
59
+ javax.xml.stream.XMLStreamException
60
+ ).each {|i| java_import i }
61
+ end
62
+
63
+ def self.create_maven
64
+ require 'java' # done lazily, so we're not loading it all the time
65
+ bin = nil
66
+ if ENV['M2_HOME'] # use M2_HOME if set
67
+ bin = File.join(ENV['M2_HOME'], "bin")
68
+ else
69
+ ENV['PATH'].split(File::PATH_SEPARATOR).detect do |path|
70
+ mvn = File.join(path, "mvn")
71
+ if File.exists?(mvn)
72
+ if File.symlink?(mvn)
73
+ link = File.readlink(mvn)
74
+ if link =~ /^\// # is absolute path
75
+ bin = File.dirname(File.expand_path(link))
76
+ else # is relative path so join with dir of the maven command
77
+ bin = File.dirname(File.expand_path(File.join(File.dirname(mvn), link)))
78
+ end
79
+ else # is no link so just expand it
80
+ bin = File.expand_path(path)
81
+ end
82
+ else
83
+ nil
84
+ end
85
+ end
86
+ end
87
+ bin = "/usr/share/maven2/bin" if bin.nil? # OK let's try debian default
88
+ if File.exists?(bin)
89
+ @mvn = File.join(bin, "mvn")
90
+ if Dir.glob(File.join(bin, "..", "lib", "maven-core-3.*jar")).size == 0
91
+ begin
92
+ gem 'ruby-maven', ">=0"
93
+ bin = File.dirname(Gem.bin_path('ruby-maven', "rmvn"))
94
+ @mvn = File.join(bin, "rmvn")
95
+ rescue LoadError
96
+ bin = nil
97
+ end
98
+ end
99
+ else
100
+ bin = nil
101
+ end
102
+ 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?
103
+
104
+ warn "Using Maven install at #{bin}" if verbose?
105
+
106
+ boot = File.join(bin, "..", "boot")
107
+ lib = File.join(bin, "..", "lib")
108
+ ext = File.join(bin, "..", "ext")
109
+ (Dir.glob(lib + "/*jar") + Dir.glob(boot + "/*jar")).each {|path| require path }
110
+
111
+ java.lang.System.setProperty("classworlds.conf", File.join(bin, "m2.conf"))
112
+ java.lang.System.setProperty("maven.home", File.join(bin, ".."))
113
+ java_imports
114
+
115
+ class_world = ClassWorld.new("plexus.core", java.lang.Thread.currentThread().getContextClassLoader());
116
+ config = DefaultContainerConfiguration.new
117
+ config.set_class_world class_world
118
+ config.set_name "ruby-tools"
119
+ container = DefaultPlexusContainer.new(config);
120
+ @@execution_request_populator = container.lookup(org.apache.maven.execution.MavenExecutionRequestPopulator.java_class)
121
+
122
+ @@settings_builder = container.lookup(org.apache.maven.settings.building.SettingsBuilder.java_class )
123
+ container.lookup(Maven.java_class)
124
+ end
125
+
126
+ def self.maven
127
+ @maven ||= create_maven
128
+ end
129
+ def maven; self.class.maven; end
130
+
131
+ def self.temp_dir
132
+ @temp_dir ||=
133
+ begin
134
+ d=Dir.mktmpdir
135
+ at_exit {FileUtils.rm_rf(d.dup)}
136
+ d
137
+ end
138
+ end
139
+
140
+ def temp_dir
141
+ self.class.temp_dir
142
+ end
143
+
144
+ def execute(goals, pomFile, props = {})
145
+ request = DefaultMavenExecutionRequest.new
146
+ request.set_show_errors(true)
147
+
148
+ props.each do |k,v|
149
+ request.user_properties.put(k.to_s, v.to_s)
150
+ end
151
+ request.set_goals(goals)
152
+ request.set_logging_level 0
153
+ request.setPom(java.io.File.new(pomFile))
154
+ if verbose?
155
+ active_profiles = request.getActiveProfiles.collect{ |p| p.to_s }
156
+ puts "active profiles:\n\t[#{active_profiles.join(', ')}]"
157
+ puts "maven goals:"
158
+ request.goals.each { |g| puts "\t#{g}" }
159
+ puts "system properties:"
160
+ request.getUserProperties.map.each { |k,v| puts "\t#{k} => #{v}" }
161
+ puts
162
+ end
163
+ out = java.lang.System.out
164
+ string_io = java.io.ByteArrayOutputStream.new
165
+ java.lang.System.setOut(java.io.PrintStream.new(string_io))
166
+ result = maven.execute request
167
+ java.lang.System.out = out
168
+ has_exceptions = false
169
+ result.exceptions.each do |e|
170
+ has_exceptions = true
171
+ e.print_stack_trace
172
+ string_io.write(e.get_message.to_java_string.get_bytes)
173
+ end
174
+ raise string_io.to_s if has_exceptions
175
+ string_io.to_s
176
+ end
177
+
178
+ def writeElement(xmlWriter,element_name, text)
179
+ xmlWriter.writeStartElement(element_name.to_java)
180
+ xmlWriter.writeCharacters(text.to_java)
181
+ xmlWriter.writeEndElement
182
+ end
183
+
184
+ public
185
+
186
+ def generate_classpath(mavenfile = 'Mvnfile', classpathfile = '.jbundler/classpath.rb')
187
+
188
+ #to resolve deps and generate a classpath
189
+ pomfile=File.join(temp_dir,"pom.xml")
190
+ puts "pomfile=#{pomfile}"
191
+ out = java.io.BufferedOutputStream.new(java.io.FileOutputStream.new(pomfile.to_java))
192
+ outputFactory = XMLOutputFactory.newFactory()
193
+ xmlStreamWriter = outputFactory.createXMLStreamWriter(out)
194
+ xmlStreamWriter.writeStartDocument
195
+ xmlStreamWriter.writeStartElement("project".to_java)
196
+
197
+ writeElement(xmlStreamWriter,"groupId","org.hokiesuns.mavengemify")
198
+ writeElement(xmlStreamWriter,"artifactId","mavengemify")
199
+ writeElement(xmlStreamWriter,"modelVersion","4.0.0")
200
+ writeElement(xmlStreamWriter,"version","1.0-SNAPSHOT")
201
+
202
+ #Repositories
203
+ if @repositories.length > 0
204
+ xmlStreamWriter.writeStartElement("repositories".to_java)
205
+ @repositories.each_with_index {|repo,i|
206
+ xmlStreamWriter.writeStartElement("repository".to_java)
207
+ writeElement(xmlStreamWriter,"id","repository_#{i}")
208
+ writeElement(xmlStreamWriter,"url",repo)
209
+ xmlStreamWriter.writeEndElement #repository
210
+ }
211
+ xmlStreamWriter.writeEndElement #repositories
212
+ end
213
+ xmlStreamWriter.writeStartElement("dependencies".to_java)
214
+
215
+
216
+ File.read(mavenfile).each do |line|
217
+ group_id, artifact_id, version = line.sub(/jar\s+/, '').gsub(/[',]/,'').split(/[\s:]+/)
218
+
219
+ xmlStreamWriter.writeStartElement("dependency".to_java)
220
+ writeElement(xmlStreamWriter,"groupId",group_id)
221
+ writeElement(xmlStreamWriter,"artifactId",artifact_id)
222
+ writeElement(xmlStreamWriter,"version",version.to_s)
223
+ xmlStreamWriter.writeEndElement #dependency
224
+
225
+ end
226
+
227
+ xmlStreamWriter.writeEndElement #dependencies
228
+
229
+ xmlStreamWriter.writeEndElement #project
230
+
231
+ xmlStreamWriter.writeEndDocument
232
+ xmlStreamWriter.close
233
+ out.close
234
+
235
+ puts File.read pomfile
236
+
237
+ execute(["dependency:resolve","dependency:build-classpath"],pomfile,{"mdep.outputFile" => "cp.txt","mdep.fileSeparator"=>"/"})
238
+
239
+ FileUtils.mkdir_p(File.dirname(classpathfile))
240
+ File.open(classpathfile, 'w') do |f|
241
+ path_separator = java.lang.System.getProperty("path.separator").to_s
242
+ File.read(File.join(temp_dir,"cp.txt")).each do |line|
243
+ line.split(/#{path_separator}/).each do |path|
244
+ f.puts "require '#{path}'"
245
+ end
246
+ end
247
+ f.close
248
+ end
249
+ end
250
+
251
+ end
252
+ end