[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 #981
This commit is contained in:
Christoph Läubrich 2023-02-05 08:35:15 +01:00 committed by Michael Osipov
parent f9ea10688c
commit 0525d90d42
6 changed files with 153 additions and 16 deletions

View File

@ -288,7 +288,7 @@ public abstract class AbstractArtifactComponentTestCase // extends PlexusTestCas
}
}
protected RepositorySystemSession initRepoSession() throws Exception {
protected DefaultRepositorySystemSession initRepoSession() throws Exception {
DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
DependencyTraverser depTraverser = new FatArtifactTraverser();
@ -311,7 +311,6 @@ public abstract class AbstractArtifactComponentTestCase // extends PlexusTestCas
LocalRepository localRepo = new LocalRepository(localRepository().getBasedir());
session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
return session;
}

View File

@ -35,6 +35,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.DefaultRepositorySystemSession;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -65,6 +66,13 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
projectArtifact = createLocalArtifact("project", "3.0");
}
@Override
protected DefaultRepositorySystemSession initRepoSession() throws Exception {
DefaultRepositorySystemSession session = super.initRepoSession();
session.setWorkspaceReader(new TestMavenWorkspaceReader());
return session;
}
@Override
protected String component() {
return "resolver";
@ -167,6 +175,17 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
assertLocalArtifactPresent(l);
}
public void testReadRepoFromModel() throws Exception {
Artifact m = createArtifact(TestMavenWorkspaceReader.ARTIFACT_ID, TestMavenWorkspaceReader.VERSION);
ArtifactMetadataSource source = getContainer().lookup(ArtifactMetadataSource.class, "maven");
ResolutionGroup group = source.retrieve(m, localRepository(), new ArrayList<ArtifactRepository>());
List<ArtifactRepository> repositories = group.getResolutionRepositories();
assertEquals(1, repositories.size(), "There should be one repository!");
ArtifactRepository repository = repositories.get(0);
assertEquals(TestMavenWorkspaceReader.REPO_ID, repository.getId());
assertEquals(TestMavenWorkspaceReader.REPO_URL, repository.getUrl());
}
@Test
public void testTransitiveResolutionOrder() throws Exception {
Artifact m = createLocalArtifact("m", "1.0");
@ -174,6 +193,7 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
Artifact n = createLocalArtifact("n", "1.0");
ArtifactMetadataSource mds = new ArtifactMetadataSource() {
@Override
public ResolutionGroup retrieve(
Artifact artifact,
ArtifactRepository localRepository,
@ -183,6 +203,7 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
return new ResolutionGroup(artifact, dependencies, remoteRepositories);
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(
Artifact artifact,
ArtifactRepository localRepository,
@ -190,15 +211,18 @@ public class ArtifactResolverTest extends AbstractArtifactComponentTestCase {
throw new UnsupportedOperationException("Cannot get available versions in this test case");
}
@Override
public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
Artifact artifact, ArtifactRepository localRepository, ArtifactRepository remoteRepository) {
throw new UnsupportedOperationException("Cannot get available versions in this test case");
}
@Override
public ResolutionGroup retrieve(MetadataResolutionRequest request) {
return retrieve(request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories());
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(MetadataResolutionRequest request) {
return retrieveAvailableVersions(
request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories());

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

@ -24,6 +24,7 @@ import javax.inject.Singleton;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.project.ProjectBuilder;
@ -40,10 +41,11 @@ public class DefaultMetadataSource extends MavenMetadataSource {
@Inject
public DefaultMetadataSource(
RepositoryMetadataManager repositoryMetadataManager,
ArtifactFactory repositorySystem,
ArtifactFactory artifactFactory,
ProjectBuilder projectBuilder,
MavenMetadataCache cache,
LegacySupport legacySupport) {
super(repositoryMetadataManager, repositorySystem, projectBuilder, cache, legacySupport);
LegacySupport legacySupport,
MavenRepositorySystem mavenRepositorySystem) {
super(repositoryMetadataManager, artifactFactory, projectBuilder, cache, legacySupport, mavenRepositorySystem);
}
}

View File

@ -37,6 +37,7 @@ import java.util.Set;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
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;
@ -56,12 +57,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;
@ -92,23 +95,27 @@ import org.slf4j.LoggerFactory;
public class MavenMetadataSource implements ArtifactMetadataSource {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final RepositoryMetadataManager repositoryMetadataManager;
private final ArtifactFactory repositorySystem;
private final ArtifactFactory artifactFactory;
private final ProjectBuilder projectBuilder;
private final MavenMetadataCache cache;
private final LegacySupport legacySupport;
private MavenRepositorySystem mavenRepositorySystem;
@Inject
public MavenMetadataSource(
RepositoryMetadataManager repositoryMetadataManager,
ArtifactFactory repositorySystem,
ArtifactFactory artifactFactory,
ProjectBuilder projectBuilder,
MavenMetadataCache cache,
LegacySupport legacySupport) {
LegacySupport legacySupport,
MavenRepositorySystem mavenRepositorySystem) {
this.repositoryMetadataManager = repositoryMetadataManager;
this.repositorySystem = repositorySystem;
this.artifactFactory = artifactFactory;
this.projectBuilder = projectBuilder;
this.cache = cache;
this.legacySupport = legacySupport;
this.mavenRepositorySystem = mavenRepositorySystem;
}
private void injectSession(MetadataResolutionRequest request) {
@ -120,6 +127,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
}
}
@Override
public ResolutionGroup retrieve(
Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
throws ArtifactMetadataRetrievalException {
@ -141,6 +149,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return retrieve(request);
}
@Override
public ResolutionGroup retrieve(MetadataResolutionRequest request) throws ArtifactMetadataRetrievalException {
Artifact artifact = request.getArtifact();
@ -175,10 +184,13 @@ 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();
Model model = null;
RepositorySystemSession repositorySession = legacySupport.getRepositorySession();
final WorkspaceReader workspace = repositorySession.getWorkspaceReader();
Model model;
if (workspace instanceof MavenWorkspaceReader) {
model = ((MavenWorkspaceReader) workspace).findModel(RepositoryUtils.toArtifact(artifact));
} else {
model = null;
}
if (model != null) {
@ -192,7 +204,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
.filter(p -> artifact.equals(p.getArtifact()))
.map(MavenProject::getRemoteArtifactRepositories)
.findFirst()
.orElseGet(ArrayList::new);
.orElseGet(() -> getRepositoriesFromModel(repositorySession, model));
} else {
pomRepositories = new ArrayList<>();
}
@ -269,6 +281,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
}
}
mavenRepositorySystem.injectMirror(repositorySession, pomRepositories);
mavenRepositorySystem.injectProxy(repositorySession, pomRepositories);
mavenRepositorySystem.injectAuthentication(repositorySession, pomRepositories);
return pomRepositories;
}
private boolean hasFile(Artifact artifact) {
return artifact != null
&& artifact.getFile() != null
@ -307,7 +334,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 +416,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return effectiveFilter;
}
@Override
public List<ArtifactVersion> retrieveAvailableVersions(
Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
throws ArtifactMetadataRetrievalException {
@ -400,6 +428,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 +444,7 @@ public class MavenMetadataSource implements ArtifactMetadataSource {
return retrieveAvailableVersionsFromMetadata(metadata.getMetadata(), availableVersions);
}
@Override
public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
Artifact artifact, ArtifactRepository localRepository, ArtifactRepository deploymentRepository)
throws ArtifactMetadataRetrievalException {
@ -488,7 +518,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())) {

View File

@ -29,6 +29,7 @@ import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.project.artifact.MavenMetadataCache;
import org.apache.maven.project.artifact.MavenMetadataSource;
@ -43,8 +44,9 @@ public class TestMetadataSource extends MavenMetadataSource {
ArtifactFactory repositorySystem,
ProjectBuilder projectBuilder,
MavenMetadataCache cache,
LegacySupport legacySupport) {
super(repositoryMetadataManager, repositorySystem, projectBuilder, cache, legacySupport);
LegacySupport legacySupport,
MavenRepositorySystem mavenRepositorySystem) {
super(repositoryMetadataManager, repositorySystem, projectBuilder, cache, legacySupport, mavenRepositorySystem);
}
@Override