diff --git a/mrm-api/src/main/java/org/codehaus/mojo/mrm/plugin/FactoryHelper.java b/mrm-api/src/main/java/org/codehaus/mojo/mrm/plugin/FactoryHelper.java index d229977d..4be5c471 100644 --- a/mrm-api/src/main/java/org/codehaus/mojo/mrm/plugin/FactoryHelper.java +++ b/mrm-api/src/main/java/org/codehaus/mojo/mrm/plugin/FactoryHelper.java @@ -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; /** @@ -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 diff --git a/mrm-maven-plugin/pom.xml b/mrm-maven-plugin/pom.xml index 8ad099ae..ad0d98ba 100644 --- a/mrm-maven-plugin/pom.xml +++ b/mrm-maven-plugin/pom.xml @@ -120,6 +120,10 @@ commons-lang commons-lang + + org.codehaus.plexus + plexus-xml + diff --git a/mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/invoker.properties b/mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/invoker.properties new file mode 100644 index 00000000..d2b8b365 --- /dev/null +++ b/mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/invoker.properties @@ -0,0 +1 @@ +invoker.goals = help:system diff --git a/mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/pom.xml b/mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/pom.xml new file mode 100644 index 00000000..4a98bb91 --- /dev/null +++ b/mrm-maven-plugin/src/it/hostedrepo/src/it/plugins-metadata/pom.xml @@ -0,0 +1,12 @@ + + + + 4.0.0 + + org.mojohaus.mrm.hostedrepo.its + plugins-metadata + 1.0.0 + pom + + Test if plugins metadata G level is downloaded + diff --git a/mrm-maven-plugin/src/it/hostedrepo/src/it/settings.xml b/mrm-maven-plugin/src/it/hostedrepo/src/it/settings.xml index ede1a36d..628f357b 100644 --- a/mrm-maven-plugin/src/it/hostedrepo/src/it/settings.xml +++ b/mrm-maven-plugin/src/it/hostedrepo/src/it/settings.xml @@ -34,7 +34,7 @@ under the License. local.central - http://local.central + http://localhost true @@ -46,7 +46,7 @@ under the License. local.central - http://local.central + http://localhost true diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStore.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStore.java index 49ac7ee0..32ef2fcf 100644 --- a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStore.java +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStore.java @@ -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; @@ -50,21 +64,11 @@ public class ProxyArtifactStore extends BaseArtifactStore { private final List remoteRepositories; - /** - * The remote repositories that we will query. - */ - private final List 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. */ @@ -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; /** @@ -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()) @@ -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 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 requests = new ArrayList<>(); + for (RemoteRepository repo : remoteRepositories) { + MetadataRequest request = new MetadataRequest(); + request.setMetadata(requestedMetadata); + request.setRepository(repo); + requests.add(request); + } + + List 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 diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/DefaultFactoryHelper.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/DefaultFactoryHelper.java index 7b23eee1..711985e5 100644 --- a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/DefaultFactoryHelper.java +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/DefaultFactoryHelper.java @@ -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; /** @@ -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; } @@ -37,11 +30,6 @@ public RepositorySystem getRepositorySystem() { return repositorySystem; } - @Override - public RepositoryMetadataManager getRepositoryMetadataManager() { - return repositoryMetadataManager; - } - @Override public ArchetypeManager getArchetypeManager() { return archetypeManager; diff --git a/mrm-maven-plugin/src/test/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStoreTest.java b/mrm-maven-plugin/src/test/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStoreTest.java index 28663df5..a6ed9c6b 100644 --- a/mrm-maven-plugin/src/test/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStoreTest.java +++ b/mrm-maven-plugin/src/test/java/org/codehaus/mojo/mrm/maven/ProxyArtifactStoreTest.java @@ -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; @@ -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); @@ -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); @@ -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);