From 57aeba0a04707bb7d8be4b86e83fdf56cc88a1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Mon, 26 Jun 2023 10:18:48 +0200 Subject: [PATCH] Support variables in Maven Installations Currently maven installations only support a fixed path but for certain setups it would be desirable to use a variable. This adds support for specify a variable in the path for an external installation. Fix https://github.com/eclipse-m2e/m2e-core/issues/1372 --- RELEASE_NOTES.md | 4 ++++ org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF | 5 +++-- .../m2e/core/ui/internal/Messages.java | 2 ++ .../m2e/core/ui/internal/messages.properties | 1 + .../launch/MavenInstallationWizardPage.java | 20 ++++++++++++++----- org.eclipse.m2e.core/META-INF/MANIFEST.MF | 3 ++- .../internal/launch/MavenExternalRuntime.java | 20 +++++++++++++------ 7 files changed, 41 insertions(+), 14 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 38e11353c8..816be7b86e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -22,6 +22,10 @@ If you want to switch back to an older runtime you currently need to modify you ![grafik](https://github.com/eclipse-m2e/m2e-core/assets/1331477/ef04e7f4-e36b-4bbc-a4d3-ff92e6a5f9f4) +### Support for variables in maven installations + +In the preferences for maven installations one can now specify variables for maven location. + ## 2.3.0 * 📅 Release Date: 23th May 2023 diff --git a/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF b/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF index 5f8cd40127..eb5de450c1 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.6.qualifier +Bundle-Version: 2.4.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-17 Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor @@ -42,7 +42,8 @@ Require-Bundle: org.eclipse.m2e.core;bundle-version="[2.0.0,3.0.0)", org.eclipse.jface.text, org.eclipse.core.filebuffers, org.eclipse.ui, - org.eclipse.ui.navigator + org.eclipse.ui.navigator, + org.eclipse.debug.ui Import-Package: org.eclipse.compare.rangedifferencer, org.eclipse.ltk.core.refactoring, org.slf4j;version="[1.7.0,3.0.0)" 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..b990488ad8 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 @@ -985,6 +985,8 @@ public class Messages extends NLS { public static String ExternalInstallPage_btnDirectory_text; + public static String ExternalInstallPage_btnVariables_text; + public static String ExternalInstallPage_lblInstallationLibraries_text; public static String ExternalInstallPage_btnRestoreDefault_text; 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..a6d8ecdd3e 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 @@ -48,6 +48,7 @@ EnableNatureAction_job_enable=Enabling Maven Dependency Management EnableNatureAction_wizard_shell=Create new POM ExternalInstallPage_btnAddProject_text=Project... ExternalInstallPage_btnDirectory_text=Directory... +ExternalInstallPage_btnVariables_text=&Variables... ExternalInstallPage_btnDown_text=Down ExternalInstallPage_btnRemove_text=Remove ExternalInstallPage_btnRestoreDefault_text=Restore Default diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/launch/MavenInstallationWizardPage.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/launch/MavenInstallationWizardPage.java index f14243bb73..ce2cce8c42 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/launch/MavenInstallationWizardPage.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/launch/MavenInstallationWizardPage.java @@ -22,6 +22,7 @@ import java.util.Set; import org.eclipse.core.resources.IProject; +import org.eclipse.debug.ui.StringVariableSelectionDialog; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredSelection; @@ -189,11 +190,24 @@ public void createControl(Composite parent) { location.addModifyListener(e -> updateStatus()); location.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - btnDirectory = new Button(container, SWT.NONE); + Composite btnComposite = new Composite(container, SWT.NONE); + btnComposite.setLayout((new GridLayout(2, true))); + btnDirectory = new Button(btnComposite, SWT.NONE); btnDirectory.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> selectLocationAction())); btnDirectory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); btnDirectory.setText(Messages.ExternalInstallPage_btnDirectory_text); + Button variablesButton = new Button(btnComposite, SWT.NONE); + variablesButton.setText(Messages.ExternalInstallPage_btnVariables_text); + variablesButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { + StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell()); + dialog.open(); + String variable = dialog.getVariableExpression(); + if(variable != null) { + location.setText(location.getText() + variable); + } + })); + Label lblInstallationName = new Label(container, SWT.NONE); lblInstallationName.setText(Messages.ExternalInstallPage_lblInstallationName_text); @@ -353,10 +367,6 @@ private boolean isValidMavenInstall(String dir) { if(dir == null || dir.length() == 0) { return false; } - File selectedDir = new File(dir); - if(!selectedDir.isDirectory()) { - return false; - } return new MavenExternalRuntime(dir).isAvailable(); } diff --git a/org.eclipse.m2e.core/META-INF/MANIFEST.MF b/org.eclipse.m2e.core/META-INF/MANIFEST.MF index 25fde7bb4f..658ea219af 100644 --- a/org.eclipse.m2e.core/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.core/META-INF/MANIFEST.MF @@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.m2e.maven.runtime;bundle-version="[3.8.6,4.0.0)", org.eclipse.m2e.workspace.cli;bundle-version="0.1.0", org.eclipse.core.runtime;bundle-version="3.12.0", org.eclipse.core.resources;bundle-version="3.9.0", - org.eclipse.core.filesystem;bundle-version="1.7.700" + org.eclipse.core.filesystem;bundle-version="1.7.700", + org.eclipse.core.variables Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-17 Export-Package: org.eclipse.m2e.core, diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/launch/MavenExternalRuntime.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/launch/MavenExternalRuntime.java index 6395cb8364..4ad4072d75 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/launch/MavenExternalRuntime.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/launch/MavenExternalRuntime.java @@ -29,6 +29,8 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Status; +import org.eclipse.core.variables.IStringVariableManager; +import org.eclipse.core.variables.VariablesPlugin; import org.codehaus.plexus.classworlds.ClassWorldException; import org.codehaus.plexus.classworlds.launcher.ConfigurationException; @@ -71,16 +73,22 @@ public boolean isEditable() { @Override public boolean isAvailable() { - return new File(location, "bin").exists() && getLauncherClasspath() != null && isSupportedVersion(); //$NON-NLS-1$ + return new File(getLocation(), "bin").exists() && getLauncherClasspath() != null && isSupportedVersion(); //$NON-NLS-1$ } @Override public String getLocation() { + IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); + try { + return manager.performStringSubstitution(location); + } catch(CoreException ex) { + //if we can't parse the location we need to return the unparsed raw value... + } return location; } private File getLauncherConfigurationFile() { - return new File(location, "bin/m2.conf"); //$NON-NLS-1$ + return new File(getLocation(), "bin/m2.conf"); //$NON-NLS-1$ } @Override @@ -140,7 +148,7 @@ public void setAppMain(String mainClassName, String mainRealmName) { Properties properties = new Properties(); copyProperties(properties, System.getProperties()); - properties.put(PROPERTY_MAVEN_HOME, location); + properties.put(PROPERTY_MAVEN_HOME, getLocation()); ConfigurationParser parser = new ConfigurationParser(handler, properties); @@ -158,7 +166,7 @@ public void setAppMain(String mainClassName, String mainRealmName) { @Override public String toString() { - return location + ' ' + getVersion(); + return getLocation() + ' ' + getVersion(); } private static class ExceptionWrapper extends RuntimeException { @@ -170,7 +178,7 @@ public ExceptionWrapper(Exception cause) { } private String getLauncherClasspath() { - File mavenHome = new File(location); + File mavenHome = new File(getLocation()); DirectoryScanner ds = new DirectoryScanner(); ds.setBasedir(mavenHome); ds.setIncludes(new String[] {"core/boot/classworlds*.jar", // 2.0.4 //$NON-NLS-1$ @@ -255,7 +263,7 @@ public void setAppMain(String mainClassName, String mainRealmName) { Properties properties = new Properties(); copyProperties(properties, System.getProperties()); - properties.put(PROPERTY_MAVEN_HOME, location); + properties.put(PROPERTY_MAVEN_HOME, getLocation()); try (FileInputStream is = new FileInputStream(getLauncherConfigurationFile())) { new ConfigurationParser(handler, properties).parse(is);