diff --git a/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF b/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF index c3aef70e28..5940563702 100644 --- a/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF @@ -1,7 +1,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.m2e.core.ui;singleton:=true -Bundle-Version: 2.0.7.qualifier +Bundle-Version: 2.0.8.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-17 Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java index c6cf4ce7e3..d53592a15f 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java @@ -557,7 +557,7 @@ public class Messages extends NLS { public static String MavenRepositoryView_update_one; - public static String MavenSettingsPreferencePage_userSettingsBrowseButton_text; + public static String MavenSettingsPreferencePage_settingsBrowseButton_text; public static String MavenSettingsPreferencePage_btnUpdate; @@ -569,6 +569,10 @@ public class Messages extends NLS { public static String MavenSettingsPreferencePage_error_userSettingsParse; + public static String MavenSettingsPreferencePage_error_userToolchainsMissing; + + public static String MavenSettingsPreferencePage_error_userToolchainsParse; + public static String MavenSettingsPreferencePage_job_indexing; public static String MavenSettingsPreferencePage_job_updating; @@ -603,6 +607,12 @@ public class Messages extends NLS { public static String MavenWarningsPreferencePage_notCoveredMojoExecution; + public static String MavenSettingsPreferencePage_userToolchainslink1; + + public static String MavenSettingsPreferencePage_userToolchainslink2; + + public static String MavenSettingsPreferencePage_userToolchainslink_tooltip; + public static String MavenWarningsPreferencePage_versionDupParent; public static String MavenWarningsPreferencePage_overridingManagedPreferences; @@ -1025,8 +1035,6 @@ public class Messages extends NLS { public static String MavenInstallationWizardPage_selectProjectTitle; - public static String MavenSettingsPreferencePage_globalSettingsBrowseButton_text; - public static String MavenProjectWizardArchetypeParametersPage_runInteractive; static { diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties index 6b5cd1ee1f..b88dee250c 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties @@ -309,19 +309,23 @@ MavenRepositoryView_reload_msg=This will reload the settings.xml and rebuild the MavenRepositoryView_reload_title=Reload settings.xml MavenRepositoryView_update_more=Update Indexes MavenRepositoryView_update_one=Update Index -MavenSettingsPreferencePage_userSettingsBrowseButton_text=Browse... +MavenSettingsPreferencePage_settingsBrowseButton_text=Browse... MavenSettingsPreferencePage_btnUpdate=Update Settings MavenSettingsPreferencePage_error_globalSettingsMissing=Global settings file doesn't exist MavenSettingsPreferencePage_error_userSettingsMissing=User settings file doesn't exist MavenSettingsPreferencePage_error_globalSettingsParse=Unable to parse global settings file {0} MavenSettingsPreferencePage_error_userSettingsParse=Unable to parse user settings file {0} -MavenSettingsPreferencePage_globalSettingsBrowseButton_text=Browse... +MavenSettingsPreferencePage_error_userToolchainsMissing=User toolchains file doesn't exist +MavenSettingsPreferencePage_error_userToolchainsParse=Unable to parse user toolchains file {0} MavenSettingsPreferencePage_job_indexing=Indexing Local Repository... MavenSettingsPreferencePage_job_updating=Updating Maven settings MavenSettingsPreferencePage_lblLocal=Local Repository (From merged user and global settings)\: MavenSettingsPreferencePage_userSettingslink1=User &Settings\: MavenSettingsPreferencePage_userSettingslink2=User &Settings (open file)\: MavenSettingsPreferencePage_userSettingslink_tooltip=Open editor for user settings +MavenSettingsPreferencePage_userToolchainslink1=User &Toolchains\: +MavenSettingsPreferencePage_userToolchainslink2=User &Toolchains (open file)\: +MavenSettingsPreferencePage_userToolchainslink_tooltip=Open editor for user toolchains MavenSettingsPreferencePage_globalSettingslink1=Global &Settings\: MavenSettingsPreferencePage_globalSettingslink2=Global &Settings (open file)\: MavenSettingsPreferencePage_globalSettingslink_tooltip=Open editor for global settings diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenSettingsPreferencePage.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenSettingsPreferencePage.java index 55787e906a..a31a766017 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenSettingsPreferencePage.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenSettingsPreferencePage.java @@ -17,6 +17,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,7 +36,6 @@ import org.eclipse.jface.preference.PreferencePage; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; @@ -55,6 +58,8 @@ import org.eclipse.ui.ide.FileStoreEditorInput; import org.eclipse.ui.ide.IDE; +import org.apache.maven.building.Problem; +import org.apache.maven.cli.MavenCli; import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor; import org.apache.maven.repository.RepositorySystem; import org.apache.maven.settings.Settings; @@ -63,6 +68,7 @@ import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.embedder.IMaven; import org.eclipse.m2e.core.embedder.IMavenConfiguration; +import org.eclipse.m2e.core.internal.IMavenToolbox; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.project.MavenUpdateRequest; import org.eclipse.m2e.core.ui.internal.Messages; @@ -92,6 +98,10 @@ public class MavenSettingsPreferencePage extends PreferencePage implements IWork private Link userSettingsLink; + private Link userToolchainsLink; + + private Text userToolchainsText; + public MavenSettingsPreferencePage() { setTitle(Messages.MavenSettingsPreferencePage_title); @@ -116,13 +126,13 @@ protected void updateSettings(boolean updateMavenDependencies) { updateLocalRepository(); String userSettings = getUserSettings(); + String userToolchains = getUserToolchains(); String globalSettings = getGlobalSettings(); - String currentGlobalSettings = mavenConfiguration.getGlobalSettingsFile(); - String currentUserSettings = mavenConfiguration.getUserSettingsFile(); - - if(Objects.equals(globalSettings, currentGlobalSettings) && Objects.equals(currentUserSettings, userSettings)) { - return; + if(Objects.equals(globalSettings, mavenConfiguration.getGlobalSettingsFile()) + && Objects.equals(userSettings, mavenConfiguration.getUserSettingsFile()) + && Objects.equals(userToolchains, mavenConfiguration.getUserToolchainsFile())) { + return; // current preferences not changed } Boolean[] updateProjects = new Boolean[1]; @@ -141,6 +151,7 @@ protected void updateSettings(boolean updateMavenDependencies) { // this clears cached settings.xml instance mavenConfiguration.setGlobalSettingsFile(globalSettings); mavenConfiguration.setUserSettingsFile(userSettings); + mavenConfiguration.setUserToolchainsFile(userToolchains); if(Boolean.TRUE.equals(updateProjects[0])) { List projects = MavenPlugin.getMavenProjectRegistry().getProjects(); @@ -187,50 +198,27 @@ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); composite.setLayout(new GridLayout(2, false)); - globalSettingsLink = new Link(composite, SWT.NONE); - globalSettingsLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); - globalSettingsLink.setText(Messages.MavenSettingsPreferencePage_globalSettingslink2); - globalSettingsLink.setToolTipText(Messages.MavenSettingsPreferencePage_globalSettingslink_tooltip); - globalSettingsLink.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { - String globalSettings = getGlobalSettings(); - if(globalSettings != null) { - openEditor(globalSettings); - } - })); + globalSettingsLink = createLink(composite, Messages.MavenSettingsPreferencePage_globalSettingslink2, + Messages.MavenSettingsPreferencePage_globalSettingslink_tooltip, this::getGlobalSettings, null); + globalSettingsText = createFileSelectionWidgets(composite, mavenConfiguration.getGlobalSettingsFile(), null); - globalSettingsText = new Text(composite, SWT.BORDER); - globalSettingsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - - Button globalSettingsBrowseButton = new Button(composite, SWT.NONE); - globalSettingsBrowseButton.setText(Messages.MavenSettingsPreferencePage_globalSettingsBrowseButton_text); - globalSettingsBrowseButton - .addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(globalSettingsText))); - - userSettingsLink = new Link(composite, SWT.NONE); - userSettingsLink.setText(Messages.MavenSettingsPreferencePage_userSettingslink2); - userSettingsLink.setToolTipText(Messages.MavenSettingsPreferencePage_userSettingslink_tooltip); - userSettingsLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); - userSettingsLink.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { - String userSettings = getUserSettings(); - if(userSettings == null) { - userSettings = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath(); - } - openEditor(userSettings); - })); - userSettingsText = new Text(composite, SWT.BORDER); - userSettingsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - userSettingsText.setMessage(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath()); + userSettingsLink = createLink(composite, Messages.MavenSettingsPreferencePage_userSettingslink2, + Messages.MavenSettingsPreferencePage_userSettingslink_tooltip, this::getUserSettings, + SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE); + userSettingsText = createFileSelectionWidgets(composite, mavenConfiguration.getUserSettingsFile(), + SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE); - Button userSettingsBrowseButton = new Button(composite, SWT.NONE); - userSettingsBrowseButton.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1)); - userSettingsBrowseButton.setText(Messages.MavenSettingsPreferencePage_userSettingsBrowseButton_text); - userSettingsBrowseButton - .addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(userSettingsText))); + userToolchainsLink = createLink(composite, Messages.MavenSettingsPreferencePage_userToolchainslink2, + Messages.MavenSettingsPreferencePage_userToolchainslink_tooltip, this::getUserToolchains, + MavenCli.DEFAULT_USER_TOOLCHAINS_FILE); + userToolchainsText = createFileSelectionWidgets(composite, mavenConfiguration.getUserToolchainsFile(), + MavenCli.DEFAULT_USER_TOOLCHAINS_FILE); Button updateSettings = new Button(composite, SWT.NONE); updateSettings.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1)); updateSettings.setText(Messages.MavenSettingsPreferencePage_btnUpdate); updateSettings.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> updateSettings(true))); + Label localRepositoryLabel = new Label(composite, SWT.NONE); GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1); gd.verticalIndent = 25; @@ -242,48 +230,52 @@ protected Control createContents(Composite parent) { localRepositoryText.setData("name", "localRepositoryText"); //$NON-NLS-1$ //$NON-NLS-2$ localRepositoryText.setEditable(false); - ModifyListener settingsModifyListener = modifyevent -> { - updateLocalRepository(); - checkSettings(); - }; - userSettingsText.addModifyListener(settingsModifyListener); - globalSettingsText.addModifyListener(settingsModifyListener); - - String globalSettings = mavenConfiguration.getGlobalSettingsFile(); - if(globalSettings != null) { - globalSettingsText.setText(globalSettings); - } - String userSettings = mavenConfiguration.getUserSettingsFile(); - if(userSettings != null) { - userSettingsText.setText(userSettings); - } checkSettings(); updateLocalRepository(); return composite; } - private void updateUserSettingsLink(String userSettings) { - File userSettingsFile = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE; - if(userSettings != null) { - userSettingsFile = new File(userSettings); - } - boolean active = userSettingsFile.canRead(); + private Link createLink(Composite composite, String text, String tooltip, Supplier selectedFile, + File defaultFile) { + Link link = new Link(composite, SWT.NONE); + link.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); + link.setText(text); + link.setToolTipText(tooltip); + link.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { + File file = Optional.ofNullable(selectedFile.get()).map(File::new).orElse(defaultFile); + if(file != null) { + openEditor(file); + } + })); + return link; + } - String text = Messages.MavenSettingsPreferencePage_userSettingslink1; - if(active) { - text = Messages.MavenSettingsPreferencePage_userSettingslink2; + private Text createFileSelectionWidgets(Composite composite, String selectedFile, File defaultFile) { + Text fileText = new Text(composite, SWT.BORDER); + fileText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); + if(defaultFile != null) { + fileText.setMessage(defaultFile.getAbsolutePath()); + } + if(selectedFile != null) { + fileText.setText(selectedFile); } - userSettingsLink.setText(text); + fileText.addModifyListener(modifyevent -> { + updateLocalRepository(); + checkSettings(); + }); + + Button browseButton = new Button(composite, SWT.NONE); + browseButton.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1)); + browseButton.setText(Messages.MavenSettingsPreferencePage_settingsBrowseButton_text); + browseButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(fileText))); + return fileText; } - private void updateGlobalSettingsLink(String globalSettings) { - boolean active = globalSettings != null && new File(globalSettings).canRead(); - String text = Messages.MavenSettingsPreferencePage_globalSettingslink1; - if(active) { - text = Messages.MavenSettingsPreferencePage_globalSettingslink2; - } - globalSettingsLink.setText(text); + private void updateLink(Link link, String path, File defaultFile, String activeText, String inactiveText) { + File file = path != null ? new File(path) : defaultFile; + boolean active = file != null && file.canRead(); + link.setText(active ? activeText : inactiveText); } protected void updateLocalRepository() { @@ -310,42 +302,51 @@ protected void checkSettings() { // NB: enable/disable links regardless of validation errors String globalSettings = getGlobalSettings(); - updateGlobalSettingsLink(globalSettings); + updateLink(globalSettingsLink, globalSettings, null, Messages.MavenSettingsPreferencePage_globalSettingslink2, + Messages.MavenSettingsPreferencePage_globalSettingslink1); String userSettings = getUserSettings(); - updateUserSettingsLink(userSettings); - - if(globalSettings != null - && !checkSettings(globalSettings, Messages.MavenSettingsPreferencePage_error_globalSettingsMissing, - Messages.MavenSettingsPreferencePage_error_globalSettingsParse)) { - //work is done in if-condition - } else if(userSettings != null) { - checkSettings(userSettings, Messages.MavenSettingsPreferencePage_error_userSettingsMissing, - Messages.MavenSettingsPreferencePage_error_userSettingsParse); - } + updateLink(userSettingsLink, userSettings, SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE, + Messages.MavenSettingsPreferencePage_userSettingslink2, Messages.MavenSettingsPreferencePage_userSettingslink1); + + String userToolchains = getUserToolchains(); + updateLink(userToolchainsLink, userToolchains, MavenCli.DEFAULT_USER_TOOLCHAINS_FILE, + Messages.MavenSettingsPreferencePage_userToolchainslink2, + Messages.MavenSettingsPreferencePage_userToolchainslink1); + + setMessage(null); + checkSettings(globalSettings, Messages.MavenSettingsPreferencePage_error_globalSettingsMissing, + l -> maven.validateSettings(l).stream().map(SettingsProblem::getMessage), + Messages.MavenSettingsPreferencePage_error_globalSettingsParse); + checkSettings(userSettings, Messages.MavenSettingsPreferencePage_error_userSettingsMissing, + l -> maven.validateSettings(l).stream().map(SettingsProblem::getMessage), + Messages.MavenSettingsPreferencePage_error_userSettingsParse); + checkSettings(userToolchains, Messages.MavenSettingsPreferencePage_error_userToolchainsMissing, + l -> IMavenToolbox.of(maven).validateToolchains(l).stream().map(Problem::getMessage), + Messages.MavenSettingsPreferencePage_error_userToolchainsParse); } - private boolean checkSettings(String location, String errorMissing, String errorParse) { - if(!new File(location).canRead()) { - setMessage(errorMissing, IMessageProvider.WARNING); - return false; - } - List result = maven.validateSettings(location); - if(!result.isEmpty()) { - setMessage(NLS.bind(errorParse, result.get(0).getMessage()), IMessageProvider.WARNING); - return false; + private void checkSettings(String location, String errorMissing, Function> validator, + String errorParse) { + if(location != null) { + String newMessage = !new File(location).canRead() // + ? errorMissing + : validator.apply(location).findFirst().map(msg -> NLS.bind(errorParse, msg)).orElse(null); + if(newMessage != null) { + String prefix = getMessage() != null ? getMessage() + " and " : ""; + setMessage(prefix + newMessage, IMessageProvider.WARNING); + } } - return true; } - void openEditor(String fileName) { + void openEditor(File file) { IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); IWorkbenchPage page = window.getActivePage(); IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor("settings.xml"); //$NON-NLS-1$ - IEditorInput input = new FileStoreEditorInput(EFS.getLocalFileSystem().fromLocalFile(new File(fileName))); + IEditorInput input = new FileStoreEditorInput(EFS.getLocalFileSystem().fromLocalFile(file)); try { IEditorPart editor = IDE.openEditor(page, input, desc.getId()); if(editor == null) { @@ -354,7 +355,7 @@ void openEditor(String fileName) { } editor.addPropertyListener((source, propId) -> { if(!editor.isDirty()) { - log.info("Refreshing settings {}", fileName); //$NON-NLS-1$ + log.info("Refreshing settings {}", file); //$NON-NLS-1$ } }); } catch(PartInitException ex) { @@ -362,11 +363,15 @@ void openEditor(String fileName) { } } - String getUserSettings() { + private String getUserSettings() { return getSettings(userSettingsText); } - String getGlobalSettings() { + private String getUserToolchains() { + return getSettings(userToolchainsText); + } + + private String getGlobalSettings() { return getSettings(globalSettingsText); } diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java index 13697b8616..a3611683fe 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/embedder/IMavenConfiguration.java @@ -55,6 +55,10 @@ public interface IMavenConfiguration { void setUserSettingsFile(String absolutePath) throws CoreException; + String getUserToolchainsFile(); + + void setUserToolchainsFile(String absolutePath) throws CoreException; + // resolution boolean isDownloadSources(); diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/IMavenToolbox.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/IMavenToolbox.java index b535416420..1380b5f492 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/IMavenToolbox.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/IMavenToolbox.java @@ -34,6 +34,10 @@ import org.eclipse.osgi.util.NLS; import org.apache.maven.DefaultMaven; +import org.apache.maven.building.FileSource; +import org.apache.maven.building.Problem; +import org.apache.maven.building.ProblemCollector; +import org.apache.maven.building.ProblemCollectorFactory; import org.apache.maven.execution.DefaultMavenExecutionResult; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenExecutionResult; @@ -50,6 +54,11 @@ import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingResult; +import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest; +import org.apache.maven.toolchain.building.ToolchainsBuilder; +import org.apache.maven.toolchain.building.ToolchainsBuildingException; +import org.apache.maven.toolchain.building.ToolchainsBuildingRequest; +import org.apache.maven.toolchain.building.ToolchainsBuildingResult; import org.eclipse.m2e.core.embedder.IComponentLookup; import org.eclipse.m2e.core.embedder.IMavenExecutionContext; @@ -237,6 +246,39 @@ default Model readModel(InputStream in) throws CoreException { } } + /** + * Validates the toolchains definition + * + * @param toolchains The path to the toolchains definition file to test. + * @return List of all problems. Is never null. + * @throws CoreException if reading failed + */ + default List validateToolchains(String toolchains) { + List problems = new ArrayList<>(); + if(toolchains != null) { + File toolchainsFile = new File(toolchains); + ProblemCollector problemsFactory = ProblemCollectorFactory.newInstance(null); + if(toolchainsFile.canRead()) { + ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); + request.setUserToolchainsSource(new FileSource(toolchainsFile)); + try { + ToolchainsBuildingResult result = getComponentLookup().orElseThrow(ERROR_NO_LOOKUP).lookup(ToolchainsBuilder.class).build(request); + problems.addAll(result.getProblems()); + } catch(ToolchainsBuildingException ex) { + problems.addAll(ex.getProblems()); + } catch(CoreException ex) { + problemsFactory.add(Problem.Severity.FATAL, toolchains, -1, -1, ex); + problems.addAll(problemsFactory.getProblems()); + } + } else { + problemsFactory.add(Problem.Severity.ERROR, NLS.bind(Messages.MavenImpl_error_read_toolchains, toolchains), -1, + -1, null); + problems.addAll(problemsFactory.getProblems()); + } + } + return problems; + } + /** * Writes the given model to the output stream, the default implementation of this requires a lookup. * diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Messages.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Messages.java index 7836c859d5..52379c6a7c 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Messages.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Messages.java @@ -132,6 +132,8 @@ public class Messages extends NLS { public static String MavenImpl_error_read_settings2; + public static String MavenImpl_error_read_toolchains; + public static String MavenImpl_error_resolve; public static String MavenImpl_error_sort; diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenExecutionContext.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenExecutionContext.java index 8d9dd845e5..a6e1e764aa 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenExecutionContext.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/embedder/MavenExecutionContext.java @@ -44,6 +44,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.cli.MavenCli; import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor; import org.apache.maven.eventspy.internal.EventSpyDispatcher; import org.apache.maven.execution.DefaultMavenExecutionRequest; @@ -176,6 +177,13 @@ static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenCon request.setUserSettingsFile(userSettingsFile); //and settings are actually derived from IMavenConfiguration + + File userToolchainsFile = MavenCli.DEFAULT_USER_TOOLCHAINS_FILE; + if(mavenConfiguration.getUserToolchainsFile() != null) { + userToolchainsFile = new File(mavenConfiguration.getUserToolchainsFile()); + } + request.setUserToolchainsFile(userToolchainsFile); + try { request = lookup.lookup(MavenExecutionRequestPopulator.class).populateFromSettings(request, settings); } catch(MavenExecutionRequestPopulationException ex) { diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/messages.properties b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/messages.properties index 2f87fe7e0b..f39e809dc1 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/messages.properties +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/messages.properties @@ -57,6 +57,7 @@ MavenImpl_error_read_pom=Could not read pom.xml MavenImpl_error_read_project=Could not read maven project MavenImpl_error_read_settings=Could not read settings.xml MavenImpl_error_read_settings2=Can not read settings file {0} +MavenImpl_error_read_toolchains=Can not read toolchains file {0} MavenImpl_error_resolve=Could not resolve artifact {0} MavenImpl_error_sort=unable to sort projects MavenImpl_error_write_lastUpdated=Could not write artifact lastUpdated status diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenConfigurationImpl.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenConfigurationImpl.java index 7c9a4d65db..40537fa807 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenConfigurationImpl.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenConfigurationImpl.java @@ -14,6 +14,7 @@ package org.eclipse.m2e.core.internal.preferences; import java.util.Map; +import java.util.Objects; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -93,17 +94,22 @@ void init() { @Override public String getGlobalSettingsFile() { - return preferenceStore.get(MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE, null, preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE, null); } @Override public String getUserSettingsFile() { - return preferenceStore.get(MavenPreferenceConstants.P_USER_SETTINGS_FILE, null, preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_USER_SETTINGS_FILE, null); + } + + @Override + public String getUserToolchainsFile() { + return getStringPreference(MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE, null); } @Override public boolean isDebugOutput() { - return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_DEBUG_OUTPUT, null, preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_DEBUG_OUTPUT); } public void setDebugOutput(boolean debug) { @@ -112,14 +118,12 @@ public void setDebugOutput(boolean debug) { @Override public boolean isDownloadJavaDoc() { - return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_DOWNLOAD_JAVADOC, null, - preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_DOWNLOAD_JAVADOC); } @Override public boolean isDownloadSources() { - return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_DOWNLOAD_SOURCES, null, - preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_DOWNLOAD_SOURCES); } public void setDownloadSources(boolean downloadSources) { @@ -132,63 +136,51 @@ public void setDownloadJavadoc(boolean downloadJavadoc) { @Override public boolean isHideFoldersOfNestedProjects() { - return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_HIDE_FOLDERS_OF_NESTED_PROJECTS, null, - preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_HIDE_FOLDERS_OF_NESTED_PROJECTS); } @Override public boolean isOffline() { - return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_OFFLINE, null, preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_OFFLINE); } @Override public void setUserSettingsFile(String settingsFile) throws CoreException { - settingsFile = trim(settingsFile); - if(!eq(settingsFile, preferencesLookup[0].get(MavenPreferenceConstants.P_USER_SETTINGS_FILE, null))) { - if(settingsFile != null) { - preferencesLookup[0].put(MavenPreferenceConstants.P_USER_SETTINGS_FILE, settingsFile); - } else { - preferencesLookup[0].remove(MavenPreferenceConstants.P_USER_SETTINGS_FILE); - } - preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); - } + setSettingsFile(settingsFile, MavenPreferenceConstants.P_USER_SETTINGS_FILE); } @Override public void setGlobalSettingsFile(String globalSettingsFile) throws CoreException { - globalSettingsFile = trim(globalSettingsFile); - if(!eq(globalSettingsFile, preferencesLookup[0].get(MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE, null))) { - if(globalSettingsFile != null) { - preferencesLookup[0].put(MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE, globalSettingsFile); - } else { - preferencesLookup[0].remove(MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE); - } - preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); - } + setSettingsFile(globalSettingsFile, MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE); } - private boolean eq(String a, String b) { - return a != null ? a.equals(b) : b == null; + @Override + public void setUserToolchainsFile(String settingsFile) throws CoreException { + setSettingsFile(settingsFile, MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE); } - private String trim(String str) { - if(str == null) { - return null; + private void setSettingsFile(String settingsFile, String preferenceKey) throws CoreException { + if(settingsFile != null) { + settingsFile = settingsFile.isBlank() ? null : settingsFile.strip(); + } + if(!Objects.equals(settingsFile, preferencesLookup[0].get(preferenceKey, null))) { + if(settingsFile != null) { + preferencesLookup[0].put(preferenceKey, settingsFile); + } else { + preferencesLookup[0].remove(preferenceKey); + } + preferenceStore.applyPreferences(preferencesLookup[0], PREFERENCE_FILTERS); } - str = str.trim(); - return !str.isEmpty() ? str : null; } @Override public boolean isUpdateProjectsOnStartup() { - return Boolean.parseBoolean(preferenceStore - .get(MavenPreferenceConstants.P_UPDATE_PROJECTS, null, preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_UPDATE_PROJECTS); } @Override public boolean isUpdateIndexesOnStartup() { - return Boolean - .parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_UPDATE_INDEXES, null, preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_UPDATE_INDEXES); } @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) @@ -225,23 +217,21 @@ public void removed(NodeChangeEvent event) { } } - private IPreferenceFilter getPreferenceFilter() { - return new IPreferenceFilter() { - @Override - public String[] getScopes() { - return new String[] {InstanceScope.SCOPE, DefaultScope.SCOPE}; - } + private static final IPreferenceFilter[] PREFERENCE_FILTERS = new IPreferenceFilter[] {new IPreferenceFilter() { + @Override + public String[] getScopes() { + return new String[] {InstanceScope.SCOPE, DefaultScope.SCOPE}; + } - @Override - public Map getMapping(String scope) { - return null; - } - }; - } + @Override + public Map getMapping(String scope) { + return null; + } + }}; @Override public String getGlobalUpdatePolicy() { - String string = preferenceStore.get(MavenPreferenceConstants.P_GLOBAL_UPDATE_POLICY, null, preferencesLookup); + String string = getStringPreference(MavenPreferenceConstants.P_GLOBAL_UPDATE_POLICY, null); //for backward compat if(string == null || "true".equalsIgnoreCase(string)) { return RepositoryPolicy.UPDATE_POLICY_NEVER; @@ -265,7 +255,7 @@ public String getWorkspaceLifecycleMappingMetadataFile() { IPath stateLocation = MavenPluginActivator.getDefault().getStateLocation(); String defaultValue = stateLocation.append(LifecycleMappingFactory.LIFECYCLE_MAPPING_METADATA_SOURCE_NAME) .toString(); - return preferenceStore.get(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION, defaultValue, preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION, defaultValue); } @Override @@ -275,13 +265,13 @@ public void setWorkspaceLifecycleMappingMetadataFile(String location) throws Cor } else { preferencesLookup[0].remove(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION); } - preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); + preferenceStore.applyPreferences(preferencesLookup[0], PREFERENCE_FILTERS); } @Override public String getOutOfDateProjectSeverity() { - return preferenceStore.get(MavenPreferenceConstants.P_OUT_OF_DATE_PROJECT_CONFIG_PB, - ProblemSeverity.error.toString(), preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_OUT_OF_DATE_PROJECT_CONFIG_PB, + ProblemSeverity.error.toString()); } /** @@ -293,12 +283,12 @@ public void setOutOfDateProjectSeverity(String severity) throws CoreException { } else { preferencesLookup[0].put(MavenPreferenceConstants.P_OUT_OF_DATE_PROJECT_CONFIG_PB, severity); } - preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); + preferenceStore.applyPreferences(preferencesLookup[0], PREFERENCE_FILTERS); } @Override public String getGlobalChecksumPolicy() { - return preferenceStore.get(MavenPreferenceConstants.P_GLOBAL_CHECKSUM_POLICY, null, preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_GLOBAL_CHECKSUM_POLICY, null); } /** @@ -318,8 +308,8 @@ public void setGlobalChecksumPolicy(String checksumPolicy) { @Override public String getNotCoveredMojoExecutionSeverity() { - return preferenceStore.get(MavenPreferenceConstants.P_NOT_COVERED_MOJO_EXECUTION_PB, - ProblemSeverity.warning.toString(), preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_NOT_COVERED_MOJO_EXECUTION_PB, + ProblemSeverity.warning.toString()); } /** @@ -331,13 +321,13 @@ public void setNotCoveredMojoExecutionSeverity(String severity) throws CoreExcep } else { preferencesLookup[0].put(MavenPreferenceConstants.P_NOT_COVERED_MOJO_EXECUTION_PB, severity); } - preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); + preferenceStore.applyPreferences(preferencesLookup[0], PREFERENCE_FILTERS); } @Override public String getOverridingManagedVersionExecutionSeverity() { - return preferenceStore.get(MavenPreferenceConstants.P_OVERRIDING_MANAGED_VERSION_PB, - ProblemSeverity.warning.toString(), preferencesLookup); + return getStringPreference(MavenPreferenceConstants.P_OVERRIDING_MANAGED_VERSION_PB, + ProblemSeverity.warning.toString()); } /** @@ -349,13 +339,12 @@ public void setOverridingManagedVersionExecutionSeverity(String severity) throws } else { preferencesLookup[0].put(MavenPreferenceConstants.P_OVERRIDING_MANAGED_VERSION_PB, severity); } - preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); + preferenceStore.applyPreferences(preferencesLookup[0], PREFERENCE_FILTERS); } @Override public boolean isAutomaticallyUpdateConfiguration() { - return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_AUTO_UPDATE_CONFIGURATION, null, - preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_AUTO_UPDATE_CONFIGURATION); } public void setAutomaticallyUpdateConfiguration(boolean value) { @@ -369,9 +358,8 @@ public void setDefaultMojoExecutionAction(PluginExecutionAction mojoAction) { @Override public PluginExecutionAction getDefaultMojoExecutionAction() { - String value = preferenceStore.get(MavenPreferenceConstants.P_DEFAULT_MOJO_EXECUTION_ACTION, - PluginExecutionAction.DEFAULT_ACTION.toString(), - preferencesLookup); + String value = getStringPreference(MavenPreferenceConstants.P_DEFAULT_MOJO_EXECUTION_ACTION, + PluginExecutionAction.DEFAULT_ACTION.toString()); try { return PluginExecutionAction.valueOf(value); } catch(IllegalArgumentException e) { @@ -382,7 +370,15 @@ public PluginExecutionAction getDefaultMojoExecutionAction() { @Override public boolean buildWithNullSchedulingRule() { - return Boolean.parseBoolean( - preferenceStore.get(MavenPreferenceConstants.P_BUILDER_USE_NULL_SCHEDULING_RULE, null, preferencesLookup)); + return getBooleanPreference(MavenPreferenceConstants.P_BUILDER_USE_NULL_SCHEDULING_RULE); + } + + private boolean getBooleanPreference(String key) { + return Boolean.parseBoolean(getStringPreference(key, null)); } + + private String getStringPreference(String key, String defaultValue) { + return preferenceStore.get(key, defaultValue, preferencesLookup); + } + } diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java index ab73d00f73..f5d1297ef8 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/preferences/MavenPreferenceConstants.java @@ -61,6 +61,9 @@ public interface MavenPreferenceConstants { /** String */ String P_USER_SETTINGS_FILE = PREFIX + "userSettingsFile"; //$NON-NLS-1$ + /** String */ + String P_USER_TOOLCHAINS_FILE = PREFIX + "userToolchainsFile"; //$NON-NLS-1$ + /** String */ String P_OUTPUT_FOLDER = PREFIX + "outputFolder"; //$NON-NLS-1$