Error while using Gradle Buildship nature in a custom Eclipse plugin

We have an Eclipse plugin that allows for creating Java projects using Gradle. We used the Gradle STS (Gradle IDE Pack) plugin to create a Gradle Java project in the past, but the Gradle STS plugin is no longer working in the latest Eclipse. So we are planning to use Gradle Buildship but when we are trying to add Gradle nature(org.eclipse.buildship.core.gradleprojectnature) to our Eclipse plugin code, it is giving below error while creating a Java project.

Missing Gradle project configuration file: .settings/org.eclipse.buildship.core.prefs

Also in eclipse .metadata/.log file, showing below error :

org.eclipse.buildship.core.internal.GradlePluginsRuntimeException: Can’t read root project location for project located at D:\Project\TestNewProject\TestThingworxProject

I would appreciate it if you could guide me on how to resolve this issue.

Can you first delete all .classpath and .projects files and all .settings folders of your root project and any sub-projects? Then import your project again: File > Import: Gradle > Existing Gradle Project

After that, look at the [Gradle Operations] Console in the Console View if there are any errors:

Or you could start with a minimal project generated with the Build Init Plugin and add missing pieces step by step. After a change you should refresh your project (right click on project: Gradle > Refresh Gradle Project) and check the console again.

I think our use case is not clear. We have our own eclipse plugin using which users can create Gradle projects. Here we do not want any manual actions for the users once the project is created. It should have all the dependencies without any need to refresh. See screenshot below for creating a new project using our plugin :

Here we are trying to use the Gradle BuildShip nature (org.eclipse.buildship.core.gradleprojectnature) in our custom Eclipse plugin code (Since the older Gradle STS plugin is no more found in eclipse marketplace). But when we generate Gradle based Java project then it shows Missing Gradle project configuration file: .settings/org.eclipse.buildship.core.prefs error.

We are looking for the way about adding the Gradle BuildShip nature in the plugin code. Please provide any references to the API or any other useful documentation that will help us resolve this issue.

I have already developed a similar plugin myself. I assume your plugin:

  1. Generates a Gradle build (settings.gradle, build.gradle, …) or checks out an existing project from source control (e.g. Git)
  2. Imports this build into the Eclipse Workspace with Eclipse Buildship

In step 1 no .classpath or .project files and no .settings folders should be written. These are automatically added in step 2 by Buildship.

A very basic handler for importing a Gradle build could look like this:

public class ImportHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        Job job = new Job("Import") {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                File rootDir = new File("/home/ole/data/workspace/demo");
                BuildConfigurationBuilder builder = BuildConfiguration.forRootProjectDirectory(rootDir);
                GradleBuild build = GradleCore.getWorkspace().createBuild(builder.build());
                build.synchronize(monitor);
                return Status.OK_STATUS;
            }
        };
        job.schedule();
        return null;
    }

}

To narrow down errors you should look at these places:

  1. Gradle Operations Console (in the Console View)
  2. Error Log (Window > Show View > Error Log)
  3. When you are running/debugging your plugin from a launch configuration: Console for the running Eclipse Application

The Javadoc for GradleBuild from org.eclipse.buildship.core is also a good starting point.

To help you further, you should share a minimal example that demonstrates your problem.

1 Like

@ oleosterhagen I have tried the solution you gave, but when we synchronize the build( build.synchronize(monitor)), it is giving the following warning and not able to synchronize project properly. I found this warning while debugging in this code CompatibilityChecker class (line no 55). Basically, it’s throwing an GradleConnectionException exception to that line.

Status WARNING: org.eclipse.buildship.core code=3 Project synchronization failed due to an error connecting to the Gradle build. org.gradle.tooling.GradleConnectionException: Could not fetch model of type 'BuildEnvironment' using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-8.1.1-bin.zip'.

If I manually use Gradle → Refresh Gradle Project after creating the project, it will sync the project properly without any warnings or exceptions in that line. So Is there anything I need to add to my custom Eclipse project or any other way to resolve this issue?

Are there no errors shown in the above mentioned places: Gradle Operations Console, Error Log, Console View?

Without sharing a small example it is difficult to help. Have you tried importing a minimal Gradle Build created by the Build Init Plugin with your custom Eclipse plugin?

@oleosterhagen I missed to notify you that I have resolved this issue. Actually, the main exception was coming as org.eclipse.swt.SWTException: Invalid thread access because this exception the above mentioned warning was coming. Basically this exception coming when listener code is called from outside the SWT Display thread. After investigating, I found that I forgot to add SubProgressMonitor and beginTask before gradle import logic in our custom eclipse plugin. After adding that code, the gradle project created and synced properly without any issues through our eclipse plugin.

Thank you so much for your help!! :slight_smile:

1 Like

I’m glad to read that you were able to solve the issue. Thank you for your reply!