Hi,
I want to ignore the Gradle build dir from some expensive resource processing in Eclipse. Therefore I need to find the build dir for a given IProject. This code will be run with a high frequency.
I tried several things and wonder if there is a solution that works in Buildship 2.x and 3.x. So far I did only find a way to achieve this either in 2.x or in 3.x but no reflection-free code did work for both versions.
-
With 3.x I can use GradleCore.getWorkspace and friends to figure the build dir. At a first glance the implementation appears to be reasonably simple, but I’m a little concerned about the runtime cost. As far as I saw it does not use any caching, e.g. it does not use
o.e.b.c.internal.preferences.PersistentModel.getBuildDir()
Optional<GradleBuild> gradleBuild = GradleCore.getWorkspace().getBuild(iProject); if (gradleBuild.isPresent()) { GradleProject gradleProject = gradleBuild.get().withConnection(connection->connection.getModel(GradleProject.class), new NullProgressMonitor()); File buildDirectory = gradleProject.getBuildDirectory(); .. }
-
Thinking this through, I came across the ProjectConfigurator extension point. Maybe this is the right thing to do to the caching of the build dir per project. Unfortunately it is only available in 3.x and not in 2.x. Also I assume that my code will now do something that is already available internally, e.g. via the persistent model.
IProject project = context.getProject(); Optional<GradleBuild> gradleBuild = GradleCore.getWorkspace().getBuild(project); if (gradleBuild.isPresent()) { try { GradleProject gradleProject = gradleBuild.get().withConnection( connection -> connection.getModel(GradleProject.class), new NullProgressMonitor()); File buildDirectory = gradleProject.getBuildDirectory(); File projectDirectory = gradleProject.getProjectDirectory(); IPath relativePath = Path.fromOSString(projectDirectory.toPath().relativize(buildDirectory.toPath()).toString()); IFolder buildDirAsFolder = project.getFolder(relativePath); buildDirs.add(buildDirAsFolder); buildDirsPerProject.put(project, buildDirAsFolder); } catch (Exception e) { e.printStackTrace(); } }
-
The internal API of the persistent model was extremely straight forward but well … internal
PersistentModel gradleModel = CorePlugin.modelPersistence().loadModel(folder.getProject()); if (gradleModel.isPresent()) { IPath buildDir = gradleModel.getBuildDir(); .. }
With 2.x I’d have to do something else I suppose.
Long story short: What would be the recommend way to do this for 3.x and is there a solution that works for both major versions of Buildship?
Best
Sebastian