From f423e6550f2462d808f758f4459eac9d008173c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 4 Feb 2024 18:38:10 +0100 Subject: [PATCH] Resolve API baseline with all matching environments Currently the baseline is only resolved against the running target environment, but this does not work when multiple ones are matching or there is a platform filter. This resolves the API baseline against all matching target environments. --- .../org/eclipse/tycho/TargetEnvironment.java | 8 ++++++++ .../eclipse/tycho/apitools/ApiAnalysisMojo.java | 16 ++++++++-------- .../tycho/apitools/ApiApplicationResolver.java | 6 ++++-- .../eclipse/tycho/core/TychoProjectManager.java | 11 +++++++++++ ...DefaultTargetPlatformConfigurationReader.java | 13 +++---------- .../framework/EclipseApplicationFactory.java | 8 +++++--- 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/tycho-api/src/main/java/org/eclipse/tycho/TargetEnvironment.java b/tycho-api/src/main/java/org/eclipse/tycho/TargetEnvironment.java index d5e2a81384..8c03b971a3 100644 --- a/tycho-api/src/main/java/org/eclipse/tycho/TargetEnvironment.java +++ b/tycho-api/src/main/java/org/eclipse/tycho/TargetEnvironment.java @@ -21,6 +21,7 @@ import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; +import org.osgi.framework.Filter; public final class TargetEnvironment { private static final Properties EMPTY_PROPERTIES = new Properties(); @@ -70,6 +71,13 @@ public boolean match(String os, String ws, String arch) { (arch == null || arch.equals(this.arch)); } + public boolean match(Filter filter) { + if (filter != null) { + return filter.matches(toFilterProperties()); + } + return true; + } + /** * Returns the target environment as string of the form ws.os.arch. This format is * used by the p2 publishers and in that context called "configuration" or "config spec". diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java index bf67f52071..f6f711ba68 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java @@ -45,6 +45,7 @@ import org.eclipse.tycho.DependencyResolutionException; import org.eclipse.tycho.IllegalArtifactReferenceException; import org.eclipse.tycho.MavenRepositoryLocation; +import org.eclipse.tycho.TargetEnvironment; import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; @@ -134,9 +135,6 @@ public class ApiAnalysisMojo extends AbstractMojo { @Component private TychoProjectManager projectManager; - @Component - private ApiApplicationResolver resolver; - @Component private ApiApplicationResolver applicationResolver; @@ -145,8 +143,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (skip) { return; } - Optional eclipseProject = projectManager.getEclipseProject(project); - if (eclipseProject.isEmpty() || !eclipseProject.get().hasNature(ApiPlugin.NATURE_ID)) { + Optional eclipseProjectValue = projectManager.getEclipseProject(project); + if (eclipseProjectValue.isEmpty() || !eclipseProjectValue.get().hasNature(ApiPlugin.NATURE_ID)) { return; } @@ -293,12 +291,14 @@ private Collection getBaselineBundles() throws MojoFailureException { long start = System.currentTimeMillis(); Collection baselineBundles; try { + Collection targetEnvironments = projectManager.getTargetEnvironments(project); Optional artifactKey = projectManager.getArtifactKey(project); - getLog().info("Resolve API baseline for " + project.getId()); - baselineBundles = resolver.getApiBaselineBundles( + getLog().info("Resolve API baseline for " + project.getId() + " with " + + targetEnvironments.stream().map(String::valueOf).collect(Collectors.joining(", "))); + baselineBundles = applicationResolver.getApiBaselineBundles( baselines.stream().filter(repo -> repo.getUrl() != null) .map(repo -> new MavenRepositoryLocation(repo.getId(), URI.create(repo.getUrl()))).toList(), - artifactKey.get()); + artifactKey.get(), targetEnvironments); getLog().debug("API baseline contains " + baselineBundles.size() + " bundles (resolve takes " + time(start) + ")."); } catch (IllegalArtifactReferenceException e) { diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiApplicationResolver.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiApplicationResolver.java index 57cf919f2b..b0034964db 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiApplicationResolver.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiApplicationResolver.java @@ -28,6 +28,7 @@ import org.eclipse.tycho.ArtifactType; import org.eclipse.tycho.IllegalArtifactReferenceException; import org.eclipse.tycho.MavenRepositoryLocation; +import org.eclipse.tycho.TargetEnvironment; import org.eclipse.tycho.TargetPlatform; import org.eclipse.tycho.core.resolver.P2ResolutionResult; import org.eclipse.tycho.core.resolver.P2ResolutionResult.Entry; @@ -59,8 +60,9 @@ public class ApiApplicationResolver { private EclipseApplicationManager applicationManager; public Collection getApiBaselineBundles(Collection baselineRepoLocations, - ArtifactKey artifactKey) throws IllegalArtifactReferenceException { - P2Resolver resolver = applicationFactory.createResolver(); + ArtifactKey artifactKey, Collection environment) + throws IllegalArtifactReferenceException { + P2Resolver resolver = applicationFactory.createResolver(environment); resolver.addDependency(ArtifactType.TYPE_INSTALLABLE_UNIT, artifactKey.getId(), "0.0.0"); List resolvedBundles = new ArrayList<>(); TargetPlatform targetPlatform = applicationFactory.createTargetPlatform(baselineRepoLocations); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java b/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java index 0565d42e79..3bc1f79f47 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/TychoProjectManager.java @@ -62,6 +62,7 @@ import org.eclipse.tycho.helper.PluginRealmHelper; import org.eclipse.tycho.model.project.EclipseProject; import org.eclipse.tycho.targetplatform.TargetDefinition; +import org.osgi.framework.Filter; import aQute.bnd.osgi.Processor; @@ -176,6 +177,16 @@ public TargetPlatformConfiguration getTargetPlatformConfiguration(ReactorProject return getTargetPlatformConfiguration(project.adapt(MavenProject.class)); } + public Collection getTargetEnvironments(MavenProject project) { + TychoProject tychoProject = projectTypes.get(project.getPackaging()); + if (tychoProject != null) { + Filter environmentFilter = tychoProject.getTargetEnvironmentFilter(project); + return getTargetPlatformConfiguration(project).getEnvironments().stream() + .filter(te -> te.match(environmentFilter)).toList(); + } + return List.of(TargetEnvironment.getRunningEnvironment()); + } + public Optional getTychoProject(MavenProject project) { if (project == null) { return Optional.empty(); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java index c9cd9d6863..cdfff6cb0b 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/resolver/DefaultTargetPlatformConfigurationReader.java @@ -375,10 +375,10 @@ private void addTargetEnvironments(TargetPlatformConfiguration result, MavenProj List skipped = new ArrayList<>(); for (Xpp3Dom environmentDom : environmentsDom.getChildren("environment")) { TargetEnvironment environment = newTargetEnvironment(environmentDom); - if (!matchFilter(environment, filter)) { - skipped.add(environment); - } else { + if (environment.match(filter)) { result.addEnvironment(environment); + } else { + skipped.add(environment); } } if (!skipped.isEmpty()) { @@ -394,13 +394,6 @@ private void addTargetEnvironments(TargetPlatformConfiguration result, MavenProj } } - private static boolean matchFilter(TargetEnvironment environment, Filter filter) { - if (filter != null) { - return filter.matches(environment.toFilterProperties()); - } - return true; - } - private static Filter getTargetEnvironmentFilter(TychoProject tychoProject, MavenProject project) { if (tychoProject != null) { return tychoProject.getTargetEnvironmentFilter(project); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java b/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java index 1cc18105ed..f0d893eb14 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/osgi/framework/EclipseApplicationFactory.java @@ -13,7 +13,6 @@ package org.eclipse.tycho.osgi.framework; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; @@ -97,8 +96,11 @@ public TargetPlatform createTargetPlatform(Collection l } public P2Resolver createResolver() { - P2Resolver resolver = resolverFactory - .createResolver(Collections.singletonList(TargetEnvironment.getRunningEnvironment())); + return createResolver(List.of(TargetEnvironment.getRunningEnvironment())); + } + + public P2Resolver createResolver(Collection environments) { + P2Resolver resolver = resolverFactory.createResolver(environments); return resolver; }