From 678919ad4098fc831f86b873fe0e89790325a2c5 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Thu, 5 May 2005 06:24:07 +0000 Subject: [PATCH] PR: MNG-145 add mirror notation to settings, introduce to bootstrap git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@168282 13f79535-47bb-0310-9956-ffa450edef68 --- maven-mboot2/src/main/java/MBoot.java | 124 +++++++++++++++++- .../java/download/ArtifactDownloader.java | 71 ++++------ .../src/main/java/model/ModelReader.java | 6 +- .../src/main/java/model/Repository.java | 19 ++- maven-settings/settings.mdo | 69 +++++++++- 5 files changed, 239 insertions(+), 50 deletions(-) diff --git a/maven-mboot2/src/main/java/MBoot.java b/maven-mboot2/src/main/java/MBoot.java index 324ad8fa80..049cd4dc67 100644 --- a/maven-mboot2/src/main/java/MBoot.java +++ b/maven-mboot2/src/main/java/MBoot.java @@ -242,16 +242,44 @@ public class MBoot online = false; } - Repository localRepository = new Repository( mavenRepoLocal, Repository.LAYOUT_DEFAULT ); + Repository localRepository = new Repository( "local", mavenRepoLocal, Repository.LAYOUT_DEFAULT ); if ( online ) { - downloader = new ArtifactDownloader( localRepository, Collections.EMPTY_LIST ); + downloader = new ArtifactDownloader( localRepository ); if ( userModelReader.getActiveProxy() != null ) { Proxy proxy = userModelReader.getActiveProxy(); downloader.setProxy( proxy.getHost(), proxy.getPort(), proxy.getUserName(), proxy.getPassword() ); } + + List remoteRepos = downloader.getRemoteRepositories(); + List newRemoteRepos = new ArrayList(); + + for ( Iterator i = remoteRepos.iterator(); i.hasNext(); ) + { + Repository repo = (Repository) i.next(); + + boolean foundMirror = false; + for ( Iterator j = userModelReader.getMirrors().iterator(); j.hasNext() && !foundMirror; ) + { + Mirror m = (Mirror) j.next(); + if ( m.getMirrorOf().equals( repo.getId() ) ) + { + newRemoteRepos.add( new Repository( m.getId(), m.getUrl(), repo.getLayout() ) ); + foundMirror = true; + } + } + if ( !foundMirror ) + { + newRemoteRepos.add( repo ); + } + } + + downloader.setRemoteRepositories( newRemoteRepos ); + + System.out.println( "Using the following for your local repository: " + localRepository ); + System.out.println( "Using the following for your remote repository: " + newRemoteRepos ); } String basedir = System.getProperty( "user.dir" ); @@ -948,6 +976,8 @@ public class MBoot class SettingsReader extends AbstractReader { + private List mirrors = new ArrayList(); + private List profiles = new ArrayList(); private Profile currentProfile = null; @@ -962,6 +992,8 @@ public class MBoot private Proxy activeProxy = null; + private Mirror currentMirror; + public Profile getActiveProfile() { return activeProfile; @@ -1018,7 +1050,7 @@ public class MBoot } else { - throw new SAXException( "Invalid proxy entry. Missing one or more " + "fields: {host, port}." ); + throw new SAXException( "Invalid proxy entry. Missing one or more fields: {host, port}." ); } } else if ( currentProxy != null ) @@ -1054,6 +1086,41 @@ public class MBoot throw new SAXException( "Illegal element inside proxy: \'" + rawName + "\'" ); } } + else if ( "mirror".equals( rawName ) ) + { + if ( notEmpty( currentMirror.getId() ) && notEmpty( currentMirror.getMirrorOf() ) && + notEmpty( currentMirror.getUrl() ) ) + { + mirrors.add( currentMirror ); + currentMirror = null; + } + else + { + throw new SAXException( "Invalid mirror entry. Missing one or more fields: {id, mirrorOf, url}." ); + } + } + else if ( currentMirror != null ) + { + if ( "id".equals( rawName ) ) + { + currentMirror.setId( currentBody.toString().trim() ); + } + else if ( "mirrorOf".equals( rawName ) ) + { + currentMirror.setMirrorOf( currentBody.toString().trim() ); + } + else if ( "url".equals( rawName ) ) + { + currentMirror.setUrl( currentBody.toString().trim() ); + } + else if ( "name".equals( rawName ) ) + { + } + else + { + throw new SAXException( "Illegal element inside proxy: \'" + rawName + "\'" ); + } + } else if ( "settings".equals( rawName ) ) { if ( profiles.size() == 1 ) @@ -1103,6 +1170,10 @@ public class MBoot { currentProxy = new Proxy(); } + else if ( "mirror".equals( rawName ) ) + { + currentMirror = new Mirror(); + } } public void reset() @@ -1111,8 +1182,15 @@ public class MBoot this.activeProfile = null; this.activeProxy = null; this.currentProfile = null; + this.currentMirror = null; this.profiles.clear(); this.proxies.clear(); + this.mirrors.clear(); + } + + public List getMirrors() + { + return mirrors; } } @@ -1143,7 +1221,7 @@ public class MBoot } } - public class Proxy + public static class Proxy { private boolean active; @@ -1206,4 +1284,42 @@ public class MBoot } } + public static class Mirror + { + private String id; + + private String mirrorOf; + + private String url; + + public String getId() + { + return id; + } + + public void setId( String id ) + { + this.id = id; + } + + public void setMirrorOf( String mirrorOf ) + { + this.mirrorOf = mirrorOf; + } + + public void setUrl( String url ) + { + this.url = url; + } + + public String getMirrorOf() + { + return mirrorOf; + } + + public String getUrl() + { + return url; + } + } } diff --git a/maven-mboot2/src/main/java/download/ArtifactDownloader.java b/maven-mboot2/src/main/java/download/ArtifactDownloader.java index 6d555084b1..01f30e746f 100644 --- a/maven-mboot2/src/main/java/download/ArtifactDownloader.java +++ b/maven-mboot2/src/main/java/download/ArtifactDownloader.java @@ -9,19 +9,15 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; +import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Set; -import java.util.HashMap; import java.util.Map; public class ArtifactDownloader { public static final String SNAPSHOT_SIGNATURE = "-SNAPSHOT"; - private List remoteRepos; - private boolean useTimestamp = true; private boolean ignoreErrors = false; @@ -40,11 +36,11 @@ public class ArtifactDownloader private Map downloadedArtifacts = new HashMap(); - public ArtifactDownloader( Repository localRepository, List remoteRepositories ) + private List remoteRepositories; + + public ArtifactDownloader( Repository localRepository ) throws Exception { - setRemoteRepos( remoteRepositories ); - if ( localRepository == null ) { System.err.println( "local repository not specified" ); @@ -53,9 +49,6 @@ public class ArtifactDownloader } this.localRepository = localRepository; - - System.out.println( "Using the following for your local repository: " + localRepository ); - System.out.println( "Using the following for your remote repositories: " + remoteRepos ); } public void setProxy( String host, String port, String userName, String password ) @@ -115,28 +108,11 @@ public class ArtifactDownloader return dep.getVersion().indexOf( SNAPSHOT_SIGNATURE ) >= 0; } - private void setRemoteRepos( List repositories ) - { - remoteRepos = new ArrayList(); - - if ( repositories != null ) - { - remoteRepos.addAll( repositories ); - } - - if ( repositories.isEmpty() ) - { - // TODO: use super POM? - Repository repository = new Repository( REPO_URL, Repository.LAYOUT_DEFAULT ); - remoteRepos.add( repository ); - } - } - private boolean getRemoteArtifact( Dependency dep, File destinationFile ) { boolean fileFound = false; - for ( Iterator i = remoteRepos.iterator(); i.hasNext(); ) + for ( Iterator i = getRemoteRepositories().iterator(); i.hasNext(); ) { Repository remoteRepo = (Repository) i.next(); @@ -177,7 +153,8 @@ public class ArtifactDownloader { File file = localRepository.getMetadataFile( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getType(), - dep.getArtifactId() + "-" + dep.getResolvedVersion() + ".pom" ); + dep.getArtifactId() + "-" + dep.getResolvedVersion() + + ".pom" ); file.getParentFile().mkdirs(); @@ -250,19 +227,6 @@ public class ArtifactDownloader return filename.substring( 0, index ) + s; } - private String replace( String text, String repl, String with ) - { - StringBuffer buf = new StringBuffer( text.length() ); - int start = 0, end = 0; - while ( ( end = text.indexOf( repl, start ) ) != -1 ) - { - buf.append( text.substring( start, end ) ).append( with ); - start = end + repl.length(); - } - buf.append( text.substring( start ) ); - return buf.toString(); - } - private void log( String message ) { System.out.println( message ); @@ -272,4 +236,25 @@ public class ArtifactDownloader { return localRepository; } + + public List getRemoteRepositories() + { + if ( remoteRepositories == null ) + { + remoteRepositories = new ArrayList(); + } + + if ( remoteRepositories.isEmpty() ) + { + // TODO: use super POM? + remoteRepositories.add( new Repository( "central", REPO_URL, Repository.LAYOUT_DEFAULT ) ); + } + + return remoteRepositories; + } + + public void setRemoteRepositories( List remoteRepositories ) + { + this.remoteRepositories = remoteRepositories; + } } diff --git a/maven-mboot2/src/main/java/model/ModelReader.java b/maven-mboot2/src/main/java/model/ModelReader.java index 0effa25571..29221f653f 100644 --- a/maven-mboot2/src/main/java/model/ModelReader.java +++ b/maven-mboot2/src/main/java/model/ModelReader.java @@ -312,7 +312,11 @@ public class ModelReader } else if ( insideRepository ) { - if ( rawName.equals( "url" ) ) + if ( rawName.equals( "id" ) ) + { + currentRepository.setId( getBodyText() ); + } + else if ( rawName.equals( "url" ) ) { currentRepository.setBasedir( getBodyText() ); } diff --git a/maven-mboot2/src/main/java/model/Repository.java b/maven-mboot2/src/main/java/model/Repository.java index 542dca5109..8068ef9ed8 100644 --- a/maven-mboot2/src/main/java/model/Repository.java +++ b/maven-mboot2/src/main/java/model/Repository.java @@ -34,12 +34,15 @@ public class Repository private String layout; + private String id; + public Repository() { } - public Repository( String basedir, String layout ) + public Repository( String id, String basedir, String layout ) { + this.id = id; this.basedir = basedir; this.layout = layout; } @@ -135,4 +138,18 @@ public class Repository this.layout = layout; } + public String getId() + { + return id; + } + + public void setId( String id ) + { + this.id = id; + } + + public String getLayout() + { + return layout; + } } diff --git a/maven-settings/settings.mdo b/maven-settings/settings.mdo index 94de91d20e..4d40aa1385 100644 --- a/maven-settings/settings.mdo +++ b/maven-settings/settings.mdo @@ -61,6 +61,17 @@ * + + mirrors + 1.0.0 + + Configuration of download mirrors for repositories. + + + Mirror + * + + profiles 1.0.0 @@ -348,5 +359,61 @@ + + Mirror + 1.0.0 + + A download mirror for a given repository. + + + + id + true + 1.0.0 + String + + The server ID of this mirror. This must -not- be the same as that of the repository you are mirroring. + + + + mirrorOf + true + 1.0.0 + String + + The server ID of the repository being mirrored, eg "central". + + + + name + false + 1.0.0 + String + + The optional name that describes the mirror. + + + + url + true + 1.0.0 + String + + The URL of the mirror repository. + + + + + - \ No newline at end of file +