[MNG-7544] MavenMetadataSource#retrieve(MetadataResolutionRequest) does not check for null when reading from project map

Currently, if a project is returned from the workspace reader, but is not part
of the reactor, a NPE occurs. In this case, the repositories are read from
the model instead.

This closes #805
This commit is contained in:
Christoph Läubrich 2023-02-05 08:35:15 +01:00 committed by Michael Osipov
parent d2a27a88bf
commit 72efba9b14
4 changed files with 151 additions and 7 deletions

View File

@ -39,6 +39,7 @@ import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.collection.DependencyGraphTransformer;
@ -47,6 +48,7 @@ import org.eclipse.aether.collection.DependencySelector;
import org.eclipse.aether.collection.DependencyTraverser;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
import org.eclipse.aether.util.graph.selector.AndDependencySelector;
import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
@ -313,6 +315,11 @@ public abstract class AbstractArtifactComponentTestCase extends PlexusTestCase {
LocalRepository localRepo = new LocalRepository(localRepository().getBasedir());
session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
try {
session.setWorkspaceReader(lookup(WorkspaceReader.class, "test"));
} catch (ComponentLookupException e) {
// no reader, nothing to do...
}
return session;
}

View File

@ -34,6 +34,7 @@ import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest;
import org.eclipse.aether.repository.WorkspaceReader;
// It would be cool if there was a hook that i could use to setup a test environment.
// I want to setup a local/remote repositories for testing but i don't want to have
@ -45,12 +46,17 @@ import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest;
* @author Jason van Zyl
*/
public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
private DefaultArtifactResolver artifactResolver;
private Artifact projectArtifact;
private TestMavenWorkspaceReader workspaceReader;
@Override
protected void setUp() throws Exception {
workspaceReader = new TestMavenWorkspaceReader();
getContainer().addComponent(workspaceReader, WorkspaceReader.class, "test");
super.setUp();
artifactResolver = (DefaultArtifactResolver) lookup(ArtifactResolver.class);
@ -163,12 +169,24 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
assertLocalArtifactPresent(l);
}
public void testReadRepoFromModel() throws Exception {
Artifact m = createArtifact(TestMavenWorkspaceReader.ARTIFACT_ID, TestMavenWorkspaceReader.VERSION);
ArtifactMetadataSource source = lookup(ArtifactMetadataSource.class, "maven");
ResolutionGroup group = source.retrieve(m, localRepository(), new ArrayList<ArtifactRepository>());
List<ArtifactRepository> repositories = group.getResolutionRepositories();
assertEquals("There should be one repository!", 1, repositories.size());
ArtifactRepository repository = repositories.get(0);
assertEquals(TestMavenWorkspaceReader.REPO_ID, repository.getId());
assertEquals(TestMavenWorkspaceReader.REPO_URL, repository.getUrl());
}
public void testTransitiveResolutionOrder() throws Exception {
Artifact m = createLocalArtifact("m", "1.0");
Artifact n = createLocalArtifact("n", "1.0");
ArtifactMetadataSource mds = new ArtifactMetadataSource() {
@Override
public ResolutionGroup retrieve(
Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
throws ArtifactMetadataRetrievalException {
@ -177,23 +195,27 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
return new ResolutionGroup(artifact, dependencies, remoteRepositories);
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(
Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
throws ArtifactMetadataRetrievalException {
throw new UnsupportedOperationException("Cannot get available versions in this test case");
}
@Override
public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
Artifact artifact, ArtifactRepository localRepository, ArtifactRepository remoteRepository)
throws ArtifactMetadataRetrievalException {
throw new UnsupportedOperationException("Cannot get available versions in this test case");
}
@Override
public ResolutionGroup retrieve(MetadataResolutionRequest request)
throws ArtifactMetadataRetrievalException {
return retrieve(request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories());
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(MetadataResolutionRequest request)
throws ArtifactMetadataRetrievalException {
return retrieveAvailableVersions(

View File

@ -0,0 +1,80 @@
/*
* 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.
*/
package org.apache.maven.artifact.resolver;
import java.io.File;
import java.util.Collections;
import java.util.List;
import org.apache.maven.model.Model;
import org.apache.maven.model.Repository;
import org.apache.maven.repository.internal.MavenWorkspaceReader;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.WorkspaceRepository;
public class TestMavenWorkspaceReader implements MavenWorkspaceReader {
static final String REPO_LAYOUT = "test";
static final String REPO_URL = "https://test/me";
static final String REPO_ID = "custom";
static final String GROUP_ID = "org.apache.maven";
static final String ARTIFACT_ID = "this.is.a.test";
static final String VERSION = "99.99";
private static final WorkspaceRepository WORKSPACE_REPOSITORY = new WorkspaceRepository(REPO_LAYOUT);
@Override
public WorkspaceRepository getRepository() {
return WORKSPACE_REPOSITORY;
}
@Override
public File findArtifact(Artifact artifact) {
return null;
}
@Override
public List<String> findVersions(Artifact artifact) {
return Collections.emptyList();
}
@Override
public Model findModel(Artifact artifact) {
if (GROUP_ID.equals(artifact.getGroupId())
&& ARTIFACT_ID.equals(artifact.getArtifactId())
&& VERSION.equals(artifact.getVersion())) {
Model m = new Model();
m.setArtifactId(ARTIFACT_ID);
m.setGroupId(GROUP_ID);
m.setVersion(VERSION);
Repository repository = new Repository();
repository.setId(REPO_ID);
repository.setUrl(REPO_URL);
repository.setLayout(REPO_LAYOUT);
m.getRepositories().add(repository);
return m;
}
return null;
}
}

View File

@ -34,6 +34,7 @@ import java.util.Set;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
@ -53,12 +54,14 @@ import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Relocation;
import org.apache.maven.model.Repository;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblem;
@ -93,7 +96,10 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
private RepositoryMetadataManager repositoryMetadataManager;
@Requirement
private ArtifactFactory repositorySystem;
private ArtifactFactory artifactFactory;
@Requirement
private MavenRepositorySystem repositorySystem;
// TODO This prevents a cycle in the composition which shows us another problem we need to deal with.
// @Requirement
@ -120,6 +126,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
}
}
@Override
public ResolutionGroup retrieve(
Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
throws ArtifactMetadataRetrievalException {
@ -141,6 +148,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return retrieve(request);
}
@Override
public ResolutionGroup retrieve(MetadataResolutionRequest request) throws ArtifactMetadataRetrievalException {
Artifact artifact = request.getArtifact();
@ -175,7 +183,8 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
Artifact relocatedArtifact = null;
// TODO hack: don't rebuild model if it was already loaded during reactor resolution
final WorkspaceReader workspace = legacySupport.getRepositorySession().getWorkspaceReader();
RepositorySystemSession repositorySession = legacySupport.getRepositorySession();
final WorkspaceReader workspace = repositorySession.getWorkspaceReader();
Model model = null;
if (workspace instanceof MavenWorkspaceReader) {
model = ((MavenWorkspaceReader) workspace).findModel(RepositoryUtils.toArtifact(artifact));
@ -188,9 +197,17 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
managedDependencies = dependencyManagement == null ? null : dependencyManagement.getDependencies();
MavenSession session = legacySupport.getSession();
if (session != null) {
MavenProject project = session.getProjectMap()
.get(ArtifactUtils.key(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()));
pomRepositories = project.getRemoteArtifactRepositories();
Map<String, MavenProject> projectMap = session.getProjectMap();
MavenProject project = projectMap == null
? null
: projectMap.get(ArtifactUtils.key(
artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()));
if (project == null) {
// if the project is not on the project map, read the repositories from the model itself!
pomRepositories = getRepositoriesFromModel(repositorySession, model);
} else {
pomRepositories = project.getRemoteArtifactRepositories();
}
} else {
pomRepositories = new ArrayList<>();
}
@ -267,6 +284,21 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return result;
}
private List<ArtifactRepository> getRepositoriesFromModel(RepositorySystemSession repositorySession, Model model) {
List<ArtifactRepository> pomRepositories = new ArrayList<>();
for (Repository modelRepository : model.getRepositories()) {
try {
pomRepositories.add(MavenRepositorySystem.buildArtifactRepository(modelRepository));
} catch (InvalidRepositoryException e) {
// can not use this then
}
}
repositorySystem.injectMirror(repositorySession, pomRepositories);
repositorySystem.injectProxy(repositorySession, pomRepositories);
repositorySystem.injectAuthentication(repositorySession, pomRepositories);
return pomRepositories;
}
private boolean hasFile(Artifact artifact) {
return artifact != null
&& artifact.getFile() != null
@ -305,7 +337,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
ArtifactFilter inheritedFilter = (owner != null) ? owner.getDependencyFilter() : null;
return createDependencyArtifact(repositorySystem, dependency, inheritedScope, inheritedFilter);
return createDependencyArtifact(artifactFactory, dependency, inheritedScope, inheritedFilter);
} catch (InvalidVersionSpecificationException e) {
throw new ArtifactMetadataRetrievalException(
"Invalid version for dependency " + dependency.getManagementKey() + ": " + e.getMessage(), e, pom);
@ -389,6 +421,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return effectiveFilter;
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(
Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
throws ArtifactMetadataRetrievalException {
@ -400,6 +433,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return retrieveAvailableVersions(request);
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(MetadataResolutionRequest request)
throws ArtifactMetadataRetrievalException {
RepositoryMetadata metadata = new ArtifactRepositoryMetadata(request.getArtifact());
@ -415,6 +449,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return retrieveAvailableVersionsFromMetadata(metadata.getMetadata(), availableVersions);
}
@Override
public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
Artifact artifact, ArtifactRepository localRepository, ArtifactRepository deploymentRepository)
throws ArtifactMetadataRetrievalException {
@ -502,7 +537,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
do {
project = null;
pomArtifact = repositorySystem.createProjectArtifact(
pomArtifact = artifactFactory.createProjectArtifact(
artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope());
if ("pom".equals(artifact.getType())) {