From 548ec8dfe933752378d6c86d74b95b8af790e92c Mon Sep 17 00:00:00 2001 From: Ole Osterhagen <76391368+oleosterhagen@users.noreply.github.com> Date: Mon, 15 Jan 2024 16:33:13 +0100 Subject: [PATCH] Prevent execution of the Java Resource Import Configurator #1288 (#1293) When importing Gradle projects from a folder with the generic project configurator, other configurators like the ProjectWithJavaResourcesImportConfigurator must not run. Otherwise resources in subprojects could be imported twice. To prevent the execution of secondary configurators the method shouldBeAnEclipseProject(...) has to return true for the root of Gradle projects. --- .../Launch Buildship UI tests.launch | 2 +- .../project/ProjectImportWizardUiTest.groovy | 39 +++++++++++++++++++ .../project/GradleProjectConfigurator.java | 9 ++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/org.eclipse.buildship.ui.test/Launch Buildship UI tests.launch b/org.eclipse.buildship.ui.test/Launch Buildship UI tests.launch index 9cbf78cae6..c9a6d6ce1e 100644 --- a/org.eclipse.buildship.ui.test/Launch Buildship UI tests.launch +++ b/org.eclipse.buildship.ui.test/Launch Buildship UI tests.launch @@ -26,7 +26,7 @@ - + diff --git a/org.eclipse.buildship.ui.test/src/main/groovy/org/eclipse/buildship/ui/internal/wizard/project/ProjectImportWizardUiTest.groovy b/org.eclipse.buildship.ui.test/src/main/groovy/org/eclipse/buildship/ui/internal/wizard/project/ProjectImportWizardUiTest.groovy index 520f01213d..63b71a10a6 100644 --- a/org.eclipse.buildship.ui.test/src/main/groovy/org/eclipse/buildship/ui/internal/wizard/project/ProjectImportWizardUiTest.groovy +++ b/org.eclipse.buildship.ui.test/src/main/groovy/org/eclipse/buildship/ui/internal/wizard/project/ProjectImportWizardUiTest.groovy @@ -11,6 +11,8 @@ package org.eclipse.buildship.ui.internal.wizard.project import org.eclipse.core.resources.IProject import org.eclipse.core.runtime.IProgressMonitor +import org.eclipse.core.runtime.jobs.Job +import org.eclipse.jdt.core.JavaCore import org.eclipse.jface.dialogs.IDialogConstants import org.eclipse.swtbot.eclipse.finder.waits.Conditions import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell @@ -72,6 +74,28 @@ class ProjectImportWizardUiTest extends SwtBotSpecification { bot.button("Cancel").click() } + def "import project from folder does not include Java resources of subprojects in the root project"() { + given: + def rootDir = dir('rootProject') { + file 'settings.gradle', 'include "subProject"' + dir('subProject') { + file 'build.gradle', 'plugins { id "java-library" }' + dir('src/main/java') { + file 'Foobar.java', 'class Foobar { }' + } + } + } + + when: + importProjectFromFolder rootDir + waitForImportJobsToFinish() + waitForGradleJobsToFinish() + + then: + findProject('rootProject').hasNature(JavaCore.NATURE_ID) == false + findProject('subProject').hasNature(JavaCore.NATURE_ID) == true + } + private static SWTBotShell openGradleImportWizard() { bot.menu("File").menu("Import...").click() SWTBotShell shell = bot.shell("Import") @@ -82,6 +106,21 @@ class ProjectImportWizardUiTest extends SwtBotSpecification { bot.shell(ProjectWizardMessages.Title_GradleProjectWizardPage) } + private void importProjectFromFolder(File projectDir) { + bot.menu("File").menu("Import...").click() + bot.shell("Import").activate() + bot.waitUntil(Conditions.shellIsActive("Import")) + bot.tree().expandNode("General").select("Projects from Folder or Archive") + bot.button("Next >").click() + bot.comboBoxWithLabel("Import source:").setText(projectDir.canonicalPath) + bot.button(IDialogConstants.FINISH_LABEL).click() + } + + private void waitForImportJobsToFinish() { + def jobFamily = Class.forName("org.eclipse.ui.internal.wizards.datatransfer.SmartImportJob") + Job.getJobManager().join(jobFamily, null) + } + class FaultyWorkspaceOperations { @Delegate WorkspaceOperations delegate = CorePlugin.workspaceOperations() diff --git a/org.eclipse.buildship.ui/src/main/java/org/eclipse/buildship/ui/internal/project/GradleProjectConfigurator.java b/org.eclipse.buildship.ui/src/main/java/org/eclipse/buildship/ui/internal/project/GradleProjectConfigurator.java index a4dbaf2230..588100b712 100644 --- a/org.eclipse.buildship.ui/src/main/java/org/eclipse/buildship/ui/internal/project/GradleProjectConfigurator.java +++ b/org.eclipse.buildship.ui/src/main/java/org/eclipse/buildship/ui/internal/project/GradleProjectConfigurator.java @@ -78,7 +78,8 @@ private boolean isGradleRoot(File root) { @Override public boolean shouldBeAnEclipseProject(IContainer container, IProgressMonitor monitor) { - return false; + IPath location = container.getLocation(); + return location != null && isGradleRoot(location.toFile()); } @Override @@ -88,11 +89,7 @@ public Set getFoldersToIgnore(IProject project, IProgressMonitor monito @Override public boolean canConfigure(IProject project, Set ignoredPaths, IProgressMonitor monitor) { - IPath location = project.getLocation(); - if(location != null) { - return isGradleRoot(location.toFile()); - } - return false; + return shouldBeAnEclipseProject(project, monitor); } @Override