Permalink
Browse files

added mirror and proxy support. improved configurations

  • Loading branch information...
1 parent eead94e commit b6bf27cf95fa9e3642fe2e3b25b717c899eb7baa @mkristian committed Jan 24, 2013
Showing with 131 additions and 343 deletions.
  1. +17 −62 lib/jbundler/aether.rb
  2. +25 −5 lib/jbundler/config.rb
  3. +89 −276 src/main/java/jbundler/Aether.java
View
79 lib/jbundler/aether.rb
@@ -3,65 +3,6 @@
module JBundler
- # allow yaml config in $HOME/.jbundlerrc and $PWD/.jbundlerrc
- class AetherConfig
-
- attr_accessor :verbose, :local_repository, :jarfile, :gemfile, :skip
-
- def initialize
- file = '.jbundlerrc'
- homefile = File.join(ENV['HOME'], file)
- home_config = YAML.load_file(homefile) if File.exists?(homefile)
- pwd_config = YAML.load_file(file) if File.exists?(file)
- @config = (home_config || {}).merge(pwd_config || {})
- end
-
- if defined? JRUBY_VERSION
- def jbundler_env(key)
- ENV[key.upcase.gsub(/./, '_')] || java.lang.System.getProperty(key.downcase.gsub(/_/, '.')) || @config[key.downcase.sub(/^j?bundler/, '').sub(/./, '_')]
- end
- else
- def jbundler_env(key)
- ENV[key.upcase.gsub(/./, '_')] || @config[key.downcase.sub(/^j?bundler/, '').sub(/./, '_')]
- end
- end
- private :jbundler_env
-
- def skip
- skip = jbundler_env('JBUNDLE_SKIP')
- # defaults to false
- @skip ||= skip && skip != 'false'
- end
-
- def verbose
- verbose = jbundler_env('JBUNDLE_VERBOSE')
- # defaults to false
- @verbose ||= verbose && verbose != 'false'
- end
-
- def jarfile
- if File.exists?('Mvnfile')
- warn "'Mvnfile' name is deprecated, please use 'Jarfile' instead"
- @jarfile = 'Mvnfile'
- end
- @jarfile ||= jbundler_env('JBUNDLE_JARFILE') || 'Jarfile'
- end
-
- def gemfile
- @gemfile ||= jbundler_env('BUNDLE_GEMFILE') || 'Gemfile'
- end
-
- def classpath_file
- '.jbundler/classpath.rb'
- end
-
- def local_repository
- # use maven default local repo as default
- @local_maven_repository ||= (jbundler_env('JBUNDLE_LOCAL_REPOSITORY') ||
- File.join( ENV['HOME'], ".m2", "repository"))
- end
- end
-
class AetherRuby
def self.setup_classloader
@@ -79,17 +20,25 @@ def self.setup_classloader
java_import 'jbundler.Aether'
rescue NameError
# assume this happens only when working on the git clone
- raise "jbundler.jar is missing - maybe you need to build it first ? try\n$ rmvn prepare-package -Dmaven.test.skip\n"
+ raise "jbundler.jar is missing - maybe you need to build it first ? use\n$ rmvn prepare-package -Dmaven.test.skip\n"
end
end
java_import 'jbundler.Aether'
end
- def initialize(config = Config.new, offline = false)
+ def initialize( config = Config.new )
unless defined? Aether
self.class.setup_classloader
end
- @aether = Aether.new(config.local_repository, config.verbose, offline)
+ @aether = Aether.new( config.verbose )
+ @aether.add_proxy( config.proxy ) if config.proxy
+ @aether.add_mirror( config.mirror ) if config.mirror
+ @aether.offline = config.offline
+ @aether.user_settings = config.settings if config.settings
+ @aether.local_repository = config.local_repository if config.local_repository
+ rescue NativeException => e
+ e.cause.print_stack_trace
+ raise e
end
def add_artifact(coordinate, extension = nil)
@@ -108,6 +57,9 @@ def add_repository(name, url)
def resolve
@aether.resolve unless artifacts.empty?
+ rescue NativeException => e
+ e.cause.print_stack_trace
+ raise e
end
def classpath
@@ -140,6 +92,9 @@ def resolved_coordinates
def install(coordinate, file)
@aether.install(coordinate, file)
+ rescue NativeException => e
+ e.cause.print_stack_trace
+ raise e
end
end
View
30 lib/jbundler/config.rb
@@ -5,7 +5,7 @@ module JBundler
# allow yaml config in $HOME/.jbundlerrc and $PWD/.jbundlerrc
class Config
- attr_accessor :verbose, :local_repository, :jarfile, :gemfile, :skip
+ attr_accessor :verbose, :local_repository, :jarfile, :gemfile, :skip, :settings, :offline
def initialize
file = '.jbundlerrc'
@@ -17,11 +17,11 @@ def initialize
if defined? JRUBY_VERSION
def jbundler_env(key)
- ENV[key.upcase.gsub(/./, '_')] || java.lang.System.getProperty(key.downcase.gsub(/_/, '.')) || @config[key.downcase.sub(/^j?bundler/, '').sub(/./, '_')]
+ ENV[key.upcase.gsub(/[.]/, '_')] || java.lang.System.getProperty(key.downcase.gsub(/_/, '.')) || @config[key.downcase.sub(/^j?bundler/, '').sub(/[.]/, '_')]
end
else
def jbundler_env(key)
- ENV[key.upcase.gsub(/./, '_')] || @config[key.downcase.sub(/^j?bundler/, '').sub(/./, '_')]
+ ENV[key.upcase.gsub(/[.]/, '_')] || @config[key.downcase.sub(/^j?bundler/, '').sub(/[.]/, '_')]
end
end
private :jbundler_env
@@ -64,8 +64,28 @@ def classpath_file
def local_repository
# use maven default local repo as default
- @local_maven_repository ||= (jbundler_env('JBUNDLE_LOCAL_REPOSITORY') ||
- File.join( ENV['HOME'], ".m2", "repository"))
+ @local_maven_repository ||= jbundler_env('JBUNDLE_LOCAL_REPOSITORY')
+ end
+
+ def settings
+ @settings ||= jbundler_env('JBUNDLE_SETTINGS')
+ end
+
+ def offline
+ @offline ||= jbundler_env('JBUNDLE_OFFLINE')
+ @offline == 'true' || @offline == true
+ end
+
+ def proxy
+ @proxy ||= jbundler_env('JBUNDLE_PROXY')
+ end
+
+ def mirror
+ @mirror ||= jbundler_env('JBUNDLE_MIRROR')
+ end
+
+ def rubygems_mirror
+ @rubygems_mirror ||= jbundler_env('BUNDLE_RUBYGEMS_MIRROR')
end
end
end
View
365 src/main/java/jbundler/Aether.java
@@ -1,25 +1,19 @@
package jbundler;
import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
-import java.util.Properties;
import org.apache.maven.repository.internal.MavenRepositorySystemSession;
import org.apache.maven.repository.internal.MavenServiceLocator;
import org.apache.maven.settings.Mirror;
-import org.apache.maven.settings.Server;
-import org.apache.maven.settings.Settings;
-import org.apache.maven.settings.building.DefaultSettingsBuilderFactory;
-import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
-import org.apache.maven.settings.building.SettingsBuilder;
-import org.apache.maven.settings.building.SettingsBuildingException;
import org.sonatype.aether.ConfigurationProperties;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
@@ -34,58 +28,67 @@
import org.sonatype.aether.installation.InstallRequest;
import org.sonatype.aether.installation.InstallationException;
import org.sonatype.aether.repository.Authentication;
-import org.sonatype.aether.repository.AuthenticationSelector;
import org.sonatype.aether.repository.LocalRepository;
import org.sonatype.aether.repository.LocalRepositoryManager;
-import org.sonatype.aether.repository.MirrorSelector;
import org.sonatype.aether.repository.Proxy;
-import org.sonatype.aether.repository.ProxySelector;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.DependencyRequest;
import org.sonatype.aether.resolution.DependencyResolutionException;
import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
import org.sonatype.aether.spi.locator.ServiceLocator;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.graph.PreorderNodeListGenerator;
-import org.sonatype.aether.util.repository.ConservativeAuthenticationSelector;
-import org.sonatype.aether.util.repository.DefaultAuthenticationSelector;
-import org.sonatype.aether.util.repository.DefaultMirrorSelector;
-import org.sonatype.aether.util.repository.DefaultProxySelector;
public class Aether {
- private static final SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
- //private static SettingsDecrypter settingsDecrypter;// = new DefaultSettingsDecryptorFactory().newInstance();
private DependencyNode node;
- private RepositorySystem repoSystem;
+ private final RepositorySystem repoSystem;
private RepositorySystemSession session;
private List<Artifact> artifacts = new LinkedList<Artifact>();
private List<RemoteRepository> repos = new LinkedList<RemoteRepository>();
- private Installer installer;
+ private final Installer installer;
- private File globalSettings;
- private File userSettings;
- private List<Proxy> proxies = new LinkedList<Proxy>();;
- private Settings settings;
- private List<Mirror> mirrors = new LinkedList<Mirror>();
-// private List<Authentication> authentications;
- private Boolean offline;
- private LocalRepository localRepository;
+ private final AetherSettings settings = new AetherSettings();
+
+ private final boolean verbose;
- public Aether(String localRepo, boolean verbose, Boolean offline){
- setLocalRepository( new File(localRepo) );
+ public Aether(boolean verbose){
+ this.verbose = verbose;
ServiceLocator locator = newServiceLocator();
- repoSystem = locator.getService( RepositorySystem.class );
- installer = locator.getService( Installer.class );
-
- this.offline = offline;
+ this.repoSystem = locator.getService( RepositorySystem.class );
+ this.installer = locator.getService( Installer.class );
- session = newSession( repoSystem, verbose );
-
- RemoteRepository central = new RemoteRepository( "central", "default", "http://repo1.maven.org/maven2/" );
- repos.add(central);
+ repos.add( new RemoteRepository( "central",
+ "default",
+ "http://repo.maven.apache.org/maven2" ) );
}
+ private RepositorySystemSession getSession()
+ {
+ if (this.session == null)
+ {
+ MavenRepositorySystemSession s = new MavenRepositorySystemSession();
+
+ Map<Object, Object> configProps = new LinkedHashMap<Object, Object>();
+ configProps.put( ConfigurationProperties.USER_AGENT, settings.getUserAgent() );
+ configProps.putAll( System.getProperties() );
+ //configProps.putAll( (Map<?, ?>) getProperties() );
+ configProps.putAll( (Map<?, ?>) settings.getUserProperties() );
+ s.setConfigProps( configProps );
+
+ s.setLocalRepositoryManager( repoSystem.newLocalRepositoryManager( settings.getLocalRepository() ) );
+ s.setRepositoryListener( new SimpleRepositoryListener( verbose, s.getLocalRepositoryManager() ) );
+ s.setOffline(settings.isOffline());
+ s.setMirrorSelector(settings.getMirrorSelector());
+ s.setAuthenticationSelector(settings.getAuthSelector());
+ s.setProxySelector(settings.getProxySelector());
+ s.setUserProps(settings.getUserProperties());
+ s.setSystemProps(settings.getSystemProperties());
+ this.session = s;
+ }
+ return this.session;
+ }
+
private ServiceLocator newServiceLocator() {
MavenServiceLocator locator = new MavenServiceLocator();// when using maven 3.0.4
//locator.addService( RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.class );
@@ -96,263 +99,62 @@ private ServiceLocator newServiceLocator() {
return locator;
}
- private RepositorySystemSession newSession( RepositorySystem system, boolean verbose ) {
-
- MavenRepositorySystemSession session = new MavenRepositorySystemSession();
-
- Map<Object, Object> configProps = new LinkedHashMap<Object, Object>();
- configProps.put( ConfigurationProperties.USER_AGENT, getUserAgent() );
- configProps.putAll( System.getProperties() );
- //configProps.putAll( (Map<?, ?>) getProperties() );
- configProps.putAll( (Map<?, ?>) getUserProperties() );
- session.setConfigProps( configProps );
-
- session.setLocalRepositoryManager( system.newLocalRepositoryManager( getLocalRepository() ) );
- session.setRepositoryListener( new SimpleRepositoryListener( verbose, session.getLocalRepositoryManager() ) );
- session.setOffline(isOffline());
- session.setMirrorSelector(getMirrorSelector());
- session.setAuthenticationSelector(getAuthSelector());
- session.setProxySelector(getProxySelector());
- session.setUserProps(getUserProperties());
- session.setSystemProps(getSystemProperties());
- return session;
- }
-
- private LocalRepository getLocalRepository()
- {
- if ( localRepository != null && localRepository.getBasedir() != null )
- {
- return localRepository;
- }
- else
- {
- return new LocalRepository( getDefaultLocalRepoDir() );
- }
- }
-
- private File getDefaultLocalRepoDir()
- {
-
- Settings settings = getSettings();
- if ( settings.getLocalRepository() != null )
- {
- return new File( settings.getLocalRepository() );
- }
-
- return new File( new File( System.getProperty( "user.home" ), ".m2" ), "repository" );
- }
-
public void setLocalRepository( File localRepository )
{
- this.localRepository = new LocalRepository( localRepository );
- }
-
- private synchronized Settings getSettings()
- {
- if ( settings == null )
- {
- DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
- request.setUserSettingsFile( getUserSettings() );
- request.setGlobalSettingsFile( getGlobalSettings() );
- request.setSystemProperties( getSystemProperties() );
- request.setUserProperties( getUserProperties() );
-
- try
- {
- settings = settingsBuilder.build( request ).getEffectiveSettings();
- }
- catch ( SettingsBuildingException e )
- {
- //log( "Could not process settings.xml: " + e.getMessage(), e );
- }
-
-// SettingsDecryptionResult result =
-// settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( settings ) );
-// settings.setServers( result.getServers() );
-// settings.setProxies( result.getProxies() );
- }
- return settings;
- }
-
- private ProxySelector getProxySelector()
- {
- DefaultProxySelector selector = new DefaultProxySelector();
-
- for ( Proxy proxy : proxies )
- {
- selector.add( proxy, proxy.getHost() );
- }
-
- Settings settings = getSettings();
- for ( org.apache.maven.settings.Proxy proxy : settings.getProxies() )
- {
- Authentication auth = new Authentication( proxy.getUsername(), proxy.getPassword() );
-
- selector.add( new Proxy( proxy.getProtocol(), proxy.getHost(),
- proxy.getPort(), auth ),
- proxy.getNonProxyHosts() );
- }
-
- return selector;
- }
-
- private String getUserAgent()
- {
- StringBuilder buffer = new StringBuilder( 128 );
-
- buffer.append( "JBundler/" );//.append( getProperty( "jbundler.version" ) );
- buffer.append( " (" );
- buffer.append( "Java " ).append( System.getProperty( "java.version" ) );
- buffer.append( "; " );
- buffer.append( System.getProperty( "os.name" ) ).append( " " ).append( System.getProperty( "os.version" ) );
- buffer.append( ")" );
- buffer.append( " Aether" );
-
- return buffer.toString();
- }
-
- private boolean isOffline()
- {
-// String prop = project.getProperty( Names.PROPERTY_OFFLINE );
-// if ( prop != null )
-// {
-// return Boolean.parseBoolean( prop );
-// }
- return offline != null ? getSettings().isOffline() : offline;
- }
+ this.settings.setLocalRepository( new LocalRepository( localRepository ) );
+ }
- private Properties getSystemProperties()
+ public void addMirror( String url )
{
- Properties props = new Properties();
- getEnvProperties( props );
- props.putAll( System.getProperties() );
- return props;
+ Mirror mirror = new Mirror();
+ mirror.setId( "jbundler" );
+ mirror.setLayout( "default" );
+ mirror.setMirrorOf( "central" );
+ mirror.setName( "JBundler Maven Central Mirror" );
+ mirror.setUrl( url );
+ mirror.setMirrorOfLayouts( "*" );
+ settings.addMirror( mirror );
}
- private Properties getUserProperties()
- {
- Properties props = new Properties();
- return props;
- }
- private Properties getEnvProperties( Properties props )
- {
- if ( props == null )
- {
- props = new Properties();
- }
- boolean envCaseInsensitive = false;//Os.isFamily( "windows" );
- for ( Map.Entry<String, String> entry : System.getenv().entrySet() )
- {
- String key = entry.getKey();
- if ( envCaseInsensitive )
- {
- key = key.toUpperCase( Locale.ENGLISH );
- }
- key = "env." + key;
- props.put( key, entry.getValue() );
- }
- return props;
- }
-
- private MirrorSelector getMirrorSelector()
+ public void setOffline( Boolean offline )
{
- DefaultMirrorSelector selector = new DefaultMirrorSelector();
-
- for ( Mirror mirror : mirrors )
- {
- selector.add( mirror.getId(), mirror.getUrl(), "default", false, mirror.getMirrorOf(), null );
- }
-
- Settings settings = getSettings();
- for ( org.apache.maven.settings.Mirror mirror : settings.getMirrors() )
- {
- selector.add( String.valueOf( mirror.getId() ), mirror.getUrl(), mirror.getLayout(), false,
- mirror.getMirrorOf(), mirror.getMirrorOfLayouts() );
- }
-
- return selector;
+ this.settings.setOffline( offline );
}
-
- private AuthenticationSelector getAuthSelector()
- {
- DefaultAuthenticationSelector selector = new DefaultAuthenticationSelector();
-
-// Collection<String> ids = new HashSet<String>();
-// for ( Authentication auth : authentications )
-// {
-// List<String> servers = auth.getServers();
-// if ( !servers.isEmpty() )
-// {
-// org.eclipse.aether.repository.Authentication a = ConverterUtils.toAuthentication( auth );
-// for ( String server : servers )
-// {
-// if ( ids.add( server ) )
-// {
-// selector.add( server, a );
-// }
-// }
-// }
-// }
-
- Settings settings = getSettings();
- for ( Server server : settings.getServers() )
- {
- Authentication auth = new Authentication( server.getUsername(), server.getPassword(),
- server.getPrivateKey(), server.getPassphrase() );
- selector.add( server.getId(), auth );
- }
-
- return new ConservativeAuthenticationSelector( selector );
- }
-
- public synchronized void setUserSettings( File file )
+ public synchronized void setUserSettings( File file )
{
// if ( !eq( this.userSettings, file ) )
// {
// settings = null;
// }
- this.userSettings = file;
+ this.settings.setUserSettings( file );
}
- File getUserSettings()
+
+ public void addProxy( String url )
{
- if ( userSettings == null )
+ URL u;
+ try
+ {
+ u = new URL( url );
+ }
+ catch (MalformedURLException e)
{
- userSettings = new File( new File( System.getProperty( "user.home" ), ".m2" ), "settings.xml" );
+ throw new RuntimeException( "can not parse given url: " + url, e );
}
- return userSettings;
- }
-
-// public void setGlobalSettings( File file )
-// {
-// if ( !eq( this.globalSettings, file ) )
-// {
-// settings = null;
-// }
-// this.globalSettings = file;
-// }
- String getMavenHome()
+
+ final Authentication authentication;
+ final String userInfo = u.getUserInfo();
+ if ( userInfo != null && userInfo.contains( ":" ) )
{
- String mavenHome = System.getProperty( "maven.home" );
- if ( mavenHome != null )
- {
- return mavenHome;
- }
- return System.getenv( "M2_HOME" );
+ int i = userInfo.indexOf(':');
+ authentication = new Authentication( userInfo.substring( 0, i ), userInfo.substring( i + 1 ) );
}
- File getGlobalSettings()
- {
- if ( globalSettings == null )
+ else
{
- globalSettings = new File( new File( getMavenHome(), "conf" ), "settings.xml" );
+ authentication = new Authentication( null, (String)null );
}
- return globalSettings;
- }
-
- public void addProxy( Proxy proxy )
- {
- proxies.add( proxy );
+ settings.addProxy( new Proxy( u.getProtocol(), u.getHost(), u.getPort(), authentication ) );
}
public void addArtifact(String coordinate){
@@ -374,14 +176,25 @@ public void resolve() throws DependencyCollectionException, DependencyResolution
}
for( RemoteRepository r: repos ){
+ RemoteRepository mirror = settings.getMirrorSelector().getMirror( r );
+ if ( mirror != null )
+ {
+ r = mirror;
+ }
+ Proxy proxy = settings.getProxySelector().getProxy( r );
+ if ( proxy != null )
+ {
+ r.setProxy( proxy );
+ }
+
collectRequest.addRepository( r );
}
-
- node = repoSystem.collectDependencies( session, collectRequest ).getRoot();
+
+ node = repoSystem.collectDependencies( getSession(), collectRequest ).getRoot();
DependencyRequest dependencyRequest = new DependencyRequest( node, null );
- repoSystem.resolveDependencies( session, dependencyRequest );
+ repoSystem.resolveDependencies( getSession(), dependencyRequest );
}
public List<RemoteRepository> getRepositories(){
@@ -453,7 +266,7 @@ public String getClasspath() {
}
public void install(String coordinate, String file) throws InstallationException{
- LocalRepositoryManager lrm = session.getLocalRepositoryManager();
+ LocalRepositoryManager lrm = getSession().getLocalRepositoryManager();
Artifact artifact = new DefaultArtifact(coordinate);
@@ -462,7 +275,7 @@ public void install(String coordinate, String file) throws InstallationException
artifact = artifact.setFile(new File(file));
InstallRequest request = new InstallRequest();
request.addArtifact(artifact);
- installer.install(session, request);
+ installer.install(getSession(), request);
}
}
}

0 comments on commit b6bf27c

Please sign in to comment.