From 36324dc5c65c5d7c3ec84776ca01d133865ebe80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 27 Apr 2024 07:57:24 +0200 Subject: [PATCH] Use EclipseWorkspaceManager.getWorkspace(URI, Mojo) in ApiAnalysisMojo --- .../tycho/apitools/ApiAnalysisMojo.java | 67 +++++-------------- .../tycho/test/apitools/ApiToolsTest.java | 10 +-- 2 files changed, 24 insertions(+), 53 deletions(-) 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 0915f8b3c9..76bf4e0107 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 @@ -49,8 +49,8 @@ import org.eclipse.tycho.TargetEnvironment; import org.eclipse.tycho.TychoConstants; import org.eclipse.tycho.core.EcJLogFileEnhancer; -import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.EcJLogFileEnhancer.Source; +import org.eclipse.tycho.core.TychoProjectManager; import org.eclipse.tycho.core.osgitools.DefaultReactorProject; import org.eclipse.tycho.model.project.EclipseProject; import org.eclipse.tycho.osgi.framework.EclipseApplication; @@ -161,16 +161,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } long start = System.currentTimeMillis(); - Collection baselineBundles; - try { - baselineBundles = getBaselineBundles(); - } catch (DependencyResolutionException e) { - if (failOnResolutionError) { - throw new MojoFailureException("Can't resolve API baseline!", e); - } else { - log.warn("Can't resolve API baseline, API baseline check is skipped!"); - return; - } + Collection baselineBundles = getBaselineBundles(); + if (baselineBundles.isEmpty()) { + log.info("Skipped because no bundles in the baseline!"); + return; } Collection dependencyBundles; try { @@ -178,8 +172,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { } catch (Exception e) { throw new MojoFailureException("Can't fetch dependencies!", e); } - EclipseWorkspace workspace = getWorkspace(); - EclipseApplication apiApplication = applicationResolver.getApiApplication(workspace.getKey().repository); + MavenRepositoryLocation repository = getRepository(); + EclipseWorkspace workspace = workspaceManager.getWorkspace(repository.getURL(), this); + EclipseApplication apiApplication = applicationResolver.getApiApplication(repository); EclipseFramework eclipseFramework; try { eclipseFramework = apiApplication.startFramework(workspace, List.of()); @@ -336,10 +331,6 @@ private Path getFullPath(IApiProblem problem) { return project.getBasedir().toPath().resolve(path); } - private EclipseWorkspace getWorkspace() { - return workspaceManager.getWorkspace(new ApiAppKey(getRepository())); - } - private boolean wasReplaced() { if (DefaultReactorProject.adapt(project) .getContextValue(TychoConstants.KEY_BASELINE_REPLACE_ARTIFACT_MAIN) instanceof Boolean replaced) { @@ -357,22 +348,29 @@ private MavenRepositoryLocation getRepository() { private Collection getBaselineBundles() throws MojoFailureException { long start = System.currentTimeMillis(); - Collection baselineBundles; try { Collection targetEnvironments = getBaselineEnvironments(); Optional artifactKey = projectManager.getArtifactKey(project); getLog().info("Resolve API baseline for " + project.getId() + " with " + targetEnvironments.stream().map(String::valueOf).collect(Collectors.joining(", "))); - baselineBundles = applicationResolver.getApiBaselineBundles( + Collection baselineBundles = applicationResolver.getApiBaselineBundles( baselines.stream().filter(repo -> repo.getUrl() != null) .map(repo -> new MavenRepositoryLocation(repo.getId(), URI.create(repo.getUrl()))).toList(), artifactKey.get(), targetEnvironments); getLog().debug("API baseline contains " + baselineBundles.size() + " bundles (resolve takes " + time(start) + ")."); + return baselineBundles; } catch (IllegalArtifactReferenceException e) { throw new MojoFailureException("Project specify an invalid artifact key", e); + } catch (DependencyResolutionException e) { + if (failOnResolutionError) { + throw new MojoFailureException("Can't resolve API baseline!", e); + } else { + getLog().warn( + "Can't resolve API baseline: " + Objects.requireNonNullElse(e.getMessage(), e.toString())); + return List.of(); + } } - return baselineBundles; } /** @@ -403,35 +401,6 @@ private String time(long start) { return sec + " s"; } - private static final class ApiAppKey { - - private URI key; - private MavenRepositoryLocation repository; - - public ApiAppKey(MavenRepositoryLocation repository) { - this.repository = repository; - key = Objects.requireNonNull(repository.getURL()).normalize(); - } - - @Override - public int hashCode() { - return Objects.hash(key); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - ApiAppKey other = (ApiAppKey) obj; - return Objects.equals(key, other.key); - } - - } - private static Path stringToPath(String file) { if (file == null) { return null; diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java index 4d155cd6d7..df086f041b 100644 --- a/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/apitools/ApiToolsTest.java @@ -156,7 +156,8 @@ public void testInvalidRepo() throws Exception { File repo = ResourceUtil.resolveTestResource("repositories/api-tools-broken"); verifier.addCliOption("-DbaselineRepo=" + repo.toURI()); - assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")), "Did not error on missing repo"); + assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")), + "Did not error on missing repo"); } @Test @@ -166,7 +167,8 @@ public void testBaselineResolutonFailure_Error() throws Exception { verifier.addCliOption("-DbaselineRepo=" + repo.toURI()); verifier.addCliOption("-DfailResolutionError=true"); - assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")), "Did not error on resolution failure"); + assertThrows(VerificationException.class, () -> verifier.executeGoals(List.of("clean", "verify")), + "Did not error on resolution failure"); verifier.verifyTextInLog("Can't resolve API baseline!"); } @@ -177,7 +179,7 @@ public void testBaselineResolutonFailure_Warn() throws Exception { verifier.addCliOption("-DbaselineRepo=" + repo.toURI()); verifier.executeGoals(List.of("clean", "verify")); - verifier.verifyTextInLog("Can't resolve API baseline, API baseline check is skipped!"); + verifier.verifyTextInLog("Can't resolve API baseline"); } @Test @@ -187,6 +189,6 @@ public void testBaselineResolutonFailure_Default() throws Exception { verifier.addCliOption("-DbaselineRepo=" + repo.toURI()); verifier.executeGoals(List.of("clean", "verify")); - verifier.verifyTextInLog("Can't resolve API baseline, API baseline check is skipped!"); + verifier.verifyTextInLog("Can't resolve API baseline"); } }