@Nullable
  public static BuildEnvironment getBuildEnvironment(@NotNull ProjectConnection connection,
                                                     @NotNull ExternalSystemTaskId taskId,
                                                     @NotNull ExternalSystemTaskNotificationListener listener,
                                                     @Nullable CancellationToken cancellationToken,
                                                     @Nullable GradleExecutionSettings settings) {
    Span span = ExternalSystemTelemetryUtil.getTracer(GradleConstants.SYSTEM_ID)
      .spanBuilder("GetBuildEnvironment")
      .startSpan();
    try (Scope ignore = span.makeCurrent()) {
      BuildEnvironment buildEnvironment = null;
      try {
        ModelBuilder<BuildEnvironment> modelBuilder = connection.model(BuildEnvironment.class);
        if (cancellationToken != null) {
          modelBuilder.withCancellationToken(cancellationToken);
        }
        if (settings != null) {
          final String javaHome = settings.getJavaHome();
          if (javaHome != null && new File(javaHome).isDirectory()) {
            modelBuilder.setJavaHome(new File(javaHome));
          }
        }
        // do not use connection.getModel methods since it doesn't allow to handle progress events
        // and we can miss gradle tooling client side events like distribution download.
        GradleProgressListener gradleProgressListener = new GradleProgressListener(listener, taskId);
        modelBuilder.addProgressListener((ProgressListener)gradleProgressListener);
        modelBuilder.addProgressListener((org.gradle.tooling.events.ProgressListener)gradleProgressListener);
        modelBuilder.setStandardOutput(new OutputWrapper(listener, taskId, true));
        modelBuilder.setStandardError(new OutputWrapper(listener, taskId, false));

        buildEnvironment = modelBuilder.get();
      }
      catch (Throwable t) {
        span.recordException(t);
        span.setStatus(StatusCode.ERROR);
        LOG.warn("Failed to obtain build environment from Gradle daemon.", t);
      }
      if (buildEnvironment != null) {
        checkThatGradleBuildEnvironmentIsSupportedByIdea(buildEnvironment);
      }
      return buildEnvironment;
    }
    finally {
      span.end();
    }
  }
@Nullable
  public static BuildEnvironment getBuildEnvironment(@NotNull ProjectConnection connection,
                                                     @NotNull ExternalSystemTaskId taskId,
                                                     @NotNull ExternalSystemTaskNotificationListener listener,
                                                     @Nullable CancellationToken cancellationToken,
                                                     @Nullable GradleExecutionSettings settings)