http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java new file mode 100644 index 0000000..0f31330 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java @@ -0,0 +1,415 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.commons.lang3.Validate; +import org.apache.maven.model.DistributionManagement; +import org.apache.maven.model.Model; +import org.apache.maven.model.Relocation; +import org.apache.maven.model.building.DefaultModelBuilderFactory; +import org.apache.maven.model.building.DefaultModelBuildingRequest; +import org.apache.maven.model.building.FileModelSource; +import org.apache.maven.model.building.ModelBuilder; +import org.apache.maven.model.building.ModelBuildingException; +import org.apache.maven.model.building.ModelBuildingRequest; +import org.apache.maven.model.building.ModelProblem; +import org.apache.maven.model.resolution.UnresolvableModelException; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.eclipse.aether.RepositoryEvent; +import org.eclipse.aether.RepositoryEvent.EventType; +import org.eclipse.aether.RepositoryException; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.RequestTrace; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.impl.ArtifactDescriptorReader; +import org.eclipse.aether.impl.ArtifactResolver; +import org.eclipse.aether.impl.RemoteRepositoryManager; +import org.eclipse.aether.impl.RepositoryEventDispatcher; +import org.eclipse.aether.impl.VersionRangeResolver; +import org.eclipse.aether.impl.VersionResolver; +import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.repository.WorkspaceRepository; +import org.eclipse.aether.resolution.ArtifactDescriptorException; +import org.eclipse.aether.resolution.ArtifactDescriptorPolicy; +import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest; +import org.eclipse.aether.resolution.ArtifactDescriptorRequest; +import org.eclipse.aether.resolution.ArtifactDescriptorResult; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.resolution.VersionRequest; +import org.eclipse.aether.resolution.VersionResolutionException; +import org.eclipse.aether.resolution.VersionResult; +import org.eclipse.aether.spi.locator.Service; +import org.eclipse.aether.spi.locator.ServiceLocator; +import org.eclipse.aether.spi.log.Logger; +import org.eclipse.aether.spi.log.LoggerFactory; +import org.eclipse.aether.spi.log.NullLoggerFactory; +import org.eclipse.aether.transfer.ArtifactNotFoundException; + +/** + * @author Benjamin Bentmann + */ +@Named +@Component( role = ArtifactDescriptorReader.class ) +public class DefaultArtifactDescriptorReader + implements ArtifactDescriptorReader, Service +{ + + @SuppressWarnings( "unused" ) + @Requirement( role = LoggerFactory.class ) + private Logger logger = NullLoggerFactory.LOGGER; + + @Requirement + private RemoteRepositoryManager remoteRepositoryManager; + + @Requirement + private VersionResolver versionResolver; + + @Requirement + private VersionRangeResolver versionRangeResolver; + + @Requirement + private ArtifactResolver artifactResolver; + + @Requirement + private RepositoryEventDispatcher repositoryEventDispatcher; + + @Requirement + private ModelBuilder modelBuilder; + + public DefaultArtifactDescriptorReader() + { + // enable no-arg constructor + } + + @Inject + DefaultArtifactDescriptorReader( RemoteRepositoryManager remoteRepositoryManager, VersionResolver versionResolver, + ArtifactResolver artifactResolver, ModelBuilder modelBuilder, + RepositoryEventDispatcher repositoryEventDispatcher, LoggerFactory loggerFactory ) + { + setRemoteRepositoryManager( remoteRepositoryManager ); + setVersionResolver( versionResolver ); + setArtifactResolver( artifactResolver ); + setModelBuilder( modelBuilder ); + setLoggerFactory( loggerFactory ); + setRepositoryEventDispatcher( repositoryEventDispatcher ); + } + + public void initService( ServiceLocator locator ) + { + setLoggerFactory( locator.getService( LoggerFactory.class ) ); + setRemoteRepositoryManager( locator.getService( RemoteRepositoryManager.class ) ); + setVersionResolver( locator.getService( VersionResolver.class ) ); + setVersionRangeResolver( locator.getService( VersionRangeResolver.class ) ); + setArtifactResolver( locator.getService( ArtifactResolver.class ) ); + setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); + modelBuilder = locator.getService( ModelBuilder.class ); + if ( modelBuilder == null ) + { + setModelBuilder( new DefaultModelBuilderFactory().newInstance() ); + } + } + + public DefaultArtifactDescriptorReader setLoggerFactory( LoggerFactory loggerFactory ) + { + this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); + return this; + } + + void setLogger( LoggerFactory loggerFactory ) + { + // plexus support + setLoggerFactory( loggerFactory ); + } + + public DefaultArtifactDescriptorReader setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager ) + { + this.remoteRepositoryManager = Validate.notNull( remoteRepositoryManager, + "remoteRepositoryManager cannot be null" ); + return this; + } + + public DefaultArtifactDescriptorReader setVersionResolver( VersionResolver versionResolver ) + { + this.versionResolver = Validate.notNull( versionResolver, "versionResolver cannot be null" ); + return this; + } + + /** @since 3.2.2 */ + public DefaultArtifactDescriptorReader setVersionRangeResolver( VersionRangeResolver versionRangeResolver ) + { + this.versionRangeResolver = Validate.notNull( versionRangeResolver, "versionRangeResolver cannot be null" ); + return this; + } + + public DefaultArtifactDescriptorReader setArtifactResolver( ArtifactResolver artifactResolver ) + { + this.artifactResolver = Validate.notNull( artifactResolver, "artifactResolver cannot be null" ); + return this; + } + + public DefaultArtifactDescriptorReader setRepositoryEventDispatcher( + RepositoryEventDispatcher repositoryEventDispatcher ) + { + this.repositoryEventDispatcher = Validate.notNull( repositoryEventDispatcher, + "repositoryEventDispatcher cannot be null" ); + return this; + } + + public DefaultArtifactDescriptorReader setModelBuilder( ModelBuilder modelBuilder ) + { + this.modelBuilder = Validate.notNull( modelBuilder, "modelBuilder cannot be null" ); + return this; + } + + public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session, + ArtifactDescriptorRequest request ) + throws ArtifactDescriptorException + { + ArtifactDescriptorResult result = new ArtifactDescriptorResult( request ); + + Model model = loadPom( session, request, result ); + if ( model != null ) + { + Map<String, Object> config = session.getConfigProperties(); + ArtifactDescriptorReaderDelegate delegate = + (ArtifactDescriptorReaderDelegate) config.get( ArtifactDescriptorReaderDelegate.class.getName() ); + + if ( delegate == null ) + { + delegate = new ArtifactDescriptorReaderDelegate(); + } + + delegate.populateResult( session, result, model ); + } + + return result; + } + + private Model loadPom( RepositorySystemSession session, ArtifactDescriptorRequest request, + ArtifactDescriptorResult result ) + throws ArtifactDescriptorException + { + RequestTrace trace = RequestTrace.newChild( request.getTrace(), request ); + + Set<String> visited = new LinkedHashSet<>(); + for ( Artifact a = request.getArtifact();; ) + { + Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact( a ); + try + { + VersionRequest versionRequest = + new VersionRequest( a, request.getRepositories(), request.getRequestContext() ); + versionRequest.setTrace( trace ); + VersionResult versionResult = versionResolver.resolveVersion( session, versionRequest ); + + a = a.setVersion( versionResult.getVersion() ); + + versionRequest = + new VersionRequest( pomArtifact, request.getRepositories(), request.getRequestContext() ); + versionRequest.setTrace( trace ); + versionResult = versionResolver.resolveVersion( session, versionRequest ); + + pomArtifact = pomArtifact.setVersion( versionResult.getVersion() ); + } + catch ( VersionResolutionException e ) + { + result.addException( e ); + throw new ArtifactDescriptorException( result ); + } + + if ( !visited.add( a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion() ) ) + { + RepositoryException exception = + new RepositoryException( "Artifact relocations form a cycle: " + visited ); + invalidDescriptor( session, trace, a, exception ); + if ( ( getPolicy( session, a, request ) & ArtifactDescriptorPolicy.IGNORE_INVALID ) != 0 ) + { + return null; + } + result.addException( exception ); + throw new ArtifactDescriptorException( result ); + } + + ArtifactResult resolveResult; + try + { + ArtifactRequest resolveRequest = + new ArtifactRequest( pomArtifact, request.getRepositories(), request.getRequestContext() ); + resolveRequest.setTrace( trace ); + resolveResult = artifactResolver.resolveArtifact( session, resolveRequest ); + pomArtifact = resolveResult.getArtifact(); + result.setRepository( resolveResult.getRepository() ); + } + catch ( ArtifactResolutionException e ) + { + if ( e.getCause() instanceof ArtifactNotFoundException ) + { + missingDescriptor( session, trace, a, (Exception) e.getCause() ); + if ( ( getPolicy( session, a, request ) & ArtifactDescriptorPolicy.IGNORE_MISSING ) != 0 ) + { + return null; + } + } + result.addException( e ); + throw new ArtifactDescriptorException( result ); + } + + Model model; + + // hack: don't rebuild model if it was already loaded during reactor resolution + final WorkspaceReader workspace = session.getWorkspaceReader(); + if ( workspace instanceof MavenWorkspaceReader ) + { + model = ( (MavenWorkspaceReader) workspace ).findModel( pomArtifact ); + if ( model != null ) + { + return model; + } + } + + try + { + ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest(); + modelRequest.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); + modelRequest.setProcessPlugins( false ); + modelRequest.setTwoPhaseBuilding( false ); + modelRequest.setSystemProperties( toProperties( session.getUserProperties(), + session.getSystemProperties() ) ); + modelRequest.setModelCache( DefaultModelCache.newInstance( session ) ); + modelRequest.setModelResolver( new DefaultModelResolver( session, trace.newChild( modelRequest ), + request.getRequestContext(), artifactResolver, + versionRangeResolver, remoteRepositoryManager, + request.getRepositories() ) ); + if ( resolveResult.getRepository() instanceof WorkspaceRepository ) + { + modelRequest.setPomFile( pomArtifact.getFile() ); + } + else + { + modelRequest.setModelSource( new FileModelSource( pomArtifact.getFile() ) ); + } + + model = modelBuilder.build( modelRequest ).getEffectiveModel(); + } + catch ( ModelBuildingException e ) + { + for ( ModelProblem problem : e.getProblems() ) + { + if ( problem.getException() instanceof UnresolvableModelException ) + { + result.addException( problem.getException() ); + throw new ArtifactDescriptorException( result ); + } + } + invalidDescriptor( session, trace, a, e ); + if ( ( getPolicy( session, a, request ) & ArtifactDescriptorPolicy.IGNORE_INVALID ) != 0 ) + { + return null; + } + result.addException( e ); + throw new ArtifactDescriptorException( result ); + } + + Relocation relocation = getRelocation( model ); + + if ( relocation != null ) + { + result.addRelocation( a ); + a = + new RelocatedArtifact( a, relocation.getGroupId(), relocation.getArtifactId(), + relocation.getVersion() ); + result.setArtifact( a ); + } + else + { + return model; + } + } + } + + private Properties toProperties( Map<String, String> dominant, Map<String, String> recessive ) + { + Properties props = new Properties(); + if ( recessive != null ) + { + props.putAll( recessive ); + } + if ( dominant != null ) + { + props.putAll( dominant ); + } + return props; + } + + private Relocation getRelocation( Model model ) + { + Relocation relocation = null; + DistributionManagement distMngt = model.getDistributionManagement(); + if ( distMngt != null ) + { + relocation = distMngt.getRelocation(); + } + return relocation; + } + + private void missingDescriptor( RepositorySystemSession session, RequestTrace trace, Artifact artifact, + Exception exception ) + { + RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_DESCRIPTOR_MISSING ); + event.setTrace( trace ); + event.setArtifact( artifact ); + event.setException( exception ); + + repositoryEventDispatcher.dispatch( event.build() ); + } + + private void invalidDescriptor( RepositorySystemSession session, RequestTrace trace, Artifact artifact, + Exception exception ) + { + RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_DESCRIPTOR_INVALID ); + event.setTrace( trace ); + event.setArtifact( artifact ); + event.setException( exception ); + + repositoryEventDispatcher.dispatch( event.build() ); + } + + private int getPolicy( RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request ) + { + ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy(); + if ( policy == null ) + { + return ArtifactDescriptorPolicy.STRICT; + } + return policy.getPolicy( session, new ArtifactDescriptorPolicyRequest( a, request.getRequestContext() ) ); + } + +}
http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCache.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCache.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCache.java new file mode 100644 index 0000000..f13495d --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelCache.java @@ -0,0 +1,119 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.model.building.ModelCache; +import org.eclipse.aether.RepositoryCache; +import org.eclipse.aether.RepositorySystemSession; + +/** + * A model builder cache backed by the repository system cache. + * + * @author Benjamin Bentmann + */ +class DefaultModelCache + implements ModelCache +{ + + private final RepositorySystemSession session; + + private final RepositoryCache cache; + + public static ModelCache newInstance( RepositorySystemSession session ) + { + if ( session.getCache() == null ) + { + return null; + } + else + { + return new DefaultModelCache( session ); + } + } + + private DefaultModelCache( RepositorySystemSession session ) + { + this.session = session; + this.cache = session.getCache(); + } + + public Object get( String groupId, String artifactId, String version, String tag ) + { + return cache.get( session, new Key( groupId, artifactId, version, tag ) ); + } + + public void put( String groupId, String artifactId, String version, String tag, Object data ) + { + cache.put( session, new Key( groupId, artifactId, version, tag ), data ); + } + + static class Key + { + + private final String groupId; + + private final String artifactId; + + private final String version; + + private final String tag; + + private final int hash; + + public Key( String groupId, String artifactId, String version, String tag ) + { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + this.tag = tag; + + int h = 17; + h = h * 31 + this.groupId.hashCode(); + h = h * 31 + this.artifactId.hashCode(); + h = h * 31 + this.version.hashCode(); + h = h * 31 + this.tag.hashCode(); + hash = h; + } + + @Override + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( null == obj || !getClass().equals( obj.getClass() ) ) + { + return false; + } + + Key that = (Key) obj; + return artifactId.equals( that.artifactId ) && groupId.equals( that.groupId ) + && version.equals( that.version ) && tag.equals( that.tag ); + } + + @Override + public int hashCode() + { + return hash; + } + + } +} http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java new file mode 100644 index 0000000..f344959 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java @@ -0,0 +1,229 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import org.apache.maven.model.Parent; +import org.apache.maven.model.Repository; +import org.apache.maven.model.building.FileModelSource; +import org.apache.maven.model.building.ModelSource; +import org.apache.maven.model.resolution.InvalidRepositoryException; +import org.apache.maven.model.resolution.ModelResolver; +import org.apache.maven.model.resolution.UnresolvableModelException; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.RequestTrace; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.impl.ArtifactResolver; +import org.eclipse.aether.impl.RemoteRepositoryManager; +import org.eclipse.aether.impl.VersionRangeResolver; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.VersionRangeRequest; +import org.eclipse.aether.resolution.VersionRangeResolutionException; +import org.eclipse.aether.resolution.VersionRangeResult; + +/** + * A model resolver to assist building of dependency POMs. This resolver gives priority to those repositories that have + * been initially specified and repositories discovered in dependency POMs are recessively merged into the search chain. + * + * @author Benjamin Bentmann + * @see DefaultArtifactDescriptorReader + */ +class DefaultModelResolver + implements ModelResolver +{ + + private final RepositorySystemSession session; + + private final RequestTrace trace; + + private final String context; + + private List<RemoteRepository> repositories; + + private final List<RemoteRepository> externalRepositories; + + private final ArtifactResolver resolver; + + private final VersionRangeResolver versionRangeResolver; + + private final RemoteRepositoryManager remoteRepositoryManager; + + private final Set<String> repositoryIds; + + public DefaultModelResolver( RepositorySystemSession session, RequestTrace trace, String context, + ArtifactResolver resolver, VersionRangeResolver versionRangeResolver, + RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories ) + { + this.session = session; + this.trace = trace; + this.context = context; + this.resolver = resolver; + this.versionRangeResolver = versionRangeResolver; + this.remoteRepositoryManager = remoteRepositoryManager; + this.repositories = repositories; + List<RemoteRepository> externalRepositories = new ArrayList<>(); + externalRepositories.addAll( repositories ); + this.externalRepositories = Collections.unmodifiableList( externalRepositories ); + + this.repositoryIds = new HashSet<>(); + } + + private DefaultModelResolver( DefaultModelResolver original ) + { + this.session = original.session; + this.trace = original.trace; + this.context = original.context; + this.resolver = original.resolver; + this.versionRangeResolver = original.versionRangeResolver; + this.remoteRepositoryManager = original.remoteRepositoryManager; + this.repositories = new ArrayList<>( original.repositories ); + this.externalRepositories = original.externalRepositories; + this.repositoryIds = new HashSet<>( original.repositoryIds ); + } + + @Override + public void addRepository( Repository repository ) + throws InvalidRepositoryException + { + addRepository( repository, false ); + } + + @Override + public void addRepository( final Repository repository, boolean replace ) + throws InvalidRepositoryException + { + if ( session.isIgnoreArtifactDescriptorRepositories() ) + { + return; + } + + if ( !repositoryIds.add( repository.getId() ) ) + { + if ( !replace ) + { + return; + } + + removeMatchingRepository( repositories, repository.getId() ); + } + + List<RemoteRepository> newRepositories = + Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) ); + + this.repositories = + remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true ); + } + + private static void removeMatchingRepository( Iterable<RemoteRepository> repositories, final String id ) + { + Iterables.removeIf( repositories, new Predicate<RemoteRepository>() + { + @Override + public boolean apply( RemoteRepository remoteRepository ) + { + return remoteRepository.getId().equals( id ); + } + } ); + } + + @Override + public ModelResolver newCopy() + { + return new DefaultModelResolver( this ); + } + + @Override + public ModelSource resolveModel( String groupId, String artifactId, String version ) + throws UnresolvableModelException + { + Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version ); + + try + { + ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context ); + request.setTrace( trace ); + pomArtifact = resolver.resolveArtifact( session, request ).getArtifact(); + } + catch ( ArtifactResolutionException e ) + { + throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e ); + } + + File pomFile = pomArtifact.getFile(); + + return new FileModelSource( pomFile ); + } + + public ModelSource resolveModel( Parent parent ) + throws UnresolvableModelException + { + Artifact artifact = new DefaultArtifact( parent.getGroupId(), parent.getArtifactId(), "", "pom", + parent.getVersion() ); + + VersionRangeRequest versionRangeRequest = new VersionRangeRequest( artifact, repositories, context ); + versionRangeRequest.setTrace( trace ); + + try + { + VersionRangeResult versionRangeResult = + versionRangeResolver.resolveVersionRange( session, versionRangeRequest ); + + if ( versionRangeResult.getHighestVersion() == null ) + { + throw new UnresolvableModelException( "No versions matched the requested range '" + parent.getVersion() + + "'", parent.getGroupId(), parent.getArtifactId(), + parent.getVersion() ); + + } + + if ( versionRangeResult.getVersionConstraint() != null + && versionRangeResult.getVersionConstraint().getRange() != null + && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null ) + { + throw new UnresolvableModelException( "The requested version range '" + parent.getVersion() + + "' does not specify an upper bound", parent.getGroupId(), + parent.getArtifactId(), parent.getVersion() ); + + } + + parent.setVersion( versionRangeResult.getHighestVersion().toString() ); + } + catch ( VersionRangeResolutionException e ) + { + throw new UnresolvableModelException( e.getMessage(), parent.getGroupId(), parent.getArtifactId(), + parent.getVersion(), e ); + + } + + return resolveModel( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); + } + +} http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java new file mode 100644 index 0000000..ddd3e97 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -0,0 +1,307 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang3.Validate; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.util.IOUtil; +import org.eclipse.aether.RepositoryEvent; +import org.eclipse.aether.RepositoryEvent.EventType; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.RequestTrace; +import org.eclipse.aether.SyncContext; +import org.eclipse.aether.impl.MetadataResolver; +import org.eclipse.aether.impl.RepositoryEventDispatcher; +import org.eclipse.aether.impl.SyncContextFactory; +import org.eclipse.aether.impl.VersionRangeResolver; +import org.eclipse.aether.metadata.DefaultMetadata; +import org.eclipse.aether.metadata.Metadata; +import org.eclipse.aether.repository.ArtifactRepository; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.resolution.MetadataRequest; +import org.eclipse.aether.resolution.MetadataResult; +import org.eclipse.aether.resolution.VersionRangeRequest; +import org.eclipse.aether.resolution.VersionRangeResolutionException; +import org.eclipse.aether.resolution.VersionRangeResult; +import org.eclipse.aether.spi.locator.Service; +import org.eclipse.aether.spi.locator.ServiceLocator; +import org.eclipse.aether.spi.log.Logger; +import org.eclipse.aether.spi.log.LoggerFactory; +import org.eclipse.aether.spi.log.NullLoggerFactory; +import org.eclipse.aether.util.version.GenericVersionScheme; +import org.eclipse.aether.version.InvalidVersionSpecificationException; +import org.eclipse.aether.version.Version; +import org.eclipse.aether.version.VersionConstraint; +import org.eclipse.aether.version.VersionScheme; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.FileInputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Benjamin Bentmann + */ +@Named +@Component( role = VersionRangeResolver.class ) +public class DefaultVersionRangeResolver + implements VersionRangeResolver, Service +{ + + private static final String MAVEN_METADATA_XML = "maven-metadata.xml"; + + @SuppressWarnings( "unused" ) + @Requirement( role = LoggerFactory.class ) + private Logger logger = NullLoggerFactory.LOGGER; + + @Requirement + private MetadataResolver metadataResolver; + + @Requirement + private SyncContextFactory syncContextFactory; + + @Requirement + private RepositoryEventDispatcher repositoryEventDispatcher; + + public DefaultVersionRangeResolver() + { + // enable default constructor + } + + @Inject + DefaultVersionRangeResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, + RepositoryEventDispatcher repositoryEventDispatcher, LoggerFactory loggerFactory ) + { + setMetadataResolver( metadataResolver ); + setSyncContextFactory( syncContextFactory ); + setLoggerFactory( loggerFactory ); + setRepositoryEventDispatcher( repositoryEventDispatcher ); + } + + public void initService( ServiceLocator locator ) + { + setLoggerFactory( locator.getService( LoggerFactory.class ) ); + setMetadataResolver( locator.getService( MetadataResolver.class ) ); + setSyncContextFactory( locator.getService( SyncContextFactory.class ) ); + setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); + } + + public DefaultVersionRangeResolver setLoggerFactory( LoggerFactory loggerFactory ) + { + this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); + return this; + } + + void setLogger( LoggerFactory loggerFactory ) + { + // plexus support + setLoggerFactory( loggerFactory ); + } + + public DefaultVersionRangeResolver setMetadataResolver( MetadataResolver metadataResolver ) + { + this.metadataResolver = Validate.notNull( metadataResolver, "metadataResolver cannot be null" ); + return this; + } + + public DefaultVersionRangeResolver setSyncContextFactory( SyncContextFactory syncContextFactory ) + { + this.syncContextFactory = Validate.notNull( syncContextFactory, "syncContextFactory cannot be null" ); + return this; + } + + public DefaultVersionRangeResolver setRepositoryEventDispatcher( + RepositoryEventDispatcher repositoryEventDispatcher ) + { + this.repositoryEventDispatcher = Validate.notNull( repositoryEventDispatcher, + "repositoryEventDispatcher cannot be null" ); + return this; + } + + public VersionRangeResult resolveVersionRange( RepositorySystemSession session, VersionRangeRequest request ) + throws VersionRangeResolutionException + { + VersionRangeResult result = new VersionRangeResult( request ); + + VersionScheme versionScheme = new GenericVersionScheme(); + + VersionConstraint versionConstraint; + try + { + versionConstraint = versionScheme.parseVersionConstraint( request.getArtifact().getVersion() ); + } + catch ( InvalidVersionSpecificationException e ) + { + result.addException( e ); + throw new VersionRangeResolutionException( result ); + } + + result.setVersionConstraint( versionConstraint ); + + if ( versionConstraint.getRange() == null ) + { + result.addVersion( versionConstraint.getVersion() ); + } + else + { + Map<String, ArtifactRepository> versionIndex = getVersions( session, result, request ); + + List<Version> versions = new ArrayList<>(); + for ( Map.Entry<String, ArtifactRepository> v : versionIndex.entrySet() ) + { + try + { + Version ver = versionScheme.parseVersion( v.getKey() ); + if ( versionConstraint.containsVersion( ver ) ) + { + versions.add( ver ); + result.setRepository( ver, v.getValue() ); + } + } + catch ( InvalidVersionSpecificationException e ) + { + result.addException( e ); + } + } + + Collections.sort( versions ); + result.setVersions( versions ); + } + + return result; + } + + private Map<String, ArtifactRepository> getVersions( RepositorySystemSession session, VersionRangeResult result, + VersionRangeRequest request ) + { + RequestTrace trace = RequestTrace.newChild( request.getTrace(), request ); + + Map<String, ArtifactRepository> versionIndex = new HashMap<>(); + + Metadata metadata = + new DefaultMetadata( request.getArtifact().getGroupId(), request.getArtifact().getArtifactId(), + MAVEN_METADATA_XML, Metadata.Nature.RELEASE_OR_SNAPSHOT ); + + List<MetadataRequest> metadataRequests = new ArrayList<>( request.getRepositories().size() ); + + metadataRequests.add( new MetadataRequest( metadata, null, request.getRequestContext() ) ); + + for ( RemoteRepository repository : request.getRepositories() ) + { + MetadataRequest metadataRequest = new MetadataRequest( metadata, repository, request.getRequestContext() ); + metadataRequest.setDeleteLocalCopyIfMissing( true ); + metadataRequest.setTrace( trace ); + metadataRequests.add( metadataRequest ); + } + + List<MetadataResult> metadataResults = metadataResolver.resolveMetadata( session, metadataRequests ); + + WorkspaceReader workspace = session.getWorkspaceReader(); + if ( workspace != null ) + { + List<String> versions = workspace.findVersions( request.getArtifact() ); + for ( String version : versions ) + { + versionIndex.put( version, workspace.getRepository() ); + } + } + + for ( MetadataResult metadataResult : metadataResults ) + { + result.addException( metadataResult.getException() ); + + ArtifactRepository repository = metadataResult.getRequest().getRepository(); + if ( repository == null ) + { + repository = session.getLocalRepository(); + } + + Versioning versioning = readVersions( session, trace, metadataResult.getMetadata(), repository, result ); + for ( String version : versioning.getVersions() ) + { + if ( !versionIndex.containsKey( version ) ) + { + versionIndex.put( version, repository ); + } + } + } + + return versionIndex; + } + + private Versioning readVersions( RepositorySystemSession session, RequestTrace trace, Metadata metadata, + ArtifactRepository repository, VersionRangeResult result ) + { + Versioning versioning = null; + + FileInputStream fis = null; + try + { + if ( metadata != null ) + { + + try ( SyncContext syncContext = syncContextFactory.newInstance( session, true ) ) + { + syncContext.acquire( null, Collections.singleton( metadata ) ); + + if ( metadata.getFile() != null && metadata.getFile().exists() ) + { + fis = new FileInputStream( metadata.getFile() ); + org.apache.maven.artifact.repository.metadata.Metadata m = + new MetadataXpp3Reader().read( fis, false ); + versioning = m.getVersioning(); + } + } + } + } + catch ( Exception e ) + { + invalidMetadata( session, trace, metadata, repository, e ); + result.addException( e ); + } + finally + { + IOUtil.close( fis ); + } + + return ( versioning != null ) ? versioning : new Versioning(); + } + + private void invalidMetadata( RepositorySystemSession session, RequestTrace trace, Metadata metadata, + ArtifactRepository repository, Exception exception ) + { + RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_INVALID ); + event.setTrace( trace ); + event.setMetadata( metadata ); + event.setException( exception ); + event.setRepository( repository ); + + repositoryEventDispatcher.dispatch( event.build() ); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java new file mode 100644 index 0000000..2b0890b --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java @@ -0,0 +1,617 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.lang3.Validate; +import org.apache.maven.artifact.repository.metadata.Snapshot; +import org.apache.maven.artifact.repository.metadata.SnapshotVersion; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; +import org.eclipse.aether.RepositoryCache; +import org.eclipse.aether.RepositoryEvent; +import org.eclipse.aether.RepositoryEvent.EventType; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.RequestTrace; +import org.eclipse.aether.SyncContext; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.impl.MetadataResolver; +import org.eclipse.aether.impl.RepositoryEventDispatcher; +import org.eclipse.aether.impl.SyncContextFactory; +import org.eclipse.aether.impl.VersionResolver; +import org.eclipse.aether.internal.impl.CacheUtils; +import org.eclipse.aether.metadata.DefaultMetadata; +import org.eclipse.aether.metadata.Metadata; +import org.eclipse.aether.repository.ArtifactRepository; +import org.eclipse.aether.repository.LocalRepository; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.repository.WorkspaceReader; +import org.eclipse.aether.repository.WorkspaceRepository; +import org.eclipse.aether.resolution.MetadataRequest; +import org.eclipse.aether.resolution.MetadataResult; +import org.eclipse.aether.resolution.VersionRequest; +import org.eclipse.aether.resolution.VersionResolutionException; +import org.eclipse.aether.resolution.VersionResult; +import org.eclipse.aether.spi.locator.Service; +import org.eclipse.aether.spi.locator.ServiceLocator; +import org.eclipse.aether.spi.log.Logger; +import org.eclipse.aether.spi.log.LoggerFactory; +import org.eclipse.aether.spi.log.NullLoggerFactory; +import org.eclipse.aether.util.ConfigUtils; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Benjamin Bentmann + */ +@Named +@Component( role = VersionResolver.class ) +public class DefaultVersionResolver + implements VersionResolver, Service +{ + + private static final String MAVEN_METADATA_XML = "maven-metadata.xml"; + + private static final String RELEASE = "RELEASE"; + + private static final String LATEST = "LATEST"; + + private static final String SNAPSHOT = "SNAPSHOT"; + + @SuppressWarnings( "unused" ) + @Requirement( role = LoggerFactory.class ) + private Logger logger = NullLoggerFactory.LOGGER; + + @Requirement + private MetadataResolver metadataResolver; + + @Requirement + private SyncContextFactory syncContextFactory; + + @Requirement + private RepositoryEventDispatcher repositoryEventDispatcher; + + public DefaultVersionResolver() + { + // enable no-arg constructor + } + + @Inject + DefaultVersionResolver( MetadataResolver metadataResolver, SyncContextFactory syncContextFactory, + RepositoryEventDispatcher repositoryEventDispatcher, LoggerFactory loggerFactory ) + { + setMetadataResolver( metadataResolver ); + setSyncContextFactory( syncContextFactory ); + setLoggerFactory( loggerFactory ); + setRepositoryEventDispatcher( repositoryEventDispatcher ); + } + + public void initService( ServiceLocator locator ) + { + setLoggerFactory( locator.getService( LoggerFactory.class ) ); + setMetadataResolver( locator.getService( MetadataResolver.class ) ); + setSyncContextFactory( locator.getService( SyncContextFactory.class ) ); + setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) ); + } + + public DefaultVersionResolver setLoggerFactory( LoggerFactory loggerFactory ) + { + this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); + return this; + } + + void setLogger( LoggerFactory loggerFactory ) + { + // plexus support + setLoggerFactory( loggerFactory ); + } + + public DefaultVersionResolver setMetadataResolver( MetadataResolver metadataResolver ) + { + this.metadataResolver = Validate.notNull( metadataResolver, "metadataResolver cannot be null" ); + return this; + } + + public DefaultVersionResolver setSyncContextFactory( SyncContextFactory syncContextFactory ) + { + this.syncContextFactory = Validate.notNull( syncContextFactory, "syncContextFactory cannot be null" ); + return this; + } + + public DefaultVersionResolver setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher ) + { + this.repositoryEventDispatcher = Validate.notNull( repositoryEventDispatcher, + "repositoryEventDispatcher cannot be null" ); + return this; + } + + public VersionResult resolveVersion( RepositorySystemSession session, VersionRequest request ) + throws VersionResolutionException + { + RequestTrace trace = RequestTrace.newChild( request.getTrace(), request ); + + Artifact artifact = request.getArtifact(); + + String version = artifact.getVersion(); + + VersionResult result = new VersionResult( request ); + + Key cacheKey = null; + RepositoryCache cache = session.getCache(); + if ( cache != null && !ConfigUtils.getBoolean( session, false, "aether.versionResolver.noCache" ) ) + { + cacheKey = new Key( session, request ); + + Object obj = cache.get( session, cacheKey ); + if ( obj instanceof Record ) + { + Record record = (Record) obj; + result.setVersion( record.version ); + result.setRepository( + CacheUtils.getRepository( session, request.getRepositories(), record.repoClass, record.repoId ) ); + return result; + } + } + + Metadata metadata; + + if ( RELEASE.equals( version ) ) + { + metadata = new DefaultMetadata( artifact.getGroupId(), artifact.getArtifactId(), MAVEN_METADATA_XML, + Metadata.Nature.RELEASE ); + } + else if ( LATEST.equals( version ) ) + { + metadata = new DefaultMetadata( artifact.getGroupId(), artifact.getArtifactId(), MAVEN_METADATA_XML, + Metadata.Nature.RELEASE_OR_SNAPSHOT ); + } + else if ( version.endsWith( SNAPSHOT ) ) + { + WorkspaceReader workspace = session.getWorkspaceReader(); + if ( workspace != null && workspace.findVersions( artifact ).contains( version ) ) + { + metadata = null; + result.setRepository( workspace.getRepository() ); + } + else + { + metadata = + new DefaultMetadata( artifact.getGroupId(), artifact.getArtifactId(), version, MAVEN_METADATA_XML, + Metadata.Nature.SNAPSHOT ); + } + } + else + { + metadata = null; + } + + if ( metadata == null ) + { + result.setVersion( version ); + } + else + { + List<MetadataRequest> metadataReqs = new ArrayList<>( request.getRepositories().size() ); + + metadataReqs.add( new MetadataRequest( metadata, null, request.getRequestContext() ) ); + + for ( RemoteRepository repository : request.getRepositories() ) + { + MetadataRequest metadataRequest = + new MetadataRequest( metadata, repository, request.getRequestContext() ); + metadataRequest.setDeleteLocalCopyIfMissing( true ); + metadataRequest.setFavorLocalRepository( true ); + metadataRequest.setTrace( trace ); + metadataReqs.add( metadataRequest ); + } + + List<MetadataResult> metadataResults = metadataResolver.resolveMetadata( session, metadataReqs ); + + Map<String, VersionInfo> infos = new HashMap<>(); + + for ( MetadataResult metadataResult : metadataResults ) + { + result.addException( metadataResult.getException() ); + + ArtifactRepository repository = metadataResult.getRequest().getRepository(); + if ( repository == null ) + { + repository = session.getLocalRepository(); + } + + Versioning v = readVersions( session, trace, metadataResult.getMetadata(), repository, result ); + merge( artifact, infos, v, repository ); + } + + if ( RELEASE.equals( version ) ) + { + resolve( result, infos, RELEASE ); + } + else if ( LATEST.equals( version ) ) + { + if ( !resolve( result, infos, LATEST ) ) + { + resolve( result, infos, RELEASE ); + } + + if ( result.getVersion() != null && result.getVersion().endsWith( SNAPSHOT ) ) + { + VersionRequest subRequest = new VersionRequest(); + subRequest.setArtifact( artifact.setVersion( result.getVersion() ) ); + if ( result.getRepository() instanceof RemoteRepository ) + { + RemoteRepository r = (RemoteRepository) result.getRepository(); + subRequest.setRepositories( Collections.singletonList( r ) ); + } + else + { + subRequest.setRepositories( request.getRepositories() ); + } + VersionResult subResult = resolveVersion( session, subRequest ); + result.setVersion( subResult.getVersion() ); + result.setRepository( subResult.getRepository() ); + for ( Exception exception : subResult.getExceptions() ) + { + result.addException( exception ); + } + } + } + else + { + String key = SNAPSHOT + getKey( artifact.getClassifier(), artifact.getExtension() ); + merge( infos, SNAPSHOT, key ); + if ( !resolve( result, infos, key ) ) + { + result.setVersion( version ); + } + } + + if ( StringUtils.isEmpty( result.getVersion() ) ) + { + throw new VersionResolutionException( result ); + } + } + + if ( cacheKey != null && metadata != null && isSafelyCacheable( session, artifact ) ) + { + cache.put( session, cacheKey, new Record( result.getVersion(), result.getRepository() ) ); + } + + return result; + } + + private boolean resolve( VersionResult result, Map<String, VersionInfo> infos, String key ) + { + VersionInfo info = infos.get( key ); + if ( info != null ) + { + result.setVersion( info.version ); + result.setRepository( info.repository ); + } + return info != null; + } + + private Versioning readVersions( RepositorySystemSession session, RequestTrace trace, Metadata metadata, + ArtifactRepository repository, VersionResult result ) + { + Versioning versioning = null; + + FileInputStream fis = null; + try + { + if ( metadata != null ) + { + + try ( SyncContext syncContext = syncContextFactory.newInstance( session, true ) ) + { + syncContext.acquire( null, Collections.singleton( metadata ) ); + + if ( metadata.getFile() != null && metadata.getFile().exists() ) + { + fis = new FileInputStream( metadata.getFile() ); + org.apache.maven.artifact.repository.metadata.Metadata m = + new MetadataXpp3Reader().read( fis, false ); + versioning = m.getVersioning(); + + /* + * NOTE: Users occasionally misuse the id "local" for remote repos which screws up the metadata + * of the local repository. This is especially troublesome during snapshot resolution so we try + * to handle that gracefully. + */ + if ( versioning != null && repository instanceof LocalRepository ) + { + if ( versioning.getSnapshot() != null && versioning.getSnapshot().getBuildNumber() > 0 ) + { + Versioning repaired = new Versioning(); + repaired.setLastUpdated( versioning.getLastUpdated() ); + Snapshot snapshot = new Snapshot(); + snapshot.setLocalCopy( true ); + repaired.setSnapshot( snapshot ); + versioning = repaired; + + throw new IOException( "Snapshot information corrupted with remote repository data" + + ", please verify that no remote repository uses the id '" + + repository.getId() + "'" ); + } + } + } + } + } + } + catch ( Exception e ) + { + invalidMetadata( session, trace, metadata, repository, e ); + result.addException( e ); + } + finally + { + IOUtil.close( fis ); + } + + return ( versioning != null ) ? versioning : new Versioning(); + } + + private void invalidMetadata( RepositorySystemSession session, RequestTrace trace, Metadata metadata, + ArtifactRepository repository, Exception exception ) + { + RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.METADATA_INVALID ); + event.setTrace( trace ); + event.setMetadata( metadata ); + event.setException( exception ); + event.setRepository( repository ); + + repositoryEventDispatcher.dispatch( event.build() ); + } + + private void merge( Artifact artifact, Map<String, VersionInfo> infos, Versioning versioning, + ArtifactRepository repository ) + { + if ( StringUtils.isNotEmpty( versioning.getRelease() ) ) + { + merge( RELEASE, infos, versioning.getLastUpdated(), versioning.getRelease(), repository ); + } + + if ( StringUtils.isNotEmpty( versioning.getLatest() ) ) + { + merge( LATEST, infos, versioning.getLastUpdated(), versioning.getLatest(), repository ); + } + + for ( SnapshotVersion sv : versioning.getSnapshotVersions() ) + { + if ( StringUtils.isNotEmpty( sv.getVersion() ) ) + { + String key = getKey( sv.getClassifier(), sv.getExtension() ); + merge( SNAPSHOT + key, infos, sv.getUpdated(), sv.getVersion(), repository ); + } + } + + Snapshot snapshot = versioning.getSnapshot(); + if ( snapshot != null && versioning.getSnapshotVersions().isEmpty() ) + { + String version = artifact.getVersion(); + if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 ) + { + String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber(); + version = version.substring( 0, version.length() - SNAPSHOT.length() ) + qualifier; + } + merge( SNAPSHOT, infos, versioning.getLastUpdated(), version, repository ); + } + } + + private void merge( String key, Map<String, VersionInfo> infos, String timestamp, String version, + ArtifactRepository repository ) + { + VersionInfo info = infos.get( key ); + if ( info == null ) + { + info = new VersionInfo( timestamp, version, repository ); + infos.put( key, info ); + } + else if ( info.isOutdated( timestamp ) ) + { + info.version = version; + info.repository = repository; + info.timestamp = timestamp; + } + } + + private void merge( Map<String, VersionInfo> infos, String srcKey, String dstKey ) + { + VersionInfo srcInfo = infos.get( srcKey ); + VersionInfo dstInfo = infos.get( dstKey ); + + if ( dstInfo == null || ( srcInfo != null && dstInfo.isOutdated( srcInfo.timestamp ) + && srcInfo.repository != dstInfo.repository ) ) + { + infos.put( dstKey, srcInfo ); + } + } + + private String getKey( String classifier, String extension ) + { + return StringUtils.clean( classifier ) + ':' + StringUtils.clean( extension ); + } + + private boolean isSafelyCacheable( RepositorySystemSession session, Artifact artifact ) + { + /* + * The workspace/reactor is in flux so we better not assume definitive information for any of its + * artifacts/projects. + */ + + WorkspaceReader workspace = session.getWorkspaceReader(); + if ( workspace == null ) + { + return true; + } + + Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact( artifact ); + + return workspace.findArtifact( pomArtifact ) == null; + } + + private static class VersionInfo + { + + String timestamp; + + String version; + + ArtifactRepository repository; + + public VersionInfo( String timestamp, String version, ArtifactRepository repository ) + { + this.timestamp = ( timestamp != null ) ? timestamp : ""; + this.version = version; + this.repository = repository; + } + + public boolean isOutdated( String timestamp ) + { + return timestamp != null && timestamp.compareTo( this.timestamp ) > 0; + } + + } + + private static class Key + { + + private final String groupId; + + private final String artifactId; + + private final String classifier; + + private final String extension; + + private final String version; + + private final String context; + + private final File localRepo; + + private final WorkspaceRepository workspace; + + private final List<RemoteRepository> repositories; + + private final int hashCode; + + public Key( RepositorySystemSession session, VersionRequest request ) + { + Artifact artifact = request.getArtifact(); + groupId = artifact.getGroupId(); + artifactId = artifact.getArtifactId(); + classifier = artifact.getClassifier(); + extension = artifact.getExtension(); + version = artifact.getVersion(); + localRepo = session.getLocalRepository().getBasedir(); + workspace = CacheUtils.getWorkspace( session ); + repositories = new ArrayList<>( request.getRepositories().size() ); + boolean repoMan = false; + for ( RemoteRepository repository : request.getRepositories() ) + { + if ( repository.isRepositoryManager() ) + { + repoMan = true; + repositories.addAll( repository.getMirroredRepositories() ); + } + else + { + repositories.add( repository ); + } + } + context = repoMan ? request.getRequestContext() : ""; + + int hash = 17; + hash = hash * 31 + groupId.hashCode(); + hash = hash * 31 + artifactId.hashCode(); + hash = hash * 31 + classifier.hashCode(); + hash = hash * 31 + extension.hashCode(); + hash = hash * 31 + version.hashCode(); + hash = hash * 31 + localRepo.hashCode(); + hash = hash * 31 + CacheUtils.repositoriesHashCode( repositories ); + hashCode = hash; + } + + @Override + public boolean equals( Object obj ) + { + if ( obj == this ) + { + return true; + } + else if ( obj == null || !getClass().equals( obj.getClass() ) ) + { + return false; + } + + Key that = (Key) obj; + return artifactId.equals( that.artifactId ) && groupId.equals( that.groupId ) && classifier.equals( + that.classifier ) && extension.equals( that.extension ) && version.equals( that.version ) + && context.equals( that.context ) && localRepo.equals( that.localRepo ) + && CacheUtils.eq( workspace, that.workspace ) + && CacheUtils.repositoriesEquals( repositories, that.repositories ); + } + + @Override + public int hashCode() + { + return hashCode; + } + + } + + private static class Record + { + final String version; + + final String repoId; + + final Class<?> repoClass; + + public Record( String version, ArtifactRepository repository ) + { + this.version = version; + if ( repository != null ) + { + repoId = repository.getId(); + repoClass = repository.getClass(); + } + else + { + repoId = null; + repoClass = null; + } + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java new file mode 100644 index 0000000..a572010 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java @@ -0,0 +1,163 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.Snapshot; +import org.apache.maven.artifact.repository.metadata.SnapshotVersion; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.eclipse.aether.artifact.Artifact; + +/** + * @author Benjamin Bentmann + */ +final class LocalSnapshotMetadata + extends MavenMetadata +{ + + private final Collection<Artifact> artifacts = new ArrayList<>(); + + private final boolean legacyFormat; + + public LocalSnapshotMetadata( Artifact artifact, boolean legacyFormat ) + { + super( createMetadata( artifact, legacyFormat ), null ); + this.legacyFormat = legacyFormat; + } + + public LocalSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat ) + { + super( metadata, file ); + this.legacyFormat = legacyFormat; + } + + private static Metadata createMetadata( Artifact artifact, boolean legacyFormat ) + { + Snapshot snapshot = new Snapshot(); + snapshot.setLocalCopy( true ); + Versioning versioning = new Versioning(); + versioning.setSnapshot( snapshot ); + + Metadata metadata = new Metadata(); + metadata.setVersioning( versioning ); + metadata.setGroupId( artifact.getGroupId() ); + metadata.setArtifactId( artifact.getArtifactId() ); + metadata.setVersion( artifact.getBaseVersion() ); + + if ( !legacyFormat ) + { + metadata.setModelVersion( "1.1.0" ); + } + + return metadata; + } + + public void bind( Artifact artifact ) + { + artifacts.add( artifact ); + } + + public MavenMetadata setFile( File file ) + { + return new LocalSnapshotMetadata( metadata, file, legacyFormat ); + } + + public Object getKey() + { + return getGroupId() + ':' + getArtifactId() + ':' + getVersion(); + } + + public static Object getKey( Artifact artifact ) + { + return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion(); + } + + @Override + protected void merge( Metadata recessive ) + { + metadata.getVersioning().updateTimestamp(); + + if ( !legacyFormat ) + { + String lastUpdated = metadata.getVersioning().getLastUpdated(); + + Map<String, SnapshotVersion> versions = new LinkedHashMap<>(); + + for ( Artifact artifact : artifacts ) + { + SnapshotVersion sv = new SnapshotVersion(); + sv.setClassifier( artifact.getClassifier() ); + sv.setExtension( artifact.getExtension() ); + sv.setVersion( getVersion() ); + sv.setUpdated( lastUpdated ); + versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv ); + } + + Versioning versioning = recessive.getVersioning(); + if ( versioning != null ) + { + for ( SnapshotVersion sv : versioning.getSnapshotVersions() ) + { + String key = getKey( sv.getClassifier(), sv.getExtension() ); + if ( !versions.containsKey( key ) ) + { + versions.put( key, sv ); + } + } + } + + metadata.getVersioning().setSnapshotVersions( new ArrayList<>( versions.values() ) ); + } + + artifacts.clear(); + } + + private String getKey( String classifier, String extension ) + { + return classifier + ':' + extension; + } + + public String getGroupId() + { + return metadata.getGroupId(); + } + + public String getArtifactId() + { + return metadata.getArtifactId(); + } + + public String getVersion() + { + return metadata.getVersion(); + } + + public Nature getNature() + { + return Nature.SNAPSHOT; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java new file mode 100644 index 0000000..072ba66 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java @@ -0,0 +1,82 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.impl.MetadataGenerator; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.metadata.Metadata; +import org.eclipse.aether.util.ConfigUtils; + +/** + * @author Benjamin Bentmann + */ +class LocalSnapshotMetadataGenerator + implements MetadataGenerator +{ + + private Map<Object, LocalSnapshotMetadata> snapshots; + + private final boolean legacyFormat; + + public LocalSnapshotMetadataGenerator( RepositorySystemSession session, InstallRequest request ) + { + legacyFormat = ConfigUtils.getBoolean( session.getConfigProperties(), false, "maven.metadata.legacy" ); + + snapshots = new LinkedHashMap<>(); + } + + public Collection<? extends Metadata> prepare( Collection<? extends Artifact> artifacts ) + { + for ( Artifact artifact : artifacts ) + { + if ( artifact.isSnapshot() ) + { + Object key = LocalSnapshotMetadata.getKey( artifact ); + LocalSnapshotMetadata snapshotMetadata = snapshots.get( key ); + if ( snapshotMetadata == null ) + { + snapshotMetadata = new LocalSnapshotMetadata( artifact, legacyFormat ); + snapshots.put( key, snapshotMetadata ); + } + snapshotMetadata.bind( artifact ); + } + } + + return Collections.emptyList(); + } + + public Artifact transformArtifact( Artifact artifact ) + { + return artifact; + } + + public Collection<? extends Metadata> finish( Collection<? extends Artifact> artifacts ) + { + return snapshots.values(); + } + +} http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenAetherModule.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenAetherModule.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenAetherModule.java new file mode 100644 index 0000000..41e98aa --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenAetherModule.java @@ -0,0 +1,80 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.model.building.DefaultModelBuilderFactory; +import org.apache.maven.model.building.ModelBuilder; +import org.eclipse.aether.impl.AetherModule; +import org.eclipse.aether.impl.ArtifactDescriptorReader; +import org.eclipse.aether.impl.MetadataGeneratorFactory; +import org.eclipse.aether.impl.VersionRangeResolver; +import org.eclipse.aether.impl.VersionResolver; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.name.Names; + +/** + * @deprecated As of Maven Resolver 1.0.3, please use class {@link MavenResolverModule}. + */ +@Deprecated +public final class MavenAetherModule + extends AbstractModule +{ + + @Override + protected void configure() + { + install( new AetherModule() ); + bind( ArtifactDescriptorReader.class ) // + .to( DefaultArtifactDescriptorReader.class ).in( Singleton.class ); + bind( VersionResolver.class ) // + .to( DefaultVersionResolver.class ).in( Singleton.class ); + bind( VersionRangeResolver.class ) // + .to( DefaultVersionRangeResolver.class ).in( Singleton.class ); + bind( MetadataGeneratorFactory.class ).annotatedWith( Names.named( "snapshot" ) ) // + .to( SnapshotMetadataGeneratorFactory.class ).in( Singleton.class ); + bind( MetadataGeneratorFactory.class ).annotatedWith( Names.named( "versions" ) ) // + .to( VersionsMetadataGeneratorFactory.class ).in( Singleton.class ); + bind( ModelBuilder.class ) // + .toInstance( new DefaultModelBuilderFactory().newInstance() ); + } + + @Provides + @Singleton + Set<MetadataGeneratorFactory> provideMetadataGeneratorFactories( @Named( "snapshot" ) + MetadataGeneratorFactory snapshot, + @Named( "versions" ) + MetadataGeneratorFactory versions ) + { + Set<MetadataGeneratorFactory> factories = new HashSet<>(); + factories.add( snapshot ); + factories.add( versions ); + return Collections.unmodifiableSet( factories ); + } + +} http://git-wip-us.apache.org/repos/asf/maven/blob/8972072e/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java ---------------------------------------------------------------------- diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java new file mode 100644 index 0000000..aef44f6 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java @@ -0,0 +1,137 @@ +package org.apache.maven.repository.internal; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer; +import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.WriterFactory; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.eclipse.aether.RepositoryException; +import org.eclipse.aether.metadata.AbstractMetadata; +import org.eclipse.aether.metadata.MergeableMetadata; + +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.util.Collections; +import java.util.Map; + +/** + * @author Benjamin Bentmann + */ +abstract class MavenMetadata + extends AbstractMetadata + implements MergeableMetadata +{ + + static final String MAVEN_METADATA_XML = "maven-metadata.xml"; + + private final File file; + + protected Metadata metadata; + + private boolean merged; + + protected MavenMetadata( Metadata metadata, File file ) + { + this.metadata = metadata; + this.file = file; + } + + public String getType() + { + return MAVEN_METADATA_XML; + } + + public File getFile() + { + return file; + } + + public void merge( File existing, File result ) + throws RepositoryException + { + Metadata recessive = read( existing ); + + merge( recessive ); + + write( result, metadata ); + + merged = true; + } + + public boolean isMerged() + { + return merged; + } + + protected abstract void merge( Metadata recessive ); + + static Metadata read( File metadataFile ) + throws RepositoryException + { + if ( metadataFile.length() <= 0 ) + { + return new Metadata(); + } + + try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) ) + { + return new MetadataXpp3Reader().read( reader, false ); + } + catch ( IOException e ) + { + throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e ); + } + catch ( XmlPullParserException e ) + { + throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e ); + } + } + + private void write( File metadataFile, Metadata metadata ) + throws RepositoryException + { + metadataFile.getParentFile().mkdirs(); + try ( Writer writer = WriterFactory.newXmlWriter( metadataFile ) ) + { + new MetadataXpp3Writer().write( writer, metadata ); + } + catch ( IOException e ) + { + throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e ); + } + } + + public Map<String, String> getProperties() + { + return Collections.emptyMap(); + } + + @Override + public org.eclipse.aether.metadata.Metadata setProperties( Map<String, String> properties ) + { + return this; + } + +}