Skip to content

Commit

Permalink
get proxy support in place
Browse files Browse the repository at this point in the history
fixes #16
  • Loading branch information
mkristian committed Nov 19, 2016
1 parent 655a68d commit da5a356
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 57 deletions.
75 changes: 20 additions & 55 deletions lib/jar_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
require 'jars/maven_settings'
module Jars
unless defined? Jars::SKIP_LOCK
MAVEN_SETTINGS = 'JARS_MAVEN_SETTINGS'.freeze
Expand Down Expand Up @@ -151,60 +152,24 @@ def lock_path( basedir = nil )

def reset
instance_variables.each { |var| instance_variable_set(var, nil) }
Jars::MavenSettings.reset
( @@jars ||= {} ).clear
end

def maven_local_settings
unless instance_variable_defined?(:@_jars_maven_local_settings_)
@_jars_maven_local_settings_ = nil
end
if @_jars_maven_local_settings_.nil?
if settings = absolute( 'settings.xml' )
if File.exists?(settings)
@_jars_maven_local_settings_ = settings
end
end
end
@_jars_maven_local_settings_ || nil
Jars::MavenSettings.local_settings
end

def maven_user_settings
unless instance_variable_defined?(:@_jars_maven_user_settings_)
@_jars_maven_user_settings_ = nil
end
if @_jars_maven_user_settings_.nil?
if settings = absolute( to_prop( MAVEN_SETTINGS ) )
unless File.exists?(settings)
Jars.warn { "configured ENV['#{MAVEN_SETTINGS}'] = '#{settings}' not found" }
settings = false
end
else # use maven default (user) settings
settings = File.join( user_home, '.m2', 'settings.xml' )
settings = false unless File.exists?(settings)
end
@_jars_maven_user_settings_ = settings
end
@_jars_maven_user_settings_ || nil
Jars::MavenSettings.user_settings
end

def maven_settings
maven_local_settings || maven_user_settings
Jars::MavenSettings.settings
end

def maven_global_settings
unless instance_variable_defined?(:@_jars_maven_global_settings_)
@_jars_maven_global_settings_ = nil
end
if @_jars_maven_global_settings_.nil?
if mvn_home = ENV[ 'M2_HOME' ] || ENV[ 'MAVEN_HOME' ]
settings = File.join( mvn_home, 'conf/settings.xml' )
settings = false unless File.exists?(settings)
else
settings = false
end
@_jars_maven_global_settings_ = settings
end
@_jars_maven_global_settings_ || nil
Jars::MavenSettings.global_settings
end

def local_maven_repo
Expand Down Expand Up @@ -297,6 +262,20 @@ def debug(msg = nil, &block)
Kernel.warn(msg || block.call) if verbose?
end

def absolute( file )
File.expand_path( file ) if file
end

def user_home
ENV[ 'HOME' ] || begin
user_home = Dir.home if Dir.respond_to?(:home)
unless user_home
user_home = ENV_JAVA[ 'user.home' ] if Object.const_defined?(:ENV_JAVA)
end
user_home
end
end

private

def require_jar_with_block( group_id, artifact_id, *classifier_version )
Expand All @@ -319,20 +298,6 @@ def require_jar_with_block( group_id, artifact_id, *classifier_version )
end
end

def absolute( file )
File.expand_path( file ) if file
end

def user_home
ENV[ 'HOME' ] || begin
user_home = Dir.home if Dir.respond_to?(:home)
unless user_home
user_home = ENV_JAVA[ 'user.home' ] if Object.const_defined?(:ENV_JAVA)
end
user_home
end
end

def detect_local_repository(settings)
return nil unless settings

Expand Down
2 changes: 1 addition & 1 deletion lib/jars/maven_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def setup( maven )
maven[ 'verbose' ] = (@debug || @verbose) == true

if Jars.maven_settings
maven.options[ '-s' ] = Jars.maven_settings
maven.options[ '-s' ] = Jars::MavenSettings.effective_settings
end

maven[ 'maven.repo.local' ] = "#{java.io.File.new( Jars.local_maven_repo ).absolute_path}"
Expand Down
139 changes: 139 additions & 0 deletions lib/jars/maven_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
require 'jar_dependencies'
require 'rubygems/request'
require 'rubygems/uri_formatter'
module Jars
class MavenSettings

LINE_SEPARATOR = java.lang.System.line_separator

class << self

def local_settings
unless instance_variable_defined?(:@_jars_maven_local_settings_)
@_jars_maven_local_settings_ = nil
end
if @_jars_maven_local_settings_.nil?
if settings = Jars.absolute( 'settings.xml' )
if File.exists?(settings)
@_jars_maven_local_settings_ = settings
end
end
end
@_jars_maven_local_settings_ || nil
end

def user_settings
unless instance_variable_defined?(:@_jars_maven_user_settings_)
@_jars_maven_user_settings_ = nil
end
if @_jars_maven_user_settings_.nil?
if settings = Jars.absolute( Jars.to_prop( MAVEN_SETTINGS ) )
unless File.exists?(settings)
Jars.warn { "configured ENV['#{MAVEN_SETTINGS}'] = '#{settings}' not found" }
settings = false
end
else # use maven default (user) settings
settings = File.join( Jars.user_home, '.m2', 'settings.xml' )
settings = false unless File.exists?(settings)
end
@_jars_maven_user_settings_ = settings
end
@_jars_maven_user_settings_ || nil
end

def effective_settings
unless instance_variable_defined?(:@_jars_effective_maven_settings_)
@_jars_effective_maven_settings_ = nil
end
if @_jars_effective_maven_settings_.nil?
http = Gem::Request.proxy_uri(Gem.configuration[:http_proxy] || Gem::Request.get_proxy_from_env('http'))
https = Gem::Request.proxy_uri(Gem.configuration[:https_proxy] || Gem::Request.get_proxy_from_env('https'))
if http.nil? && https.nil?
@_jars_effective_maven_settings_ = settings
else
@_jars_effective_maven_settings_ =
setup_interpolated_settings(http, https) || settings
end
end
@_jars_effective_maven_settings_
end

def cleanup
if effective_settings != settings
File.unlink(effective_settings)
end
ensure
reset
end

def reset
instance_variables.each { |var| instance_variable_set(var, nil) }
end

def settings
unless instance_variable_defined?(:@_jars_maven_settings_)
@_jars_maven_settings_ = nil
end
if @_jars_maven_settings_.nil?
local_settings || user_settings
end
end

def global_settings
unless instance_variable_defined?(:@_jars_maven_global_settings_)
@_jars_maven_global_settings_ = nil
end
if @_jars_maven_global_settings_.nil?
if mvn_home = ENV[ 'M2_HOME' ] || ENV[ 'MAVEN_HOME' ]
settings = File.join( mvn_home, 'conf/settings.xml' )
settings = false unless File.exists?(settings)
else
settings = false
end
@_jars_maven_global_settings_ = settings
end
@_jars_maven_global_settings_ || nil
end

private

def setup_interpolated_settings(http, https)
proxy = raw_proxy_settings_xml(http, https).gsub("\n", LINE_SEPARATOR)
if settings.nil?
raw = "<settings>#{LINE_SEPARATOR}#{proxy}</settings>"
else
raw = File.read(settings)
if raw.includes?('<proxy>')
Jars.info("can not interpolated proxy info for #{settings}")
return
else
raw.sub!("<settings>", "<settings>#{LINE_SEPARATOR}#{proxy}")
end
end
tempfile = java.io.File.create_temp_file('settings', '.xml')
tempfile.delete_on_exit
File.write(tempfile.path, raw)
tempfile.path
end

def raw_proxy_settings_xml(http, https)
raw = File.read(File.join(File.dirname(__FILE__), 'settings.xml'))
if http
raw.sub!('__HTTP_ACTIVE__', 'true')
raw.sub!('__HTTP_SERVER__', http.host)
raw.sub!('__HTTP_PORT__', http.port.to_s)
else
raw.sub!('__HTTP_ACTIVE__', 'false')
end
if https
raw.sub!('__HTTPS_ACTIVE__', 'true')
raw.sub!('__HTTPS_SERVER__', https.host)
raw.sub!('__HTTPS_PORT__', https.port.to_s)
else
raw.sub!('__HTTPS_ACTIVE__', 'false')
end
raw
end
end
end
end
20 changes: 20 additions & 0 deletions lib/jars/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<proxies>
<proxy>
<active>__HTTP_ACTIVE__</active>
<protocol>http</protocol>
<host>__HTTP_SERVER__</host>
<port>__HTTP_PORT__</port>
<!--nonProxyHosts>
www.oracle.com|localhost|127.0.0.1
</nonProxyHosts-->
</proxy>
<proxy>
<active>__HTTPS_ACTIVE__</active>
<protocol>https</protocol>
<host>__HTTPS_SERVER__</host>
<port>__HTTPS_PORT__</port>
<!--nonProxyHosts>
www.oracle.com|localhost|127.0.0.1
</nonProxyHosts-->
</proxy>
</proxies>
2 changes: 1 addition & 1 deletion lib/jars/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Jars
VERSION = '0.3.5'.freeze
VERSION = '0.3.6'.freeze
JRUBY_PLUGINS_VERSION = '1.1.3'.freeze
DEPENDENCY_PLUGIN_VERSION = '2.8'.freeze
end

0 comments on commit da5a356

Please sign in to comment.