diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java index a94f8c0a473b..3eb5568e4288 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java @@ -22,6 +22,7 @@ import com.intellij.openapi.projectRoots.*; import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; @@ -96,4 +97,41 @@ public class ExternalSystemJdkUtil { return null; } + + @Nullable + public static Pair getAvailableJdk(@Nullable Project project) throws ExternalSystemJdkException { + + if (project != null) { + Sdk res = ProjectRootManager.getInstance(project).getProjectSdk(); + if (res != null) return Pair.create(USE_PROJECT_JDK, res); + + Module[] modules = ModuleManager.getInstance(project).getModules(); + for (Module module : modules) { + Sdk sdk = ModuleRootManager.getInstance(module).getSdk(); + if (sdk != null && sdk.getSdkType() instanceof JavaSdkType) return Pair.create(USE_PROJECT_JDK, sdk); + } + } + + final String javaHome = System.getenv("JAVA_HOME"); + if (!StringUtil.isEmptyOrSpaces(javaHome) && (JdkUtil.checkForJdk(new File(javaHome)) || JdkUtil.checkForJre(javaHome))) { + final Sdk sdk = JavaSdk.getInstance().createJdk("", javaHome); + if (sdk != null) { + return Pair.create(USE_JAVA_HOME, sdk); + } + } + + for (Sdk projectJdk : ProjectJdkTable.getInstance().getAllJdks()) { + if (projectJdk.getHomePath() != null && + (JdkUtil.checkForJdk(new File(projectJdk.getHomePath())) || JdkUtil.checkForJre(projectJdk.getHomePath()))) { + return Pair.create(projectJdk.getName(), projectJdk); + } + } + + final Sdk internalJdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk(); + if (internalJdk != null) { + return Pair.create(USE_INTERNAL_JAVA, internalJdk); + } + + return null; + } } diff --git a/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemTestCase.java b/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemTestCase.java index 36bd84f772dd..56277f77f2b3 100644 --- a/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemTestCase.java +++ b/platform/external-system-impl/testSrc/com/intellij/openapi/externalSystem/test/ExternalSystemTestCase.java @@ -18,6 +18,7 @@ package com.intellij.openapi.externalSystem.test; import com.intellij.compiler.CompilerTestUtil; import com.intellij.compiler.artifacts.ArtifactsTestUtil; import com.intellij.compiler.impl.ModuleCompileScope; +import com.intellij.openapi.Disposable; import com.intellij.openapi.application.AccessToken; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.Result; @@ -33,17 +34,20 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl; import com.intellij.openapi.roots.ModuleRootModificationUtil; +import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.ByteSequence; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtilRt; import com.intellij.openapi.vfs.*; +import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess; import com.intellij.packaging.artifacts.Artifact; import com.intellij.packaging.impl.compiler.ArtifactCompileScope; import com.intellij.testFramework.*; import com.intellij.testFramework.fixtures.IdeaProjectTestFixture; import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory; +import com.intellij.util.ArrayUtil; import com.intellij.util.io.TestFileSystemItem; import gnu.trove.THashSet; import org.jetbrains.annotations.NonNls; @@ -80,6 +84,8 @@ public abstract class ExternalSystemTestCase extends UsefulTestCase { protected VirtualFile myProjectConfig; protected List myAllConfigs = new ArrayList(); + private List myAllowedRoots = new ArrayList(); + static { IdeaTestCase.initPlatformPrefix(); } @@ -118,6 +124,32 @@ public abstract class ExternalSystemTestCase extends UsefulTestCase { }); } }); + + ArrayList allowedRoots = new ArrayList(); + collectAllowedRoots(allowedRoots); + registerAllowedRoots(allowedRoots, myTestRootDisposable); + + CompilerTestUtil.enableExternalCompiler(); + } + + protected void collectAllowedRoots(List roots) throws IOException { + } + + public void registerAllowedRoots(List roots, @NotNull Disposable disposable) { + final List newRoots = new ArrayList(roots); + newRoots.removeAll(myAllowedRoots); + + final String[] newRootsArray = ArrayUtil.toStringArray(newRoots); + VfsRootAccess.allowRootAccess(newRootsArray); + myAllowedRoots.addAll(newRoots); + + Disposer.register(disposable, new Disposable() { + @Override + public void dispose() { + VfsRootAccess.disallowRootAccess(newRootsArray); + myAllowedRoots.removeAll(newRoots); + } + }); } private void ensureTempDirCreated() throws IOException { diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleProjectImportBuilder.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleProjectImportBuilder.java index 93955a334d5b..8968d230f10f 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleProjectImportBuilder.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/project/wizard/GradleProjectImportBuilder.java @@ -19,12 +19,14 @@ import com.intellij.externalSystem.JavaProjectData; import com.intellij.ide.util.projectWizard.WizardContext; import com.intellij.openapi.externalSystem.model.DataNode; import com.intellij.openapi.externalSystem.model.project.ProjectData; +import com.intellij.openapi.externalSystem.service.execution.ExternalSystemJdkUtil; import com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager; import com.intellij.openapi.externalSystem.service.project.wizard.AbstractExternalProjectImportBuilder; import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.*; import com.intellij.openapi.roots.LanguageLevelProjectExtension; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.pom.java.LanguageLevel; @@ -67,7 +69,13 @@ public class GradleProjectImportBuilder extends AbstractExternalProjectImportBui if (file != null && file.isDirectory()) { pathToUse = new File(pathToUse, GradleConstants.DEFAULT_SCRIPT_NAME).getAbsolutePath(); } - getControl(context.getProject()).setLinkedProjectPath(pathToUse); + + final ImportFromGradleControl importFromGradleControl = getControl(context.getProject()); + importFromGradleControl.setLinkedProjectPath(pathToUse); + final Pair sdkPair = ExternalSystemJdkUtil.getAvailableJdk(getCurrentProject()); + if (sdkPair != null) { + importFromGradleControl.getProjectSettings().setGradleJvm(sdkPair.first); + } } @Override diff --git a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleImportingTestCase.java b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleImportingTestCase.java index 2ca987e8ebef..867bdb12fd40 100644 --- a/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleImportingTestCase.java +++ b/plugins/gradle/testSources/org/jetbrains/plugins/gradle/importing/GradleImportingTestCase.java @@ -16,17 +16,24 @@ package org.jetbrains.plugins.gradle.importing; import com.intellij.compiler.server.BuildManager; +import com.intellij.openapi.application.PathManager; import com.intellij.openapi.application.Result; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.externalSystem.model.ProjectSystemId; import com.intellij.openapi.externalSystem.model.settings.ExternalSystemExecutionSettings; +import com.intellij.openapi.externalSystem.service.execution.ExternalSystemJdkUtil; import com.intellij.openapi.externalSystem.settings.ExternalProjectSettings; +import com.intellij.openapi.externalSystem.settings.ExternalSystemSettingsListenerAdapter; import com.intellij.openapi.externalSystem.test.ExternalSystemImportingTestCase; +import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil; +import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.TestDialog; +import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.containers.ContainerUtil; import org.gradle.util.GradleVersion; import org.gradle.wrapper.GradleWrapperMain; import org.intellij.lang.annotations.Language; @@ -49,6 +56,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Properties; import static org.jetbrains.plugins.gradle.tooling.builder.AbstractModelBuilderTest.DistributionLocator; @@ -93,6 +101,16 @@ public abstract class GradleImportingTestCase extends ExternalSystemImportingTes } } + @Override + protected void collectAllowedRoots(List roots) throws IOException { + final String javaHome = System.getenv("JAVA_HOME"); + if (javaHome != null) { + roots.add(javaHome); + } + + roots.add(PathManager.getOptionsPath()); + } + @Override public String getName() { return name.getMethodName() == null ? super.getName() : FileUtil.sanitizeFileName(name.getMethodName()); @@ -115,6 +133,18 @@ public abstract class GradleImportingTestCase extends ExternalSystemImportingTes @Override protected void importProject(@NonNls @Language("Groovy") String config) throws IOException { + ExternalSystemApiUtil.subscribe(myProject, GradleConstants.SYSTEM_ID, new ExternalSystemSettingsListenerAdapter() { + @Override + public void onProjectsLinked(@NotNull Collection settings) { + final Object item = ContainerUtil.getFirstItem(settings); + if (item instanceof GradleProjectSettings) { + final Pair availableJdk = ExternalSystemJdkUtil.getAvailableJdk(myProject); + if (availableJdk != null) { + ((GradleProjectSettings)item).setGradleJvm(availableJdk.first); + } + } + } + }); super.importProject(config); }