From 776120f5d7a7778d77312322ac36bf4f360e3b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Mon, 4 Nov 2024 06:38:30 +0100 Subject: [PATCH] Add new option to API tools verify goal to runAsJob This allows to configure if the analysis should run as a workspace job. (cherry picked from commit f784331b6f950da30c61d1d915897282456746cd) --- .../eclipse/tycho/apitools/ApiAnalysis.java | 88 +++++++++++-------- .../tycho/apitools/ApiAnalysisMojo.java | 10 ++- 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java index 0106ca9bab..c9136d6e16 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java @@ -103,10 +103,12 @@ public class ApiAnalysis implements Serializable, Callable { private String apiPreferences; private String binaryArtifact; private String outputDir; + private boolean runAsJob; ApiAnalysis(Collection baselineBundles, Collection dependencyBundles, String baselineName, Path apiFilterFile, Path apiPreferences, Path projectDir, boolean debug, Path binaryArtifact, - Path outputDir) { + Path outputDir, boolean runAsJob) { + this.runAsJob = runAsJob; this.targetBundles = dependencyBundles.stream().map(ApiAnalysis::pathAsString).toList(); this.baselineBundles = baselineBundles.stream().map(ApiAnalysis::pathAsString).toList(); this.baselineName = baselineName; @@ -163,45 +165,23 @@ public void aboutToRun(IJobChangeEvent event) { IPath projectPath = IPath.fromOSString(projectDir); IProject project = getProject(projectPath); ApiAnalysisResult result = new ApiAnalysisResult(getVersion()); - WorkspaceJob job = new WorkspaceJob("Tycho API Analysis") { + IStatus status; + if (runAsJob) { + WorkspaceJob job = new WorkspaceJob("Tycho API Analysis") { - @Override - public IStatus runInWorkspace(IProgressMonitor monitor) { - try { - BundleComponent projectComponent = getApiComponent(project, projectPath); - IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline"); - ResolverError[] resolverErrors = projectComponent.getErrors(); - if (resolverErrors != null && resolverErrors.length > 0) { - for (ResolverError error : resolverErrors) { - result.addResolverError(error); - } - } - IApiFilterStore filterStore = getApiFilterStore(projectComponent); - Properties preferences = getPreferences(); - BaseApiAnalyzer analyzer = new BaseApiAnalyzer(); - try { - analyzer.setContinueOnResolverError(true); - analyzer.analyzeComponent(null, filterStore, preferences, baseline, projectComponent, - new BuildContext(), new NullProgressMonitor()); - IApiProblem[] problems = analyzer.getProblems(); - for (IApiProblem problem : problems) { - result.addProblem(problem, project); - debug(String.valueOf(problem)); - } - } finally { - analyzer.dispose(); - ResourcesPlugin.getWorkspace().save(true, new NullProgressMonitor()); - } - } catch (Exception e) { - return Status.error("Api Analysis failed", e); + @Override + public IStatus runInWorkspace(IProgressMonitor monitor) { + return performAPIAnalysis(project, projectPath, result); } - return Status.OK_STATUS; - } - }; - job.setRule(ResourcesPlugin.getWorkspace().getRoot()); - job.schedule(); - job.join(); - IStatus status = job.getResult(); + + }; + job.setRule(ResourcesPlugin.getWorkspace().getRoot()); + job.schedule(); + job.join(); + status = job.getResult(); + } else { + status = performAPIAnalysis(project, projectPath, result); + } JRTUtil.reset(); // reclaim space due to loaded multiple JRTUtil should better be fixed to not // use that much space if (!status.isOK() && status.getException() instanceof Exception error) { @@ -210,6 +190,38 @@ public IStatus runInWorkspace(IProgressMonitor monitor) { return result; } + private IStatus performAPIAnalysis(IProject project, IPath projectPath, ApiAnalysisResult result) { + try { + BundleComponent projectComponent = getApiComponent(project, projectPath); + IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline"); + ResolverError[] resolverErrors = projectComponent.getErrors(); + if (resolverErrors != null && resolverErrors.length > 0) { + for (ResolverError error : resolverErrors) { + result.addResolverError(error); + } + } + IApiFilterStore filterStore = getApiFilterStore(projectComponent); + Properties preferences = getPreferences(); + BaseApiAnalyzer analyzer = new BaseApiAnalyzer(); + try { + analyzer.setContinueOnResolverError(true); + analyzer.analyzeComponent(null, filterStore, preferences, baseline, projectComponent, + new BuildContext(), new NullProgressMonitor()); + IApiProblem[] problems = analyzer.getProblems(); + for (IApiProblem problem : problems) { + result.addProblem(problem, project); + debug(String.valueOf(problem)); + } + } finally { + analyzer.dispose(); + ResourcesPlugin.getWorkspace().save(true, new NullProgressMonitor()); + } + } catch (Exception e) { + return Status.error("Api Analysis failed", e); + } + return Status.OK_STATUS; + } + private String getVersion() { Bundle apiToolsBundle = FrameworkUtil.getBundle(ApiModelFactory.class); if (apiToolsBundle != null) { 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 0510ad49ff..3f758d208a 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 @@ -141,6 +141,14 @@ public class ApiAnalysisMojo extends AbstractMojo { @Parameter(defaultValue = "false", property = "tycho.apitools.enhanceLogs") private boolean enhanceLogs; + /** + * Configures if the API Analysis should run as a workspace job, this ensure + * that no other actions are allowed to run in parallel what sometimes can + * result in failures to execute the api-analysis + */ + @Parameter(defaultValue = "true", property = "tycho.apitools.runAsJob") + private boolean runAsJob; + @Component private EclipseWorkspaceManager workspaceManager; @@ -350,7 +358,7 @@ private ApiAnalysisResult performAnalysis(Collection baselineBundles, Coll ApiAnalysis analysis = new ApiAnalysis(baselineBundles, dependencyBundles, project.getName(), eclipseProject.getFile(fileToPath(apiFilter)), eclipseProject.getFile(fileToPath(apiPreferences)), fileToPath(project.getBasedir()), debug, fileToPath(project.getArtifact().getFile()), - stringToPath(project.getBuild().getOutputDirectory())); + stringToPath(project.getBuild().getOutputDirectory()), runAsJob); return eclipseFramework.execute(analysis); } catch (Exception e) { throw new MojoExecutionException("Execute ApiApplication failed", e);