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 committed Jan 13, 2024
1 parent 98dbefe commit aa6e2b5
Show file tree
Hide file tree
Showing 11 changed files with 183 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,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,19 @@ 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
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_userToolchainsBrowseButton_text=Browse...
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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

Expand All @@ -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<IMavenProjectFacade> projects = MavenPlugin.getMavenProjectRegistry().getProjects();
if(projects != null && !projects.isEmpty()) {
Expand Down Expand Up @@ -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;
Expand All @@ -247,6 +283,7 @@ protected Control createContents(Composite parent) {
checkSettings();
};
userSettingsText.addModifyListener(settingsModifyListener);
userToolchainsText.addModifyListener(settingsModifyListener);
globalSettingsText.addModifyListener(settingsModifyListener);

String globalSettings = mavenConfiguration.getGlobalSettingsFile();
Expand All @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -315,13 +370,19 @@ 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)) {
//work is done in if-condition
} 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);
}
}

Expand All @@ -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<Problem> 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();
Expand Down Expand Up @@ -366,6 +445,10 @@ String getUserSettings() {
return getSettings(userSettingsText);
}

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

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,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 <code>null</code>.
* @throws CoreException if reading failed
*/
default List<Problem> validateToolchains(String toolchains) throws CoreException {
IComponentLookup componentLookup = getComponentLookup().orElseThrow(ERROR_NO_LOOKUP);
ProblemCollector problemsFactory = ProblemCollectorFactory.newInstance(null);
List<Problem> 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.
*
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_toolchains2;

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_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
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 aa6e2b5

Please sign in to comment.