Skip to content

Commit f784331

Browse files
committed
Add new option to API tools verify goal to runAsJob
This allows to configure if the analysis should run as a workspace job.
1 parent dfab8b7 commit f784331

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ public class ApiAnalysis implements Serializable, Callable<ApiAnalysisResult> {
103103
private String apiPreferences;
104104
private String binaryArtifact;
105105
private String outputDir;
106+
private boolean runAsJob;
106107

107108
ApiAnalysis(Collection<Path> baselineBundles, Collection<Path> dependencyBundles, String baselineName,
108109
Path apiFilterFile, Path apiPreferences, Path projectDir, boolean debug, Path binaryArtifact,
109-
Path outputDir) {
110+
Path outputDir, boolean runAsJob) {
111+
this.runAsJob = runAsJob;
110112
this.targetBundles = dependencyBundles.stream().map(ApiAnalysis::pathAsString).toList();
111113
this.baselineBundles = baselineBundles.stream().map(ApiAnalysis::pathAsString).toList();
112114
this.baselineName = baselineName;
@@ -163,45 +165,23 @@ public void aboutToRun(IJobChangeEvent event) {
163165
IPath projectPath = IPath.fromOSString(projectDir);
164166
IProject project = getProject(projectPath);
165167
ApiAnalysisResult result = new ApiAnalysisResult(getVersion());
166-
WorkspaceJob job = new WorkspaceJob("Tycho API Analysis") {
168+
IStatus status;
169+
if (runAsJob) {
170+
WorkspaceJob job = new WorkspaceJob("Tycho API Analysis") {
167171

168-
@Override
169-
public IStatus runInWorkspace(IProgressMonitor monitor) {
170-
try {
171-
BundleComponent projectComponent = getApiComponent(project, projectPath);
172-
IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline");
173-
ResolverError[] resolverErrors = projectComponent.getErrors();
174-
if (resolverErrors != null && resolverErrors.length > 0) {
175-
for (ResolverError error : resolverErrors) {
176-
result.addResolverError(error);
177-
}
178-
}
179-
IApiFilterStore filterStore = getApiFilterStore(projectComponent);
180-
Properties preferences = getPreferences();
181-
BaseApiAnalyzer analyzer = new BaseApiAnalyzer();
182-
try {
183-
analyzer.setContinueOnResolverError(true);
184-
analyzer.analyzeComponent(null, filterStore, preferences, baseline, projectComponent,
185-
new BuildContext(), new NullProgressMonitor());
186-
IApiProblem[] problems = analyzer.getProblems();
187-
for (IApiProblem problem : problems) {
188-
result.addProblem(problem, project);
189-
debug(String.valueOf(problem));
190-
}
191-
} finally {
192-
analyzer.dispose();
193-
ResourcesPlugin.getWorkspace().save(true, new NullProgressMonitor());
194-
}
195-
} catch (Exception e) {
196-
return Status.error("Api Analysis failed", e);
172+
@Override
173+
public IStatus runInWorkspace(IProgressMonitor monitor) {
174+
return performAPIAnalysis(project, projectPath, result);
197175
}
198-
return Status.OK_STATUS;
199-
}
200-
};
201-
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
202-
job.schedule();
203-
job.join();
204-
IStatus status = job.getResult();
176+
177+
};
178+
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
179+
job.schedule();
180+
job.join();
181+
status = job.getResult();
182+
} else {
183+
status = performAPIAnalysis(project, projectPath, result);
184+
}
205185
JRTUtil.reset(); // reclaim space due to loaded multiple JRTUtil should better be fixed to not
206186
// use that much space
207187
if (!status.isOK() && status.getException() instanceof Exception error) {
@@ -210,6 +190,38 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
210190
return result;
211191
}
212192

193+
private IStatus performAPIAnalysis(IProject project, IPath projectPath, ApiAnalysisResult result) {
194+
try {
195+
BundleComponent projectComponent = getApiComponent(project, projectPath);
196+
IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline");
197+
ResolverError[] resolverErrors = projectComponent.getErrors();
198+
if (resolverErrors != null && resolverErrors.length > 0) {
199+
for (ResolverError error : resolverErrors) {
200+
result.addResolverError(error);
201+
}
202+
}
203+
IApiFilterStore filterStore = getApiFilterStore(projectComponent);
204+
Properties preferences = getPreferences();
205+
BaseApiAnalyzer analyzer = new BaseApiAnalyzer();
206+
try {
207+
analyzer.setContinueOnResolverError(true);
208+
analyzer.analyzeComponent(null, filterStore, preferences, baseline, projectComponent,
209+
new BuildContext(), new NullProgressMonitor());
210+
IApiProblem[] problems = analyzer.getProblems();
211+
for (IApiProblem problem : problems) {
212+
result.addProblem(problem, project);
213+
debug(String.valueOf(problem));
214+
}
215+
} finally {
216+
analyzer.dispose();
217+
ResourcesPlugin.getWorkspace().save(true, new NullProgressMonitor());
218+
}
219+
} catch (Exception e) {
220+
return Status.error("Api Analysis failed", e);
221+
}
222+
return Status.OK_STATUS;
223+
}
224+
213225
private String getVersion() {
214226
Bundle apiToolsBundle = FrameworkUtil.getBundle(ApiModelFactory.class);
215227
if (apiToolsBundle != null) {

tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysisMojo.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ public class ApiAnalysisMojo extends AbstractMojo {
141141
@Parameter(defaultValue = "false", property = "tycho.apitools.enhanceLogs")
142142
private boolean enhanceLogs;
143143

144+
/**
145+
* Configures if the API Analysis should run as a workspace job, this ensure
146+
* that no other actions are allowed to run in parallel what sometimes can
147+
* result in failures to execute the api-analysis
148+
*/
149+
@Parameter(defaultValue = "true", property = "tycho.apitools.runAsJob")
150+
private boolean runAsJob;
151+
144152
@Component
145153
private EclipseWorkspaceManager workspaceManager;
146154

@@ -350,7 +358,7 @@ private ApiAnalysisResult performAnalysis(Collection<Path> baselineBundles, Coll
350358
ApiAnalysis analysis = new ApiAnalysis(baselineBundles, dependencyBundles, project.getName(),
351359
eclipseProject.getFile(fileToPath(apiFilter)), eclipseProject.getFile(fileToPath(apiPreferences)),
352360
fileToPath(project.getBasedir()), debug, fileToPath(project.getArtifact().getFile()),
353-
stringToPath(project.getBuild().getOutputDirectory()));
361+
stringToPath(project.getBuild().getOutputDirectory()), runAsJob);
354362
return eclipseFramework.execute(analysis);
355363
} catch (Exception e) {
356364
throw new MojoExecutionException("Execute ApiApplication failed", e);

0 commit comments

Comments
 (0)