From 50f362ef9cd95a84da6596d9d2441680c9a2bfce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Tue, 24 Sep 2024 07:09:23 +0200 Subject: [PATCH] Convert AbstractEclipseTestMojo to use Aether API --- .../surefire/AbstractEclipseTestMojo.java | 42 ++++++++++--------- .../surefire/TychoIntegrationTestMojo.java | 12 ++++-- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java index 654b82e903..bb6e8a32f0 100644 --- a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java +++ b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/AbstractEclipseTestMojo.java @@ -40,24 +40,24 @@ import java.util.StringJoiner; import java.util.concurrent.ExecutionException; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; -import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; -import org.apache.maven.artifact.resolver.ArtifactResolutionResult; -import org.apache.maven.artifact.resolver.ResolutionErrorHandler; import org.apache.maven.model.Dependency; import org.apache.maven.model.Repository; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.repository.RepositorySystem; import org.apache.maven.surefire.api.booter.ProviderParameterNames; import org.apache.maven.surefire.api.util.ScanResult; import org.apache.maven.surefire.booter.BooterConstants; import org.apache.maven.surefire.booter.PropertiesWrapper; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.resolution.ArtifactRequest; +import org.eclipse.aether.resolution.ArtifactResolutionException; +import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.equinox.internal.p2.metadata.IRequiredCapability; import org.eclipse.equinox.p2.metadata.IRequirement; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; @@ -325,9 +325,6 @@ public abstract class AbstractEclipseTestMojo extends AbstractTestMojo { @Component protected RepositorySystem repositorySystem; - @Component - private ResolutionErrorHandler resolutionErrorHandler; - @Component(role = TychoProject.class) private Map projectTypes; @@ -1208,23 +1205,28 @@ private List getFrameworkExtensions() throws MojoExecutionException { if (frameworkExtensions != null) { for (Dependency frameworkExtension : frameworkExtensions) { - Artifact artifact = repositorySystem.createDependencyArtifact(frameworkExtension); - ArtifactResolutionRequest request = new ArtifactResolutionRequest(); - request.setArtifact(artifact); - request.setResolveRoot(true).setResolveTransitively(false); - request.setLocalRepository(session.getLocalRepository()); - // XXX wrong repositories -- these are user artifacts, not plugin artifacts - request.setRemoteRepositories(project.getPluginArtifactRepositories()); - request.setOffline(session.isOffline()); - request.setForceUpdate(session.getRequest().isUpdateSnapshots()); - ArtifactResolutionResult result = repositorySystem.resolve(request); + org.eclipse.aether.artifact.Artifact artifact = new org.eclipse.aether.artifact.DefaultArtifact( + frameworkExtension.getGroupId(), frameworkExtension.getArtifactId(), + frameworkExtension.getType(), frameworkExtension.getVersion()); + ArtifactRequest artifactRequest = new ArtifactRequest(); + artifactRequest.setArtifact(artifact); + artifactRequest.setRepositories(RepositoryUtils.toRepos(project.getRemoteArtifactRepositories())); + ArtifactResult artifactResult; try { - resolutionErrorHandler.throwErrors(request, result); + artifactResult = repositorySystem.resolveArtifact(session.getRepositorySession(), artifactRequest); + if (artifactResult.isResolved()) { + files.add(artifactResult.getArtifact().getFile()); + } else { + MojoExecutionException exception = new MojoExecutionException( + "Failed to resolve framework extension " + frameworkExtension.getManagementKey()); + artifactResult.getExceptions().forEach(exception::addSuppressed); + throw exception; + } } catch (ArtifactResolutionException e) { throw new MojoExecutionException( "Failed to resolve framework extension " + frameworkExtension.getManagementKey(), e); } - files.add(artifact.getFile()); + } } diff --git a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TychoIntegrationTestMojo.java b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TychoIntegrationTestMojo.java index 9b8796219b..86988ccca5 100644 --- a/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TychoIntegrationTestMojo.java +++ b/tycho-surefire/tycho-surefire-plugin/src/main/java/org/eclipse/tycho/surefire/TychoIntegrationTestMojo.java @@ -28,6 +28,7 @@ import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -50,8 +51,8 @@ * harness bundles. The bundles are resolved from the target platform of the project. Note that the * test runtime does typically not contain the entire target platform. If there are * implicitly required bundles (e.g. org.apache.felix.scr to make declarative services - * work), they need to be added manually through an extraRequirements configuration on the - * target-platform-configuration plugin. + * work), they need to be added manually through an extraRequirements configuration on + * the target-platform-configuration plugin. *

*

* This goal adopts the maven-failsafe paradigm, that works in the following way: @@ -99,6 +100,9 @@ public class TychoIntegrationTestMojo extends AbstractEclipseTestMojo { @Parameter(defaultValue = "${localRepository}", required = true, readonly = true) private ArtifactRepository localRepository; + @Component + protected org.apache.maven.repository.RepositorySystem oldRepositorySystem; + /** * Configures the packaging type where this mojos applies */ @@ -201,7 +205,7 @@ protected boolean useMetadataDirectory(ReactorProject otherProject) { } private ArtifactResolutionResult resolveDependency(final Dependency dependency) { - final var artifact = repositorySystem.createDependencyArtifact(dependency); + final var artifact = oldRepositorySystem.createDependencyArtifact(dependency); final var remoteRepositories = new ArrayList(32); remoteRepositories.addAll(pluginRemoteRepositories); remoteRepositories.addAll(projectRemoteRepositories); @@ -213,7 +217,7 @@ private ArtifactResolutionResult resolveDependency(final Dependency dependency) .setResolveTransitively(true)// .setCollectionFilter(new ProviderDependencyArtifactFilter())// .setRemoteRepositories(remoteRepositories); - return repositorySystem.resolve(request); + return oldRepositorySystem.resolve(request); } private static final class ProviderDependencyArtifactFilter implements ArtifactFilter {