Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of usage RepositoryMetadataManager #182

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.codehaus.mojo.mrm.plugin;

import org.apache.maven.archetype.ArchetypeManager;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.eclipse.aether.RepositorySystem;

/**
Expand All @@ -31,12 +30,6 @@ public interface FactoryHelper {
*/
RepositorySystem getRepositorySystem();

/**
* @return returns the {@link RepositoryMetadataManager} instance
* @since 1.0
*/
RepositoryMetadataManager getRepositoryMetadataManager();

/**
* @return returns the {@link ArchetypeManager} instance
* @since 1.0
Expand Down
4 changes: 4 additions & 0 deletions mrm-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>
</dependency>

<!-- tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = help:system
12 changes: 12 additions & 0 deletions mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.mojohaus.mrm.hostedrepo.its</groupId>
<artifactId>plugins-metadata</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<description>Test if plugins metadata G level is downloaded</description>
</project>
4 changes: 2 additions & 2 deletions mrm-maven-plugin/src/it/hostedrepo/src/it/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ under the License.
<repositories>
<repository>
<id>local.central</id>
<url>http://local.central</url>
<url>http://localhost</url>
<releases>
<enabled>true</enabled>
</releases>
Expand All @@ -46,7 +46,7 @@ under the License.
<pluginRepositories>
<pluginRepository>
<id>local.central</id>
<url>http://local.central</url>
<url>http://localhost</url>
<releases>
<enabled>true</enabled>
</releases>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,40 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.archetype.ArchetypeManager;
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.*;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.mrm.api.ResolverUtils;
import org.codehaus.mojo.mrm.api.maven.*;
import org.codehaus.mojo.mrm.api.maven.ArchetypeCatalogNotFoundException;
import org.codehaus.mojo.mrm.api.maven.Artifact;
import org.codehaus.mojo.mrm.api.maven.ArtifactNotFoundException;
import org.codehaus.mojo.mrm.api.maven.BaseArtifactStore;
import org.codehaus.mojo.mrm.api.maven.MetadataNotFoundException;
import org.codehaus.mojo.mrm.plugin.FactoryHelper;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.metadata.DefaultMetadata;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.MetadataRequest;
import org.eclipse.aether.resolution.MetadataResult;

import static java.util.Optional.ofNullable;

Expand All @@ -50,21 +64,11 @@ public class ProxyArtifactStore extends BaseArtifactStore {

private final List<RemoteRepository> remoteRepositories;

/**
* The remote repositories that we will query.
*/
private final List<ArtifactRepository> artifactRepositories;

/**
* The {@link Log} to log to.
*/
private final Log log;

/**
* A version range that matches any version
*/
private static final VersionRange ANY_VERSION;

/**
* A cache of what artifacts are present.
*/
Expand All @@ -74,17 +78,6 @@ public class ProxyArtifactStore extends BaseArtifactStore {

private final MavenSession session;

static {
try {
ANY_VERSION = VersionRange.createFromVersionSpec("[0,]");
} catch (InvalidVersionSpecificationException e) {
// must never happen... so if it does make sure we stop
throw new IllegalStateException("[0,] should always be a valid version specification", e);
}
}

private final RepositoryMetadataManager repositoryMetadataManager;

private final ArchetypeManager archetypeManager;

/**
Expand All @@ -96,16 +89,10 @@ public class ProxyArtifactStore extends BaseArtifactStore {
*/
public ProxyArtifactStore(FactoryHelper factoryHelper, MavenSession session, Log log) {
this.repositorySystem = Objects.requireNonNull(factoryHelper.getRepositorySystem());
this.repositoryMetadataManager = Objects.requireNonNull(factoryHelper.getRepositoryMetadataManager());
this.archetypeManager = Objects.requireNonNull(factoryHelper.getArchetypeManager());
this.log = log;
this.session = Objects.requireNonNull(session);

artifactRepositories = Stream.concat(
session.getCurrentProject().getRemoteArtifactRepositories().stream(),
session.getCurrentProject().getPluginArtifactRepositories().stream())
.distinct()
.collect(Collectors.toList());
remoteRepositories = Stream.concat(
session.getCurrentProject().getRemoteProjectRepositories().stream(),
session.getCurrentProject().getRemotePluginRepositories().stream())
Expand Down Expand Up @@ -242,49 +229,76 @@ public void set(Artifact artifact, InputStream content) {

@Override
public Metadata getMetadata(String path) throws MetadataNotFoundException {
path = StringUtils.strip(path, "/");
Metadata metadata = new Metadata();
boolean foundSomething = false;

// is this path a groupId:artifactId pair?
int slashIndex = path.lastIndexOf('/');
String artifactId = slashIndex == -1 ? null : path.substring(slashIndex + 1);
String groupId = slashIndex == -1 ? null : path.substring(0, slashIndex).replace('/', '.');
if (!StringUtils.isEmpty(artifactId) && !StringUtils.isEmpty(groupId)) {
org.apache.maven.artifact.Artifact artifact = createDependencyArtifact(groupId, artifactId);
ArtifactRepositoryMetadata artifactRepositoryMetadata = new ArtifactRepositoryMetadata(artifact);
try {
repositoryMetadataManager.resolve(
artifactRepositoryMetadata, artifactRepositories, session.getLocalRepository());

final Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata();
if (artifactMetadata.getVersioning() != null) {
foundSomething = true;
if (StringUtils.isEmpty(metadata.getGroupId())) {
metadata.setGroupId(groupId);
metadata.setArtifactId(artifactId);
}
metadata.merge(artifactMetadata);
for (String v : artifactMetadata.getVersioning().getVersions()) {
addResolved(path + "/" + v);
}
LinkedList<String> pathItems =
new LinkedList<>(Arrays.asList(StringUtils.strip(path, "/").split("/")));

String version;
String artifactId;
String groupId;

org.eclipse.aether.metadata.Metadata.Nature metadataNature;

if (pathItems.getLast().endsWith("-SNAPSHOT")) {
// V level metadata request
if (pathItems.size() < 3) {
// at least we need G:A:V
throw new MetadataNotFoundException(path);
}
metadataNature = org.eclipse.aether.metadata.Metadata.Nature.SNAPSHOT;
version = pathItems.pollLast();
artifactId = pathItems.pollLast();
} else {
// A or G level metadata request
metadataNature = org.eclipse.aether.metadata.Metadata.Nature.RELEASE_OR_SNAPSHOT;
version = null;
artifactId = null;
}

groupId = String.join(".", pathItems);

org.eclipse.aether.metadata.Metadata requestedMetadata =
new DefaultMetadata(groupId, artifactId, version, "maven-metadata.xml", metadataNature);
List<MetadataRequest> requests = new ArrayList<>();
for (RemoteRepository repo : remoteRepositories) {
MetadataRequest request = new MetadataRequest();
request.setMetadata(requestedMetadata);
request.setRepository(repo);
requests.add(request);
}

List<MetadataResult> metadataResults =
repositorySystem.resolveMetadata(session.getRepositorySession(), requests);

Metadata resultMetadata = null;
for (MetadataResult result : metadataResults) {
if (!result.isResolved()) {
continue;
}
Metadata metadata = readMetadata(result.getMetadata().getFile());
if (metadata != null) {
if (resultMetadata == null) {
resultMetadata = metadata;
} else {
resultMetadata.merge(metadata);
}
} catch (RepositoryMetadataResolutionException e) {
log.debug(e);
}
}

if (!foundSomething) {
if (resultMetadata == null) {
throw new MetadataNotFoundException(path);
}

addResolved(path);
return metadata;
return resultMetadata;
}

private org.apache.maven.artifact.Artifact createDependencyArtifact(String groupId, String artifactId) {

return new org.apache.maven.artifact.DefaultArtifact(
groupId, artifactId, ANY_VERSION, "compile", "pom", "", null);
private Metadata readMetadata(File file) {
try (InputStream in = Files.newInputStream(file.toPath())) {
return new MetadataXpp3Reader().read(in);
} catch (IOException | XmlPullParserException e) {
log.warn("Error reading metadata from file: " + file, e);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javax.inject.Singleton;

import org.apache.maven.archetype.ArchetypeManager;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.eclipse.aether.RepositorySystem;

/**
Expand All @@ -18,17 +17,11 @@
public class DefaultFactoryHelper implements FactoryHelper {
private RepositorySystem repositorySystem;

private RepositoryMetadataManager repositoryMetadataManager;

private ArchetypeManager archetypeManager;

@Inject
public DefaultFactoryHelper(
RepositorySystem repositorySystem,
RepositoryMetadataManager repositoryMetadataManager,
ArchetypeManager archetypeManager) {
public DefaultFactoryHelper(RepositorySystem repositorySystem, ArchetypeManager archetypeManager) {
this.repositorySystem = repositorySystem;
this.repositoryMetadataManager = repositoryMetadataManager;
this.archetypeManager = archetypeManager;
}

Expand All @@ -37,11 +30,6 @@ public RepositorySystem getRepositorySystem() {
return repositorySystem;
}

@Override
public RepositoryMetadataManager getRepositoryMetadataManager() {
return repositoryMetadataManager;
}

@Override
public ArchetypeManager getArchetypeManager() {
return archetypeManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Collections;

import org.apache.maven.archetype.ArchetypeManager;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.mrm.api.maven.Artifact;
Expand Down Expand Up @@ -63,7 +62,6 @@ void verifyArtifactNotFoundExceptionOnGet() throws Exception {
doThrow(ArtifactResolutionException.class).when(repositorySystem).resolveArtifact(any(), any());
FactoryHelper factoryHelper = mock(FactoryHelper.class);
when(factoryHelper.getRepositorySystem()).thenReturn(repositorySystem);
when(factoryHelper.getRepositoryMetadataManager()).then(i -> mock(RepositoryMetadataManager.class));
when(factoryHelper.getArchetypeManager()).then(i -> mock(ArchetypeManager.class));

ProxyArtifactStore store = new ProxyArtifactStore(factoryHelper, mavenSession, null);
Expand All @@ -79,7 +77,6 @@ void verifyArtifactResolutionExceptionOnGet() throws Exception {
doThrow(new RuntimeException("test123")).when(repositorySystem).resolveArtifact(any(), any());
FactoryHelper factoryHelper = mock(FactoryHelper.class);
when(factoryHelper.getRepositorySystem()).thenReturn(repositorySystem);
when(factoryHelper.getRepositoryMetadataManager()).then(i -> mock(RepositoryMetadataManager.class));
when(factoryHelper.getArchetypeManager()).then(i -> mock(ArchetypeManager.class));

ProxyArtifactStore store = new ProxyArtifactStore(factoryHelper, mavenSession, null);
Expand All @@ -95,7 +92,6 @@ void verifyArchetypeCatalogNotFoundException() throws Exception {
doThrow(new RuntimeException("test123")).when(archetypeManager).getLocalCatalog(any());
FactoryHelper factoryHelper = mock(FactoryHelper.class);
when(factoryHelper.getRepositorySystem()).then(i -> mock(RepositorySystem.class));
when(factoryHelper.getRepositoryMetadataManager()).then(i -> mock(RepositoryMetadataManager.class));
when(factoryHelper.getArchetypeManager()).thenReturn(archetypeManager);
ProxyArtifactStore store = new ProxyArtifactStore(factoryHelper, mavenSession, null);

Expand Down