Skip to content

Commit

Permalink
Configure Maven JRE separately
Browse files Browse the repository at this point in the history
The value is automatically derived from maven-enforcer rule requireJava

This closes eclipse-m2e#1134
This closes eclipse-m2e#1099
  • Loading branch information
kwin committed Dec 12, 2022
1 parent f9551f4 commit 23eee3e
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 100 deletions.
2 changes: 2 additions & 0 deletions org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Require-Bundle: org.eclipse.m2e.core;bundle-version="[2.0.0,3.0.0)",
org.eclipse.core.filebuffers,
org.eclipse.ui
Import-Package: org.eclipse.compare.rangedifferencer,
org.eclipse.jdt.core,
org.eclipse.jdt.launching,
org.eclipse.ltk.core.refactoring,
org.slf4j;version="[1.7.0,3.0.0)"
Service-Component: OSGI-INF/component.xml,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,18 @@ public class Messages extends NLS {

public static String MavenProjectPreferencePage_title;

public static String MavenProjectPreferencePage_defaultWorkspaceJRE;

public static String MavenProjectPreferencePage_defaultProjectJRE;

public static String MavenProjectPreferencePage_mavenJRE;

public static String MavenProjectPreferencePage_alternateJRE;

public static String MavenProjectPreferencePage_btnInstalledJREs;

public static String MavenProjectPreferencePage_jreNotSet;

public static String MavenProjectWizardArchetypePage_add_title;

public static String MavenProjectWizardArchetypePage_all;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ MavenProjectPreferencePage_dialog_title=Maven Settings
MavenProjectPreferencePage_job=Updating {0} Sources
MavenProjectPreferencePage_lblProfiles=Active Maven &Profiles (comma separated)\:
MavenProjectPreferencePage_title=Maven
MavenProjectPreferencePage_defaultWorkspaceJRE=Default Workspace JRE ({0})
MavenProjectPreferencePage_defaultProjectJRE=Default Project JRE ({0})
MavenProjectPreferencePage_mavenJRE=Maven JRE:
MavenProjectPreferencePage_alternateJRE=Alternate JRE:
MavenProjectPreferencePage_btnInstalledJREs=Installed JREs...
MavenProjectPreferencePage_jreNotSet=Not set
MavenProjectWizardArchetypePage_add_title=Add Archetype
MavenProjectWizardArchetypePage_all=All Catalogs
MavenProjectWizardArchetypePage_btnAdd=&Add Archetype...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

package org.eclipse.m2e.core.ui.internal.preferences;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -22,16 +28,31 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstallType;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMStandin;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.dialogs.PropertyPage;

import org.eclipse.m2e.core.MavenPlugin;
Expand All @@ -54,10 +75,26 @@ public class MavenProjectPreferencePage extends PropertyPage {

// private Button includeModulesButton;

//https://git.eclipse.org/c/jdt/eclipse.jdt.debug.git/tree/org.eclipse.jdt.debug.ui/plugin.xml#n2467
private static final String INSTALLED_JRE_PAGE = "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"; //$NON-NLS-1$

private Text selectedProfilesText;

private Button defaultJRERadioButton;

private Button alternateJREsRadioButton;

private Combo alternateJREsCombo;

/**
* VMs being displayed
*/
private List<? extends IVMInstall> vmInstalls;

public MavenProjectPreferencePage() {
setTitle(Messages.MavenProjectPreferencePage_title);
vmInstalls = getInstalledJREs();

}

@Override
Expand Down Expand Up @@ -98,21 +135,176 @@ protected Control createContents(Composite parent) {
// + "source folders from nested modules are added to the current "
// + "project build path (use \"Update Sources\" action)");

init(getResolverConfiguration());
createJREControls(composite);

init(getResolverConfiguration());
return composite;
}

private IJavaProject getCurrentJavaProject() {
IProject project = getElement().getAdapter(IProject.class);
if(project != null) {
IJavaProject javaProject = JavaCore.create(project);
if(javaProject.exists()) {
return javaProject;
}
}
return null;
}

private String getDefaultJreName() {
IJavaProject javaProject = getCurrentJavaProject();
String jreName;
if(javaProject == null) {
IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
if(vmInstall == null) {
jreName = Messages.MavenProjectPreferencePage_jreNotSet;
} else {
jreName = vmInstall.getName();
}
return NLS.bind(Messages.MavenProjectPreferencePage_defaultWorkspaceJRE, jreName);
}
try {
IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
jreName = vmInstall.getName();
} catch(CoreException ex) {
jreName = "unknown";
}
return NLS.bind(Messages.MavenProjectPreferencePage_defaultProjectJRE, jreName);
}

/**
* Creates JRE selection controls. Inspired by //
* (https://github.com/eclipse-jdt/eclipse.jdt.debug/blob/0c8c6d9fa30cda3c83741613f6ea4c541c037973/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsComboBlock.java)
*
* @param container the container
*/
private void createJREControls(Composite container) {
Group group = new Group(container, SWT.LEFT);
group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
group.setLayout(new GridLayout(1, false));
group.setText(Messages.MavenProjectPreferencePage_mavenJRE);

Composite composite = new Composite(group, SWT.FILL);
composite.setLayout(new GridLayout(3, false));
composite.setLayoutData(new GridData(GridData.FILL_BOTH));

createDefaultJREControls(composite);
createAlternateJREControls(composite);
}

private void createDefaultJREControls(Composite comp) {
defaultJRERadioButton = new Button(comp, SWT.RADIO);
defaultJRERadioButton.setText(getDefaultJreName());
defaultJRERadioButton.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false, 3, 1));
defaultJRERadioButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(defaultJRERadioButton.getSelection()) {
alternateJREsCombo.setEnabled(false);
}
}
});
}

public void createAlternateJREControls(Composite comp) {
alternateJREsRadioButton = new Button(comp, SWT.RADIO);
alternateJREsRadioButton.setText(Messages.MavenProjectPreferencePage_alternateJRE);
alternateJREsRadioButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(alternateJREsRadioButton.getSelection()) {
alternateJREsCombo.setEnabled(true);
}
}
});

alternateJREsCombo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
alternateJREsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
alternateJREsCombo.setItems(vmInstalls.stream().map(IVMInstall::getName).toArray(String[]::new));
alternateJREsCombo.setVisibleItemCount(Math.min(vmInstalls.size(), 20));
alternateJREsCombo.setEnabled(false);

Button installedJREsButton = new Button(comp, SWT.PUSH);
installedJREsButton.setText(Messages.MavenProjectPreferencePage_btnInstalledJREs);
installedJREsButton.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
if(showPreferencePage(INSTALLED_JRE_PAGE, comp.getShell()) == Window.OK) {
vmInstalls = getInstalledJREs();
alternateJREsCombo.setItems(vmInstalls.stream().map(IVMInstall::getName).toArray(String[]::new));
}
}
});
}

/**
* Displays the given preference page
*
* @param pageId the fully qualified id of the preference page, e.g.
* <code>org.eclipse.jdt.debug.ui.preferences.VMPreferencePage</code>
* @since 3.3
*/
public static int showPreferencePage(String pageId, Shell shell) {
return PreferencesUtil.createPreferenceDialogOn(shell, pageId, new String[] {pageId}, null).open();
}


/**
* @return the selected alternate JRE or <code>null</code> if none.
*/
private IVMInstall getJRE() {
if(alternateJREsRadioButton.getSelection()) {
int index = alternateJREsCombo.getSelectionIndex();
if(index >= 0) {
return vmInstalls.get(index);
}
}
return null;
}

public static List<? extends IVMInstall> getInstalledJREs() {
List<VMStandin> standins = new ArrayList<>();
for (IVMInstallType type : JavaRuntime.getVMInstallTypes()) {
for (IVMInstall install : type.getVMInstalls()) {
standins.add(new VMStandin(install));
}
}
// sort by name
Collections.sort(standins, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
IVMInstall left = (IVMInstall) o1;
IVMInstall right = (IVMInstall) o2;
return left.getName().compareToIgnoreCase(right.getName());
}

@Override
public boolean equals(Object obj) {
return obj == this;
}
});
return standins;
}

@Override
protected void performDefaults() {
init(new ResolverConfiguration());
}

private void init(IProjectConfiguration configuration) {

resolveWorspaceProjectsButton.setSelection(configuration.isResolveWorkspaceProjects());
// includeModulesButton.setSelection(configuration.shouldIncludeModules());
selectedProfilesText.setText(configuration.getSelectedProfiles());
IVMInstall vmInstall = configuration.getMavenJre();
if(vmInstall != null) {
alternateJREsRadioButton.setSelection(true);
alternateJREsCombo.select(vmInstalls.indexOf(vmInstall));
alternateJREsCombo.setEnabled(true);
} else {
defaultJRERadioButton.setSelection(true);
alternateJREsCombo.setEnabled(false);
}
}

@Override
Expand All @@ -130,14 +322,15 @@ public boolean performOk() {
final ResolverConfiguration configuration = new ResolverConfiguration(getResolverConfiguration());
if(configuration.getSelectedProfiles().equals(selectedProfilesText.getText()) &&
// configuration.shouldIncludeModules()==includeModulesButton.getSelection() &&
configuration.isResolveWorkspaceProjects() == resolveWorspaceProjectsButton.getSelection()) {
configuration.isResolveWorkspaceProjects() == resolveWorspaceProjectsButton.getSelection()
&& Objects.equals(configuration.getMavenJre(), getJRE())) {
return true;
}

configuration.setResolveWorkspaceProjects(resolveWorspaceProjectsButton.getSelection());
// configuration.setIncludeModules(includeModulesButton.getSelection());
configuration.setSelectedProfiles(selectedProfilesText.getText());

configuration.setMavenJRE(getJRE());
IProjectConfigurationManager projectManager = MavenPlugin.getProjectConfigurationManager();
boolean isSet = projectManager.setResolverConfiguration(getProject(), configuration);
if(isSet) {
Expand Down
2 changes: 2 additions & 0 deletions org.eclipse.m2e.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ MavenArtifact-ArtifactId: org.eclipse.m2e.core
Import-Package: com.google.common.base;version="30.1.0",
com.google.common.cache,
javax.inject;version="1.0.0",
org.eclipse.jdt.core,
org.eclipse.jdt.launching,
org.slf4j;version="[1.7.0,3.0.0)",
org.slf4j.spi;version="[1.7.31,3.0.0)"
Automatic-Module-Name: org.eclipse.m2e.core
Expand Down
Loading

0 comments on commit 23eee3e

Please sign in to comment.