Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option to API tools verify goal to runAsJob #4405

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ public class ApiAnalysis implements Serializable, Callable<ApiAnalysisResult> {
private String apiPreferences;
private String binaryArtifact;
private String outputDir;
private boolean runAsJob;

ApiAnalysis(Collection<Path> baselineBundles, Collection<Path> 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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -350,7 +358,7 @@ private ApiAnalysisResult performAnalysis(Collection<Path> 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);
Expand Down
Loading