From 0aa2eb3e4f9a9db7b05633b03ad8645acda921a6 Mon Sep 17 00:00:00 2001 From: Nicola Isotta Date: Mon, 22 Jun 2026 18:27:19 +0200 Subject: [PATCH] Fix Maven compile panel to respect Maven default JDK When the Maven default JDK differed from the IDE default JDK, the compile panel's Java Platform combo-box would show the IDE default, while Maven actually used its own configured default to build. This could be misleading to the user e might caused builds to fail. The combo-box now correctly reflects the Maven default JDK and labels it as "(Maven default)" when it differs from the IDE default. Fixes #3928 --- .../maven/customizer/CompilePanel.java | 66 ++++++++++++++++--- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/java/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java b/java/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java index 5eeaf69ee8d0..daf2662f056a 100644 --- a/java/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java +++ b/java/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java @@ -30,12 +30,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; + import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; import javax.swing.JLabel; @@ -45,6 +47,7 @@ import javax.swing.event.ListDataListener; import javax.swing.plaf.UIResource; import javax.xml.namespace.QName; + import org.apache.maven.model.InputLocation; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator; @@ -87,6 +90,7 @@ import static org.netbeans.modules.maven.api.Constants.HINT_JDK_PLATFORM; import static org.netbeans.modules.maven.api.Constants.PLUGIN_COMPILER; import static org.netbeans.modules.maven.api.Constants.SOURCE_PARAM; +import org.netbeans.modules.maven.options.MavenSettings; /** * @@ -96,6 +100,23 @@ public class CompilePanel extends javax.swing.JPanel implements HelpCtx.Provider private static final Logger LOG = Logger.getLogger(CompilePanel.class.getName()); + private static JavaPlatform getDefaultMavenPlatform() { + String mavenDefaultJdk = MavenSettings.getDefault().getDefaultJdk(); + if (mavenDefaultJdk != null && !mavenDefaultJdk.isEmpty()) { + for (JavaPlatform platform : JavaPlatformManager.getDefault().getInstalledPlatforms()) { + if (platform.isValid() && Objects.equals(platform.getProperties().get("platform.ant.name"), mavenDefaultJdk)) { + return platform; + } + } + } + return null; + } + + private static JavaPlatform getDefaultPlatform() { + JavaPlatform defaultMavenPlatform = getDefaultMavenPlatform(); + return defaultMavenPlatform == null ? JavaPlatform.getDefault() : defaultMavenPlatform; + } + private final ModelHandle2 handle; private final Project project; private static boolean warningShown = false; @@ -317,7 +338,7 @@ public Union2 getValue() { } if (val != null) { if (val.equals(DEFAULT_PLATFORM_VALUE)) { - return Union2.createFirst(JavaPlatformManager.getDefault().getDefaultPlatform()); + return Union2.createFirst(getDefaultPlatform()); } return Optional.ofNullable(BootClassPathImpl.getActivePlatform(val)) .filter(JavaPlatform::isValid) @@ -334,7 +355,7 @@ public Union2 getValue() { @Override public Union2 getDefaultValue() { - return Union2.createFirst(JavaPlatformManager.getDefault().getDefaultPlatform()); + return Union2.createFirst(getDefaultPlatform()); } @Override @@ -342,12 +363,12 @@ public void setValue(Union2 value) { handle.removePOMModification(operation); modifiedValue = null; final Union2 platf = value == null ? - Union2.createFirst(JavaPlatformManager.getDefault().getDefaultPlatform()) : + Union2.createFirst(getDefaultPlatform()) : value; final String platformId; if (platf.hasFirst()) { final JavaPlatform jp = platf.first(); - platformId = JavaPlatformManager.getDefault().getDefaultPlatform().equals(jp) ? + platformId = getDefaultPlatform().equals(jp) ? null : jp.getProperties().get("platform.ant.name"); //NOI18N } else { @@ -375,9 +396,15 @@ public void setValue(Union2 value) { platformComboBoxUpdater.ancestorRemoved(null); } - private Pair getSelPlatform () { + private Pair getSelPlatform() { String platformId = project.getLookup().lookup(AuxiliaryProperties.class).get(HINT_JDK_PLATFORM, true); - return Pair.of(platformId,BootClassPathImpl.getActivePlatform(platformId)); + if (platformId == null) { + JavaPlatform defaultMavenPlatform = getDefaultMavenPlatform(); + if (defaultMavenPlatform != null) { + platformId = defaultMavenPlatform.getProperties().get("platform.ant.name"); + } + } + return Pair.of(platformId, BootClassPathImpl.getActivePlatform(platformId)); } @SuppressWarnings("unchecked") @@ -908,6 +935,19 @@ private static POMExtensibilityElement getElement(POMComponent parent, String na return null; } + @NbBundle.Messages({ + "# {0} - platform display name", + "TXT_MavenDefaultPlatformFmt={0} (Maven default)" + }) + private static String getDisplayName(JavaPlatform javaPlatform) { + if (Objects.equals(javaPlatform, getDefaultMavenPlatform()) + && !Objects.equals(javaPlatform, JavaPlatform.getDefault())) { + return Bundle.TXT_MavenDefaultPlatformFmt(javaPlatform.getDisplayName()); + } else { + return javaPlatform.getDisplayName(); + } + } + static class PlatformsModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener { private static final long serialVersionUID = 1L; @@ -988,11 +1028,21 @@ private void getPlatforms(JavaPlatformManager jpm) { } private static String displayName(Union2 item) { - return item.hasFirst() ? item.first().getDisplayName() : item.second(); + if (item.hasFirst()) { + return getDisplayName(item.first()); + } else { + return item.second(); + } } private Pair getSelPlatform() { String platformId = project.getLookup().lookup(AuxiliaryProperties.class).get(HINT_JDK_PLATFORM, true); + if (platformId == null) { + JavaPlatform defaultMavenPlatform = getDefaultMavenPlatform(); + if (defaultMavenPlatform != null) { + platformId = defaultMavenPlatform.getProperties().get("platform.ant.name"); + } + } return Pair.of(platformId, BootClassPathImpl.getActivePlatform(platformId)); } } @@ -1017,7 +1067,7 @@ public Component getListCellRendererComponent(JList list, Object value, if (value instanceof Union2) { final Union2 u2 = (Union2) value; if (u2.hasFirst()) { - strValue = u2.first().getDisplayName(); + strValue = getDisplayName(u2.first()); } else { strValue = "" //NOI18N + Bundle.TXT_BrokenPlatformFmt(u2.second());