Skip to content

Commit

Permalink
Bug 571572 - UI for setting the location of the toolchain.xml files #114
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ork authored and HannesWell committed Feb 4, 2024
1 parent 28e32f6 commit 85aabd2
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,17 @@ 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_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 (<a href\="\#">open file</a>)\:
MavenSettingsPreferencePage_userSettingslink_tooltip=Open editor for user settings
MavenSettingsPreferencePage_userToolchainslink1=User &Toolchains\:
MavenSettingsPreferencePage_userToolchainslink2=User &Toolchains (<a href\="\#">open file</a>)\:
MavenSettingsPreferencePage_userToolchainslink_tooltip=Open editor for user toolchains
MavenSettingsPreferencePage_globalSettingslink1=Global &Settings\:
MavenSettingsPreferencePage_globalSettingslink2=Global &Settings (<a href\="\#">open file</a>)\:
MavenSettingsPreferencePage_globalSettingslink_tooltip=Open editor for global settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,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;
Expand All @@ -66,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;
Expand Down Expand Up @@ -95,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);

Expand All @@ -119,10 +126,12 @@ protected void updateSettings(boolean updateMavenDependencies) {
updateLocalRepository();

String userSettings = getUserSettings();
String userToolchains = getUserToolchains();
String globalSettings = getGlobalSettings();

if(Objects.equals(globalSettings, mavenConfiguration.getGlobalSettingsFile())
&& Objects.equals(userSettings, mavenConfiguration.getUserSettingsFile())) {
&& Objects.equals(userSettings, mavenConfiguration.getUserSettingsFile())
&& Objects.equals(userToolchains, mavenConfiguration.getUserToolchainsFile())) {
return; // current preferences not changed
}

Expand All @@ -142,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<IMavenProjectFacade> projects = MavenPlugin.getMavenProjectRegistry().getProjects();
Expand Down Expand Up @@ -198,6 +208,12 @@ protected Control createContents(Composite parent) {
userSettingsText = createFileSelectionWidgets(composite, mavenConfiguration.getUserSettingsFile(),
SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);

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);
Expand Down Expand Up @@ -293,13 +309,21 @@ protected void checkSettings() {
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 void checkSettings(String location, String errorMissing, Function<String, Stream<String>> validator,
Expand Down Expand Up @@ -343,6 +367,10 @@ private String getUserSettings() {
return getSettings(userSettingsText);
}

private String getUserToolchains() {
return getSettings(userToolchainsText);
}

private String getGlobalSettings() {
return getSettings(globalSettingsText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public interface IMavenConfiguration {

void setUserSettingsFile(String absolutePath) throws CoreException;

String getUserToolchainsFile();

void setUserToolchainsFile(String absolutePath) throws CoreException;

// resolution

boolean isDownloadSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 <code>null</code>.
* @throws CoreException if reading failed
*/
default List<Problem> validateToolchains(String toolchains) {
List<Problem> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public String getUserSettingsFile() {
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 getBooleanPreference(MavenPreferenceConstants.P_DEBUG_OUTPUT);
Expand Down Expand Up @@ -149,6 +154,11 @@ public void setGlobalSettingsFile(String globalSettingsFile) throws CoreExceptio
setSettingsFile(globalSettingsFile, MavenPreferenceConstants.P_GLOBAL_SETTINGS_FILE);
}

@Override
public void setUserToolchainsFile(String settingsFile) throws CoreException {
setSettingsFile(settingsFile, MavenPreferenceConstants.P_USER_TOOLCHAINS_FILE);
}

private void setSettingsFile(String settingsFile, String preferenceKey) throws CoreException {
if(settingsFile != null) {
settingsFile = settingsFile.isBlank() ? null : settingsFile.strip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$

Expand Down

0 comments on commit 85aabd2

Please sign in to comment.