From ff1f7538c8ecd9718a1bd269dd791289ff1c7ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Thu, 20 Jul 2023 19:21:34 +0200 Subject: [PATCH] Check if the about to be injected maven coordinates can be resolved Fix https://github.com/eclipse-tycho/tycho/issues/2625 --- .../core/TargetPlatformConfiguration.java | 30 +++++++++++++++++ .../p2resolver/P2DependencyResolver.java | 33 +++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java b/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java index 186b55c3f6..d103c80fc9 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/TargetPlatformConfiguration.java @@ -69,6 +69,22 @@ public enum LocalArtifactHandling { ignore; } + public enum InjectP2MavenMetadataHandling { + /** + * ignores P2 maven metadata + */ + ignore, + /** + * inject if found in P2 metadata + */ + inject, + /** + * inject if found in P2 metadata, but validate if it is actually valid for the current + * build + */ + validate; + } + private String resolver; private List environments = new ArrayList<>(); @@ -99,6 +115,9 @@ public enum LocalArtifactHandling { private LocalArtifactHandling localArtifactHandling; private boolean requireEagerResolve; + + private InjectP2MavenMetadataHandling p2MavenMetadataHandling; + /** * Returns the list of configured target environments, or the running environment if no * environments have been specified explicitly. @@ -277,6 +296,17 @@ public LocalArtifactHandling getIgnoreLocalArtifacts() { return localArtifactHandling; } + public InjectP2MavenMetadataHandling getP2MetadataHandling() { + if (p2MavenMetadataHandling == null) { + return InjectP2MavenMetadataHandling.validate; + } + return p2MavenMetadataHandling; + } + + public void setP2MavenMetadataHandling(InjectP2MavenMetadataHandling p2MavenMetadataHandling) { + this.p2MavenMetadataHandling = p2MavenMetadataHandling; + } + public void setLocalArtifactHandling(LocalArtifactHandling localArtifactHandling) { this.localArtifactHandling = localArtifactHandling; } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java index c0e380fd68..92257acb70 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/p2resolver/P2DependencyResolver.java @@ -30,6 +30,7 @@ import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import java.util.function.Function; import java.util.function.Supplier; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -44,8 +45,10 @@ import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; +import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.metadata.IRequirement; +import org.eclipse.tycho.ArtifactDescriptor; import org.eclipse.tycho.ArtifactKey; import org.eclipse.tycho.BuildFailureException; import org.eclipse.tycho.BuildProperties; @@ -56,6 +59,7 @@ import org.eclipse.tycho.IDependencyMetadata; import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType; import org.eclipse.tycho.IllegalArtifactReferenceException; +import org.eclipse.tycho.MavenDependencyDescriptor; import org.eclipse.tycho.MavenRepositoryLocation; import org.eclipse.tycho.OptionalResolutionAction; import org.eclipse.tycho.ReactorProject; @@ -65,8 +69,10 @@ import org.eclipse.tycho.core.DependencyResolver; import org.eclipse.tycho.core.DependencyResolverConfiguration; import org.eclipse.tycho.core.TargetPlatformConfiguration; +import org.eclipse.tycho.core.TargetPlatformConfiguration.InjectP2MavenMetadataHandling; import org.eclipse.tycho.core.TargetPlatformConfiguration.LocalArtifactHandling; import org.eclipse.tycho.core.TychoProjectManager; +import org.eclipse.tycho.core.maven.MavenDependenciesResolver; import org.eclipse.tycho.core.maven.MavenDependencyInjector; import org.eclipse.tycho.core.osgitools.AbstractTychoProject; import org.eclipse.tycho.core.osgitools.BundleReader; @@ -131,6 +137,9 @@ public class P2DependencyResolver extends AbstractLogEnabled implements Dependen @Requirement private PomUnits pomUnits; + @Requirement + private MavenDependenciesResolver dependenciesResolver; + @Override public void setupProjects(final MavenSession session, final MavenProject project, final ReactorProject reactorProject) { @@ -387,8 +396,28 @@ public void initialize() throws InitializationException { @Override public void injectDependenciesIntoMavenModel(MavenProject project, AbstractTychoProject projectType, DependencyArtifacts dependencyArtifacts, DependencyArtifacts testDependencyArtifacts, Logger logger) { + Function descriptorMapping; + TargetPlatformConfiguration configuration = projectManager.getTargetPlatformConfiguration(project); + if (configuration.getP2MetadataHandling() == InjectP2MavenMetadataHandling.inject) { + descriptorMapping = resolverFactory::resolveDependencyDescriptor; + } else if (configuration.getP2MetadataHandling() == InjectP2MavenMetadataHandling.validate) { + descriptorMapping = descriptor -> { + MavenDependencyDescriptor result = resolverFactory.resolveDependencyDescriptor(descriptor); + if (result != null) { + try { + dependenciesResolver.resolveArtifact(project, context.getSession(), result.getGroupId(), + result.getArtifactId(), result.getVersion()); + } catch (ArtifactResolutionException e) { + logger.warn("Mapping P2 > Maven Coordinates failed: " + e.getMessage()); + } + } + return null; + }; + } else { + descriptorMapping = null; + } MavenDependencyInjector.injectMavenDependencies(project, dependencyArtifacts, testDependencyArtifacts, - bundleReader, resolverFactory::resolveDependencyDescriptor, logger, repositorySystem, - context.getSession().getSettings(), buildPropertiesParser); + bundleReader, descriptorMapping, logger, repositorySystem, context.getSession().getSettings(), + buildPropertiesParser); } }