Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Maven support with JRuby #1683

Closed
wants to merge 11 commits into from
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ See [UPGRADING](https://github.com/carlhuda/bundler/blob/master/UPGRADING.md).
### Other questions

Feel free to chat with the Bundler core team (and many other users) on IRC in the [#bundler](irc://irc.freenode.net/bundler) channel on Freenode, or via email on the [Bundler mailing list](http://groups.google.com/group/ruby-bundler).


### Maven Integration (JRuby only)
This version of bundler allows maven dependencies to be included alongside your other Ruby dependencies opening up a whole new world of possibilities!
There are two ways to add your maven dependencies:
<ol>
<li> mvn "repo URL (or 'default' for the default repo URL)" do
gem "mvn:<group_id>:<artifact_id>", "version number"
end</li>
<li> gem "mvn:<group_id>:<artifact_id>", "version number", :mvn=>"repo URL (or 'default' for the default repo URL)" </li>
</ol>
This integration will download the right jar using maven into your *maven* repo location and simply write the necessary ruby files to require the jars from the
right location in the maven repository. This allows for maven repos and gem repos to live side by side without duplication of binaries and ensure that dependencies
are resolved properly as maven does that automatically.
4 changes: 4 additions & 0 deletions lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class InvalidSpecSet < StandardError; end
class << self
attr_writer :ui, :bundle_path

def java?
RUBY_PLATFORM == "java"
end

def configure
@configured ||= begin
configure_gem_home_and_path
Expand Down
51 changes: 43 additions & 8 deletions lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'bundler/dependency'

module Bundler
class Dsl
def self.evaluate(gemfile, lockfile, unlock)
Expand Down Expand Up @@ -45,7 +44,31 @@ def gemspec(opts = nil)
raise InvalidOption, "There are multiple gemspecs at #{path}. Please use the :name option to specify which one."
end
end


#START MAVEN STUFF

#Influenced from https://github.com/jkutner/bundler/blob/master/lib/bundler/dsl.rb
def mvn(repo, options={}, source_options={}, &blk)
unless Bundler.java?
raise InvalidOption, "mvn can only be executed in JRuby"
end

if (options['name'].nil? || options['version'].nil?) and !block_given?
raise InvalidOption, 'Must specify a dependency name+version, or block of dependencies.'
end
puts "MVN REPO=#{repo} OPTS = #{options.inspect} SOPTS = #{source_options.inspect}"
remotes = Array === repo ? repo : [repo]
local_source = source Source::Maven.new(_normalize_hash(options).merge('remotes' => remotes)), source_options, &blk

#This is when you specify the mvn information on the gem line
#i.e. gem ... :mvn=> ...
if options['name'] && options['version']
local_source.add_dependency(options['name'], options['version'])
end
local_source
end
#END MAVEN STUFF

def gem(name, *args)
if name.is_a?(Symbol)
raise GemfileError, %{You need to specify gem names as Strings. Use 'gem "#{name.to_s}"' instead.}
Expand All @@ -56,8 +79,15 @@ def gem(name, *args)

_deprecated_options(options)
_normalize_options(name, version, options)

dep = Dependency.new(name, version, options)
#Do a custom dependency creation for Maven dependencies because
#maven source needs the mvn: prefixed name while everything else
#will use the maven_name for dependency resolution, gem require etc.
if options['source'] && options['source'].is_a?(Bundler::Source::Maven)
mvn_source = options['source']
dep = Dependency.new(mvn_source.maven_name(name), version, options)
else
dep = Dependency.new(name, version, options)
end

# if there's already a dependency with this name we try to prefer one
if current = @dependencies.find { |d| d.name == dep.name }
Expand Down Expand Up @@ -85,7 +115,13 @@ def gem(name, *args)
end
end
end


#@source is populated if gem is called from within the mvn block
#i.e. mvn do gem ... end
#need to add the dependency to the maven source
if !@source.nil? && @source.is_a?(Bundler::Source::Maven)
@source.add_dependency(name,version[0])
end
@dependencies << dep
end

Expand Down Expand Up @@ -185,7 +221,7 @@ def _normalize_hash(opts)
def _normalize_options(name, version, opts)
_normalize_hash(opts)

invalid_keys = opts.keys - %w(group groups git github path name branch ref tag require submodules platform platforms type)
invalid_keys = opts.keys - %w(group groups git github path name branch ref tag require submodules platform platforms type mvn)
if invalid_keys.any?
plural = invalid_keys.size > 1
message = "You passed #{invalid_keys.map{|k| ':'+k }.join(", ")} "
Expand Down Expand Up @@ -215,8 +251,7 @@ def _normalize_options(name, version, opts)
github = "#{github}/#{github}" unless github.include?("/")
opts["git"] = "git://github.com/#{github}.git"
end

["git", "path"].each do |type|
["git", "path","mvn"].each do |type|
if param = opts[type]
if version.first && version.first =~ /^\s*=?\s*(\d[^\s]*)\s*$/
options = opts.merge("name" => name, "version" => $1)
Expand Down
5 changes: 3 additions & 2 deletions lib/bundler/lockfile_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ def initialize(lockfile)
TYPES = {
"GIT" => Bundler::Source::Git,
"GEM" => Bundler::Source::Rubygems,
"PATH" => Bundler::Source::Path
"PATH" => Bundler::Source::Path,
"MAVEN" => Bundler::Source::Maven
}

def parse_source(line)
case line
when "GIT", "GEM", "PATH"
when "GIT", "GEM", "PATH", "MAVEN"
@current_source = nil
@opts, @type = {}, line
when " specs:"
Expand Down
Loading