Author: jvanzyl Date: Thu Jul 2 14:02:18 2009 New Revision: 790596 URL: http://svn.apache.org/viewvc?rev=790596&view=rev Log: MANTTASKS-141: Using expressions in a mirrorOf element brakes the downloading of depencies
Modified: maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AntDownloadMonitor.java Modified: maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java URL: http://svn.apache.org/viewvc/maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java?rev=790596&r1=790595&r2=790596&view=diff ============================================================================== --- maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java (original) +++ maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AbstractArtifactTask.java Thu Jul 2 14:02:18 2009 @@ -19,6 +19,21 @@ * under the License. */ +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.manager.WagonManager; @@ -59,16 +74,6 @@ import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import java.io.File; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - /** * Base class for artifact tasks. * @@ -78,8 +83,16 @@ public abstract class AbstractArtifactTask extends Task { + private static final String WILDCARD = "*"; + + private static final String EXTERNAL_WILDCARD = "external:*"; + + private static int anonymousMirrorIdSeed = 0; + private static ClassLoader plexusClassLoader; + private Map/*<String, ArtifactRepository>*/ mirrors = new LinkedHashMap/*<String, ArtifactRepository>*/(); + private File userSettingsFile; private File globalSettingsFile; @@ -439,15 +452,23 @@ repository.addProxy( new Proxy( proxy ) ); } } - - Mirror mirror = getSettings().getMirrorOf( repository.getId() ); - if ( mirror == null ) - { - mirror = getSettings().getMirrorOf( "*" ); - } - if ( mirror != null ) + + if ( getSettings().getMirrors() != null ) { - repository.setUrl( mirror.getUrl() ); + for ( Iterator i = getSettings().getMirrors().iterator(); i.hasNext(); ) + { + Mirror mirror = (Mirror) i.next(); + + addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); + } + + ArtifactRepository mirrorRepository = (ArtifactRepository) getMirrors( + Arrays.asList( new ArtifactRepository[] { new DefaultArtifactRepository( repository.getId(), repository.getUrl(), null ) } ) ).get( 0 ); + + if ( mirrorRepository != null ) + { + repository.setUrl( mirrorRepository.getUrl() ); + } } } @@ -694,4 +715,195 @@ * The main entry point for the task. */ protected abstract void doExecute(); + + // + // Code taken from 3.x to deal with external:* + // + + public void addMirror( String id, String mirrorOf, String url ) + { + if ( id == null ) + { + id = "mirror-" + anonymousMirrorIdSeed++; + } + + ArtifactRepository mirror = new DefaultArtifactRepository( id, url, null ); + + if ( !mirrors.containsKey( mirrorOf ) ) + { + mirrors.put( mirrorOf, mirror ); + } + } + + /** + * This method finds a matching mirror for the selected repository. If there is an exact match, + * this will be used. If there is no exact match, then the list of mirrors is examined to see if + * a pattern applies. + * + * @param originalRepository See if there is a mirror for this repository. + * @return the selected mirror or null if none are found. + */ + public ArtifactRepository getMirror( ArtifactRepository originalRepository ) + { + ArtifactRepository selectedMirror = (ArtifactRepository) mirrors.get( originalRepository.getId() ); + if ( null == selectedMirror ) + { + // Process the patterns in order. First one that matches wins. + Set/*<String>*/ keySet = mirrors.keySet(); + if ( keySet != null ) + { + for ( Iterator i = keySet.iterator(); i.hasNext(); ) + { + String pattern = (String) i.next(); + + if ( matchPattern( originalRepository, pattern ) ) + { + selectedMirror = (ArtifactRepository) mirrors.get( pattern ); + //stop on the first match. + break; + } + } + } + } + + return selectedMirror; + } + + public void clearMirrors() + { + mirrors.clear(); + anonymousMirrorIdSeed = 0; + } + + public List/*<ArtifactRepository>*/ getMirrors( List/*<ArtifactRepository>*/ remoteRepositories ) + { + if ( remoteRepositories != null ) + { + for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); ) + { + ArtifactRepository repository = (ArtifactRepository) i.next(); + + // Check to see if we have a valid mirror for this repository + ArtifactRepository mirror = getMirror( repository ); + + if ( mirror != null ) + { + // We basically just want to take the URL + ((org.apache.maven.wagon.repository.Repository)repository).setUrl( mirror.getUrl() ); + + // I would like a mirrored repository to be visually different but we'll put another field + // in the repository as changing the ID hoses up authentication. + ((org.apache.maven.wagon.repository.Repository)repository).setId( mirror.getId() ); + } + } + } + + return remoteRepositories; + } + + // Make these available to tests + + ArtifactRepository getMirrorRepository( ArtifactRepository repository ) + { + ArtifactRepository mirror = getMirror( repository ); + if ( mirror != null ) + { + String id = mirror.getId(); + if ( id == null ) + { + // TODO: this should be illegal in settings.xml + id = repository.getId(); + } + + repository = getArtifactRepositoryFactory().createArtifactRepository( id, mirror.getUrl(), repository.getLayout(), repository.getSnapshots(), repository.getReleases() ); + } + return repository; + } + + private ArtifactRepositoryFactory getArtifactRepositoryFactory() + { + return (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + } + + /** + * This method checks if the pattern matches the originalRepository. Valid patterns: * = + * everything external:* = everything not on the localhost and not file based. repo,repo1 = repo + * or repo1 *,!repo1 = everything except repo1 + * + * @param originalRepository to compare for a match. + * @param pattern used for match. Currently only '*' is supported. + * @return true if the repository is a match to this pattern. + */ + boolean matchPattern( ArtifactRepository originalRepository, String pattern ) + { + boolean result = false; + String originalId = originalRepository.getId(); + + // simple checks first to short circuit processing below. + if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) ) + { + result = true; + } + else + { + // process the list + String[] repos = pattern.split( "," ); + + for ( int i = 0; i < repos.length; i++ ) + { + String repo = repos[i]; + + // see if this is a negative match + if ( repo.length() > 1 && repo.startsWith( "!" ) ) + { + if ( originalId.equals( repo.substring( 1 ) ) ) + { + // explicitly exclude. Set result and stop processing. + result = false; + break; + } + } + // check for exact match + else if ( originalId.equals( repo ) ) + { + result = true; + break; + } + // check for external:* + else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) ) + { + result = true; + // don't stop processing in case a future segment explicitly excludes this repo + } + else if ( WILDCARD.equals( repo ) ) + { + result = true; + // don't stop processing in case a future segment explicitly excludes this repo + } + } + } + return result; + } + + + /** + * Checks the URL to see if this repository refers to an external repository + * + * @param originalRepository + * @return true if external. + */ + boolean isExternalRepo( ArtifactRepository originalRepository ) + { + try + { + URL url = new URL( originalRepository.getUrl() ); + return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" ) || url.getProtocol().equals( "file" ) ); + } + catch ( MalformedURLException e ) + { + // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it + return false; + } + } + } Modified: maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AntDownloadMonitor.java URL: http://svn.apache.org/viewvc/maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AntDownloadMonitor.java?rev=790596&r1=790595&r2=790596&view=diff ============================================================================== --- maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AntDownloadMonitor.java (original) +++ maven/ant-tasks/trunk/src/main/java/org/apache/maven/artifact/ant/AntDownloadMonitor.java Thu Jul 2 14:02:18 2009 @@ -60,7 +60,7 @@ String message = event.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading"; String dest = event.getRequestType() == TransferEvent.REQUEST_PUT ? " to " : " from "; - log( message + ": " + event.getResource().getName() + dest + event.getWagon().getRepository().getId() ); + log( message + ": " + event.getResource().getName() + dest + event.getWagon().getRepository().getId() + "[" + event.getWagon().getRepository().getUrl() + "]" ); } public void transferProgress( TransferEvent event, byte[] bytes, int i )