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

Resolves #99: Removing dependencies on maven-artifact-manager from Maven 2 #157

Merged
merged 1 commit into from
May 12, 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 @@ -20,12 +20,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -34,25 +29,14 @@
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
import org.apache.maven.artifact.repository.metadata.*;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, strange: this was generated by spotless:apply.

import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
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.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.api.maven.*;
import org.codehaus.mojo.mrm.plugin.FactoryHelper;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.repository.RemoteRepository;
Expand Down Expand Up @@ -263,66 +247,13 @@ public void set(Artifact artifact, InputStream content) {
@Override
public Metadata getMetadata(String path) throws MetadataNotFoundException {
path = StringUtils.strip(path, "/");
int index = path.lastIndexOf('/');
int index2 = index == -1 ? -1 : path.lastIndexOf('/', index - 1);

String version = index2 == -1 ? null : path.substring(index + 1);
String artifactId = index2 == -1 ? null : path.substring(index2 + 1, index);
String groupId = index2 == -1 ? null : path.substring(0, index2).replace('/', '.');

Metadata metadata = new Metadata();

boolean foundSomething = false;

// is this path a groupId:artifactId pair?
if (version != null
&& version.endsWith("-SNAPSHOT")
&& !StringUtils.isEmpty(artifactId)
&& !StringUtils.isEmpty(groupId)) {
org.apache.maven.artifact.Artifact artifact = artifactFactory.createDependencyArtifact(
groupId, artifactId, VersionRange.createFromVersion(version), "pom", null, "compile");
SnapshotArtifactRepositoryMetadata artifactRepositoryMetadata =
new SnapshotArtifactRepositoryMetadata(artifact);
try {
repositoryMetadataManager.resolve(
artifactRepositoryMetadata, artifactRepositories, session.getLocalRepository());

Metadata artifactMetadata = artifactRepositoryMetadata.getMetadata();
if (artifactMetadata.getVersioning() != null
&& artifactMetadata.getVersioning().getSnapshot() != null) {
foundSomething = true;
metadata.setGroupId(groupId);
metadata.setArtifactId(artifactId);
metadata.setVersion(version);
metadata.merge(artifactMetadata);
}
try {
if (artifactMetadata.getVersioning() != null
&& !artifactMetadata
.getVersioning()
.getSnapshotVersions()
.isEmpty()) {
// TODO up to and including Maven 3.0.3 we do not get a populated SnapshotVersions
for (SnapshotVersion v :
artifactMetadata.getVersioning().getSnapshotVersions()) {
metadata.getVersioning().addSnapshotVersion(v);
if (v.getVersion().endsWith("-SNAPSHOT")) {
addResolved(new Artifact(
groupId, artifactId, version, v.getClassifier(), v.getExtension()));
}
}
}
} catch (NoSuchMethodError e) {
// ignore Maven 2.x doesn't give us the info
}
} catch (RepositoryMetadataResolutionException e) {
log.debug(e);
}
}

// is this path a groupId:artifactId pair?
artifactId = index == -1 ? null : path.substring(index + 1);
groupId = index == -1 ? null : path.substring(0, index).replace('/', '.');
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 =
artifactFactory.createDependencyArtifact(groupId, artifactId, ANY_VERSION, "pom", null, "compile");
Expand All @@ -348,23 +279,6 @@ public Metadata getMetadata(String path) throws MetadataNotFoundException {
}
}

// if this path a groupId on its own?
groupId = path.replace('/', '.');
final GroupRepositoryMetadata groupRepositoryMetadata = new GroupRepositoryMetadata(groupId);
try {
repositoryMetadataManager.resolve(
groupRepositoryMetadata,
session.getCurrentProject().getPluginArtifactRepositories(),
session.getLocalRepository());
foundSomething = true;
metadata.merge(groupRepositoryMetadata.getMetadata());
for (Plugin plugin : groupRepositoryMetadata.getMetadata().getPlugins()) {
addResolved(path + "/" + plugin.getArtifactId());
}
} catch (RepositoryMetadataResolutionException e) {
log.debug(e);
}

if (!foundSomething) {
throw new MetadataNotFoundException(path);
}
Expand Down Expand Up @@ -399,12 +313,12 @@ public long getMetadataLastModified(String path) throws MetadataNotFoundExceptio

@Override
public ArchetypeCatalog getArchetypeCatalog() {
return archetypeManager.getDefaultLocalCatalog();
return archetypeManager.getLocalCatalog(session.getProjectBuildingRequest());
}

@Override
public long getArchetypeCatalogLastModified() throws ArchetypeCatalogNotFoundException {
if (archetypeManager.getDefaultLocalCatalog() != null) {
if (archetypeManager.getLocalCatalog(session.getProjectBuildingRequest()) != null) {
return System.currentTimeMillis();
} else {
throw new ArchetypeCatalogNotFoundException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void verifyArtifactResolutionExceptionOnGet() throws Exception {
@Test(expected = RuntimeException.class)
public void verifyArchetypeCatalogNotFoundException() throws Exception {
ArchetypeManager archetypeManager = mock(ArchetypeManager.class);
doThrow(RuntimeException.class).when(archetypeManager).getDefaultLocalCatalog();
doThrow(RuntimeException.class).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));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<dependency>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-common</artifactId>
<version>2.2</version>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down