From aa6e2b578a2246559edf9698e707ff635154dc99 Mon Sep 17 00:00:00 2001 From: G-Ork Date: Mon, 8 Jan 2024 19:49:41 +0100 Subject: [PATCH] Bug 571572 - UI for setting the location of the toolchain.xml files #114 --- org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF | 2 +- .../m2e/core/ui/internal/Messages.java | 12 +++ .../m2e/core/ui/internal/messages.properties | 6 ++ .../MavenSettingsPreferencePage.java | 85 ++++++++++++++++++- .../core/embedder/IMavenConfiguration.java | 4 + .../m2e/core/internal/IMavenToolbox.java | 44 ++++++++++ .../eclipse/m2e/core/internal/Messages.java | 2 + .../embedder/MavenExecutionContext.java | 8 ++ .../m2e/core/internal/messages.properties | 1 + .../preferences/MavenConfigurationImpl.java | 18 ++++ .../preferences/MavenPreferenceConstants.java | 3 + 11 files changed, 183 insertions(+), 2 deletions(-) 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..ab28ed9fa7 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 @@ -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,14 @@ 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 MavenSettingsPreferencePage_userToolchainsBrowseButton_text; + public static String MavenWarningsPreferencePage_versionDupParent; public static String MavenWarningsPreferencePage_overridingManagedPreferences; 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..d6f464dabe 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 @@ -315,6 +315,8 @@ MavenSettingsPreferencePage_error_globalSettingsMissing=Global settings file doe 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_error_userToolchainsMissing=User toolchains file doesn't exist +MavenSettingsPreferencePage_error_userToolchainsParse=Unable to parse user toolchains file {0} MavenSettingsPreferencePage_globalSettingsBrowseButton_text=Browse... MavenSettingsPreferencePage_job_indexing=Indexing Local Repository... MavenSettingsPreferencePage_job_updating=Updating Maven settings @@ -322,6 +324,10 @@ MavenSettingsPreferencePage_lblLocal=Local Repository (From merged user and glob 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_userToolchainsBrowseButton_text=Browse... 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..9c8f1573fe 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 @@ -55,6 +55,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 +65,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 +95,10 @@ public class MavenSettingsPreferencePage extends PreferencePage implements IWork private Link userSettingsLink; + private Link userToolchainsLink; + + private Text userToolchainsText; + public MavenSettingsPreferencePage() { setTitle(Messages.MavenSettingsPreferencePage_title); @@ -116,12 +123,15 @@ protected void updateSettings(boolean updateMavenDependencies) { updateLocalRepository(); String userSettings = getUserSettings(); + String userToolchains = getUserToolchains(); String globalSettings = getGlobalSettings(); String currentGlobalSettings = mavenConfiguration.getGlobalSettingsFile(); String currentUserSettings = mavenConfiguration.getUserSettingsFile(); + String currentUserToolchains = mavenConfiguration.getUserToolchainsFile(); - if(Objects.equals(globalSettings, currentGlobalSettings) && Objects.equals(currentUserSettings, userSettings)) { + if(Objects.equals(globalSettings, currentGlobalSettings) && Objects.equals(currentUserSettings, userSettings) + && Objects.equals(currentUserToolchains, userToolchains)) { return; } @@ -142,6 +152,10 @@ protected void updateSettings(boolean updateMavenDependencies) { mavenConfiguration.setGlobalSettingsFile(globalSettings); mavenConfiguration.setUserSettingsFile(userSettings); + if(null != userToolchains) { + mavenConfiguration.setUserToolchainsFile(userToolchains); + } + if(Boolean.TRUE.equals(updateProjects[0])) { List projects = MavenPlugin.getMavenProjectRegistry().getProjects(); if(projects != null && !projects.isEmpty()) { @@ -231,6 +245,28 @@ protected Control createContents(Composite parent) { updateSettings.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1)); updateSettings.setText(Messages.MavenSettingsPreferencePage_btnUpdate); updateSettings.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> updateSettings(true))); + + userToolchainsLink = new Link(composite, SWT.NONE); + userToolchainsLink.setText(Messages.MavenSettingsPreferencePage_userToolchainslink2); + userToolchainsLink.setToolTipText(Messages.MavenSettingsPreferencePage_userToolchainslink_tooltip); + userToolchainsLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); + userToolchainsLink.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { + String userToolchains = getUserToolchains(); + if(userToolchains == null) { + userToolchains = MavenCli.DEFAULT_USER_TOOLCHAINS_FILE.getAbsolutePath(); + } + openEditor(userToolchains); + })); + userToolchainsText = new Text(composite, SWT.BORDER); + userToolchainsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); + userToolchainsText.setMessage(MavenCli.DEFAULT_USER_TOOLCHAINS_FILE.getAbsolutePath()); + + Button userToolchainsBrowseButton = new Button(composite, SWT.NONE); + userToolchainsBrowseButton.setLayoutData(new GridData(SWT.FILL, SWT.RIGHT, false, false, 1, 1)); + userToolchainsBrowseButton.setText(Messages.MavenSettingsPreferencePage_userToolchainsBrowseButton_text); + userToolchainsBrowseButton + .addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> browseSettingsAction(userToolchainsText))); + Label localRepositoryLabel = new Label(composite, SWT.NONE); GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1); gd.verticalIndent = 25; @@ -247,6 +283,7 @@ protected Control createContents(Composite parent) { checkSettings(); }; userSettingsText.addModifyListener(settingsModifyListener); + userToolchainsText.addModifyListener(settingsModifyListener); globalSettingsText.addModifyListener(settingsModifyListener); String globalSettings = mavenConfiguration.getGlobalSettingsFile(); @@ -257,6 +294,10 @@ protected Control createContents(Composite parent) { if(userSettings != null) { userSettingsText.setText(userSettings); } + String userToolchains = mavenConfiguration.getUserToolchainsFile(); + if(userToolchains != null) { + userToolchainsText.setText(userToolchains); + } checkSettings(); updateLocalRepository(); @@ -277,6 +318,20 @@ private void updateUserSettingsLink(String userSettings) { userSettingsLink.setText(text); } + private void updateUserToolchainsLink(String userToolchains) { + File userToolchainsFile = MavenCli.DEFAULT_USER_TOOLCHAINS_FILE; + if(userToolchains != null) { + userToolchainsFile = new File(userToolchains); + } + boolean active = userToolchainsFile.canRead(); + + String text = Messages.MavenSettingsPreferencePage_userToolchainslink1; + if(active) { + text = Messages.MavenSettingsPreferencePage_userToolchainslink2; + } + userToolchainsLink.setText(text); + } + private void updateGlobalSettingsLink(String globalSettings) { boolean active = globalSettings != null && new File(globalSettings).canRead(); String text = Messages.MavenSettingsPreferencePage_globalSettingslink1; @@ -315,6 +370,9 @@ protected void checkSettings() { String userSettings = getUserSettings(); updateUserSettingsLink(userSettings); + String userToolchains = getUserToolchains(); + updateUserToolchainsLink(userToolchains); + if(globalSettings != null && !checkSettings(globalSettings, Messages.MavenSettingsPreferencePage_error_globalSettingsMissing, Messages.MavenSettingsPreferencePage_error_globalSettingsParse)) { @@ -322,6 +380,9 @@ protected void checkSettings() { } else if(userSettings != null) { checkSettings(userSettings, Messages.MavenSettingsPreferencePage_error_userSettingsMissing, Messages.MavenSettingsPreferencePage_error_userSettingsParse); + } else if(userToolchains != null) { + checkToolchains(userToolchains, Messages.MavenSettingsPreferencePage_error_userToolchainsMissing, + Messages.MavenSettingsPreferencePage_error_userToolchainsParse); } } @@ -338,6 +399,24 @@ private boolean checkSettings(String location, String errorMissing, String error return true; } + private boolean checkToolchains(String location, String errorMissing, String errorParse) { + if(!new File(location).canRead()) { + setMessage(errorMissing, IMessageProvider.WARNING); + return false; + } + try { + List result = IMavenToolbox.of(this.maven).validateToolchains(location); + if(!result.isEmpty()) { + setMessage(NLS.bind(errorParse, result.get(0).getMessage()), IMessageProvider.WARNING); + return false; + } + return true; + } catch(final CoreException exception) { + setMessage(NLS.bind(errorParse, exception.getMessage()), IMessageProvider.ERROR); + return false; + } + } + void openEditor(String fileName) { IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); @@ -366,6 +445,10 @@ String getUserSettings() { return getSettings(userSettingsText); } + String getUserToolchains() { + return getSettings(userToolchainsText); + } + 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..eadc3fc2d5 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,41 @@ 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) throws CoreException { + IComponentLookup componentLookup = getComponentLookup().orElseThrow(ERROR_NO_LOOKUP); + ProblemCollector problemsFactory = ProblemCollectorFactory.newInstance(null); + List problems = new ArrayList<>(); + if(toolchains != null) { + File toolchainsFile = new File(toolchains); + if(toolchainsFile.canRead()) { + ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); + request.setUserToolchainsSource(new FileSource(toolchainsFile)); + try { + ToolchainsBuildingResult result = componentLookup.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_toolchains2, 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..4778c80df9 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_toolchains2; + 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..d1c88f6981 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_toolchains2=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..e7f0cb7fb1 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 @@ -101,6 +101,11 @@ public String getUserSettingsFile() { return preferenceStore.get(MavenPreferenceConstants.P_USER_SETTINGS_FILE, null, preferencesLookup); } + @Override + public String getUserToolchainsFile() { + return preferenceStore.get(MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE, null, preferencesLookup); + } + @Override public boolean isDebugOutput() { return Boolean.parseBoolean(preferenceStore.get(MavenPreferenceConstants.P_DEBUG_OUTPUT, null, preferencesLookup)); @@ -167,6 +172,19 @@ public void setGlobalSettingsFile(String globalSettingsFile) throws CoreExceptio } } + @Override + public void setUserToolchainsFile(String settingsFile) throws CoreException { + settingsFile = trim(settingsFile); + if(!eq(settingsFile, preferencesLookup[0].get(MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE, null))) { + if(settingsFile != null) { + preferencesLookup[0].put(MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE, settingsFile); + } else { + preferencesLookup[0].remove(MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE); + } + preferenceStore.applyPreferences(preferencesLookup[0], new IPreferenceFilter[] {getPreferenceFilter()}); + } + } + private boolean eq(String a, String b) { return a != null ? a.equals(b) : b == null; } 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$