Skip to content

Commit f81e6c5

Browse files
committed
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 #1372
1 parent 26ac02f commit f81e6c5

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ can not supply any useful completions.
1212
m2e now contains a new lemminx-extension that provides such completions in a
1313
basic way to support people writing such bnd instructions in pom xml configurations.
1414

15+
### Support for variables in maven installations
16+
17+
In the preferences for maven installations one can now specify variables for maven location.
1518

1619
## 2.7.0
1720

org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-SymbolicName: org.eclipse.m2e.core.ui;singleton:=true
4-
Bundle-Version: 2.1.0.qualifier
4+
Bundle-Version: 2.4.0.qualifier
55
Bundle-RequiredExecutionEnvironment: JavaSE-21
66
Bundle-Name: %Bundle-Name
77
Bundle-Vendor: %Bundle-Vendor
@@ -41,7 +41,8 @@ Require-Bundle: org.eclipse.m2e.core;bundle-version="[2.0.0,3.0.0)",
4141
org.eclipse.jface.text,
4242
org.eclipse.core.filebuffers,
4343
org.eclipse.ui,
44-
org.eclipse.ui.navigator
44+
org.eclipse.ui.navigator,
45+
org.eclipse.debug.ui
4546
Import-Package: org.apache.maven.archetype.catalog;provider=m2e;version="[3.2.1,4.0.0)",
4647
org.apache.maven.archetype.catalog.io.xpp3;provider=m2e;version="[3.2.1,4.0.0)",
4748
org.apache.maven.archetype.common;provider=m2e;version="[3.2.1,4.0.0)",

org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,8 @@ public class Messages extends NLS {
995995

996996
public static String ExternalInstallPage_btnDirectory_text;
997997

998+
public static String ExternalInstallPage_btnVariables_text;
999+
9981000
public static String ExternalInstallPage_lblInstallationLibraries_text;
9991001

10001002
public static String ExternalInstallPage_btnRestoreDefault_text;

org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ EnableNatureAction_job_enable=Enabling Maven Dependency Management
4848
EnableNatureAction_wizard_shell=Create new POM
4949
ExternalInstallPage_btnAddProject_text=Project...
5050
ExternalInstallPage_btnDirectory_text=Directory...
51+
ExternalInstallPage_btnVariables_text=&Variables...
5152
ExternalInstallPage_btnDown_text=Down
5253
ExternalInstallPage_btnRemove_text=Remove
5354
ExternalInstallPage_btnRestoreDefault_text=Restore Default

org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/launch/MavenInstallationWizardPage.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Set;
2323

2424
import org.eclipse.core.resources.IProject;
25+
import org.eclipse.debug.ui.StringVariableSelectionDialog;
2526
import org.eclipse.jface.viewers.ILabelProvider;
2627
import org.eclipse.jface.viewers.ILabelProviderListener;
2728
import org.eclipse.jface.viewers.IStructuredSelection;
@@ -189,11 +190,24 @@ public void createControl(Composite parent) {
189190
location.addModifyListener(e -> updateStatus());
190191
location.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
191192

192-
btnDirectory = new Button(container, SWT.NONE);
193+
Composite btnComposite = new Composite(container, SWT.NONE);
194+
btnComposite.setLayout((new GridLayout(2, true)));
195+
btnDirectory = new Button(btnComposite, SWT.NONE);
193196
btnDirectory.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> selectLocationAction()));
194197
btnDirectory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
195198
btnDirectory.setText(Messages.ExternalInstallPage_btnDirectory_text);
196199

200+
Button variablesButton = new Button(btnComposite, SWT.NONE);
201+
variablesButton.setText(Messages.ExternalInstallPage_btnVariables_text);
202+
variablesButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
203+
StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
204+
dialog.open();
205+
String variable = dialog.getVariableExpression();
206+
if(variable != null) {
207+
location.setText(location.getText() + variable);
208+
}
209+
}));
210+
197211
Label lblInstallationName = new Label(container, SWT.NONE);
198212
lblInstallationName.setText(Messages.ExternalInstallPage_lblInstallationName_text);
199213

@@ -353,10 +367,6 @@ private boolean isValidMavenInstall(String dir) {
353367
if(dir == null || dir.length() == 0) {
354368
return false;
355369
}
356-
File selectedDir = new File(dir);
357-
if(!selectedDir.isDirectory()) {
358-
return false;
359-
}
360370
return new MavenExternalRuntime(dir).isAvailable();
361371
}
362372

org.eclipse.m2e.core/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.m2e.maven.runtime;bundle-version="[3.8.6,4.0.0)",
1010
org.eclipse.m2e.workspace.cli;bundle-version="0.1.0",
1111
org.eclipse.core.runtime;bundle-version="[3.27.0,4.0.0)",
1212
org.eclipse.core.resources;bundle-version="3.9.0",
13-
org.eclipse.core.filesystem;bundle-version="1.7.700"
13+
org.eclipse.core.filesystem;bundle-version="1.7.700",
14+
org.eclipse.core.variables
1415
Bundle-ActivationPolicy: lazy
1516
Bundle-RequiredExecutionEnvironment: JavaSE-21
1617
Export-Package: org.eclipse.m2e.core,

org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/launch/MavenExternalRuntime.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.eclipse.core.runtime.CoreException;
3030
import org.eclipse.core.runtime.IProgressMonitor;
3131
import org.eclipse.core.runtime.Status;
32+
import org.eclipse.core.variables.IStringVariableManager;
33+
import org.eclipse.core.variables.VariablesPlugin;
3234

3335
import org.codehaus.plexus.classworlds.ClassWorldException;
3436
import org.codehaus.plexus.classworlds.launcher.ConfigurationException;
@@ -71,16 +73,22 @@ public boolean isEditable() {
7173

7274
@Override
7375
public boolean isAvailable() {
74-
return new File(location, "bin").exists() && getLauncherClasspath() != null && isSupportedVersion(); //$NON-NLS-1$
76+
return new File(getLocation(), "bin").exists() && getLauncherClasspath() != null && isSupportedVersion(); //$NON-NLS-1$
7577
}
7678

7779
@Override
7880
public String getLocation() {
81+
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
82+
try {
83+
return manager.performStringSubstitution(location);
84+
} catch(CoreException ex) {
85+
//if we can't parse the location we need to return the unparsed raw value...
86+
}
7987
return location;
8088
}
8189

8290
private File getLauncherConfigurationFile() {
83-
return new File(location, "bin/m2.conf"); //$NON-NLS-1$
91+
return new File(getLocation(), "bin/m2.conf"); //$NON-NLS-1$
8492
}
8593

8694
@Override
@@ -140,7 +148,7 @@ public void setAppMain(String mainClassName, String mainRealmName) {
140148

141149
Properties properties = new Properties();
142150
copyProperties(properties, System.getProperties());
143-
properties.put(PROPERTY_MAVEN_HOME, location);
151+
properties.put(PROPERTY_MAVEN_HOME, getLocation());
144152

145153
ConfigurationParser parser = new ConfigurationParser(handler, properties);
146154

@@ -158,7 +166,7 @@ public void setAppMain(String mainClassName, String mainRealmName) {
158166

159167
@Override
160168
public String toString() {
161-
return location + ' ' + getVersion();
169+
return getLocation() + ' ' + getVersion();
162170
}
163171

164172
private static class ExceptionWrapper extends RuntimeException {
@@ -170,7 +178,7 @@ public ExceptionWrapper(Exception cause) {
170178
}
171179

172180
private String getLauncherClasspath() {
173-
File mavenHome = new File(location);
181+
File mavenHome = new File(getLocation());
174182
DirectoryScanner ds = new DirectoryScanner();
175183
ds.setBasedir(mavenHome);
176184
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) {
255263

256264
Properties properties = new Properties();
257265
copyProperties(properties, System.getProperties());
258-
properties.put(PROPERTY_MAVEN_HOME, location);
266+
properties.put(PROPERTY_MAVEN_HOME, getLocation());
259267

260268
try (FileInputStream is = new FileInputStream(getLauncherConfigurationFile())) {
261269
new ConfigurationParser(handler, properties).parse(is);

0 commit comments

Comments
 (0)