From 92080adcae333ba29161ba06432bf9ff9cd39827 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Fri, 24 Nov 2023 22:03:50 +0100 Subject: [PATCH] Centralize internal maven artifact version queries. - put all hardcoded fallback versions in one place - use index for updates if available --- .../maven/apisupport/MavenNbModuleImpl.java | 11 +- .../maven/apisupport/NbmWizardIterator.java | 30 ++--- .../apisupport/NbmWizardPanelVisual.java | 4 + java/maven/manifest.mf | 1 + .../nbproject/org-netbeans-modules-maven.sig | 7 +- java/maven/nbproject/project.properties | 1 - .../modules/maven/api/ModelUtils.java | 9 +- .../maven/customizer/CompilePanel.java | 57 ++++----- .../maven/customizer/SourcesPanel.java | 26 ++-- .../maven/nodes/BootCPNodeFactory.java | 2 +- .../maven/options/MavenVersionSettings.java | 113 +++++++++++------- 11 files changed, 127 insertions(+), 134 deletions(-) diff --git a/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenNbModuleImpl.java b/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenNbModuleImpl.java index 3b46afcd8cfe..9ad8c1cdecfb 100644 --- a/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenNbModuleImpl.java +++ b/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenNbModuleImpl.java @@ -69,6 +69,7 @@ import org.netbeans.modules.maven.model.pom.POMExtensibilityElement; import org.netbeans.modules.maven.model.pom.POMModel; import org.netbeans.modules.maven.model.pom.Plugin; +import org.netbeans.modules.maven.options.MavenVersionSettings; import org.netbeans.spi.project.AuxiliaryProperties; import org.netbeans.spi.project.ProjectServiceProvider; import org.netbeans.spi.project.ui.ProjectOpenedHook; @@ -102,7 +103,6 @@ public class MavenNbModuleImpl implements NbModuleProvider { public static final String GROUPID_MOJO = "org.codehaus.mojo"; public static final String GROUPID_APACHE = "org.apache.netbeans.utilities"; public static final String NBM_PLUGIN = "nbm-maven-plugin"; - static final String LATEST_NBM_PLUGIN_VERSION = "4.8"; public static final String NETBEANSAPI_GROUPID = "org.netbeans.api"; @@ -123,14 +123,7 @@ static List netbeansRepo() { * This method will not wait for the index to be downloaded, it will return a default value instead. */ public static String getLatestNbmPluginVersion() { - RepositoryQueries.Result versionsResult = RepositoryQueries.getVersionsResult(GROUPID_APACHE, NBM_PLUGIN, null); - - // Versions are sorted in descending order - return versionsResult.getResults().stream() - .map(NBVersionInfo::getVersion) - .filter(v -> !v.endsWith("-SNAPSHOT")) - .findFirst() - .orElse(LATEST_NBM_PLUGIN_VERSION); + return MavenVersionSettings.getDefault().getVersion(GROUPID_APACHE, NBM_PLUGIN); } private File getModuleXmlLocation() { diff --git a/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardIterator.java b/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardIterator.java index 2c1f1723bc67..916d4708e534 100644 --- a/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardIterator.java +++ b/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardIterator.java @@ -30,7 +30,6 @@ import java.util.Set; import javax.swing.JComponent; import javax.swing.event.ChangeListener; -import org.apache.maven.artifact.versioning.ComparableVersion; import org.apache.maven.project.MavenProject; import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectManager; @@ -52,29 +51,26 @@ import static org.netbeans.modules.maven.apisupport.Bundle.*; import static org.netbeans.modules.maven.apisupport.MavenNbModuleImpl.APACHE_SNAPSHOT_REPO_ID; import org.netbeans.modules.maven.embedder.EmbedderFactory; -import org.netbeans.modules.maven.indexer.api.NBVersionInfo; -import org.netbeans.modules.maven.indexer.api.RepositoryQueries; import org.netbeans.modules.maven.model.pom.Plugin; import org.netbeans.modules.maven.model.pom.PluginManagement; +import org.netbeans.modules.maven.options.MavenVersionSettings; import org.netbeans.spi.project.ui.support.CommonProjectActions; public class NbmWizardIterator implements WizardDescriptor.BackgroundInstantiatingIterator { public static final String NBM_ARTIFACTID = "nbm_artifactId"; - - static final Archetype NB_MODULE_ARCH, NB_APP_ARCH; public static final String SNAPSHOT_VERSION = "dev-SNAPSHOT"; + + static final Archetype NB_MODULE_ARCH; + static final Archetype NB_APP_ARCH; static { NB_MODULE_ARCH = new Archetype(); NB_MODULE_ARCH.setGroupId("org.apache.netbeans.archetypes"); //NOI18N - NB_MODULE_ARCH.setVersion("1.18"); //NOI18N NB_MODULE_ARCH.setArtifactId("nbm-archetype"); //NOI18N NB_APP_ARCH = new Archetype(); NB_APP_ARCH.setGroupId("org.apache.netbeans.archetypes"); //NOI18N - NB_APP_ARCH.setVersion("1.23"); //NOI18N NB_APP_ARCH.setArtifactId("netbeans-platform-app-archetype"); //NOI18N - } static final String OSGIDEPENDENCIES = "osgi.dependencies"; @@ -120,20 +116,9 @@ private String[] createSteps() { } // non blocking - private static void updateToLatestKnownArchetypeVersion(Archetype archetype) { - RepositoryQueries.Result versionsResult = RepositoryQueries.getVersionsResult(archetype.getGroupId(), archetype.getArtifactId(), null); - - // Versions are sorted in descending order - List results = versionsResult.getResults(); - for (NBVersionInfo result : results) { - String betterVersion = result.getVersion(); - if (!betterVersion.contains("SNAPSHOT")) { // skip snapshots - if (new ComparableVersion(betterVersion).compareTo(new ComparableVersion(archetype.getVersion())) > 0) { - archetype.setVersion(betterVersion); - } - return; - } - } + private static void updateToLatestKnownArchetypeVersion(Archetype arch) { + MavenVersionSettings prefs = MavenVersionSettings.getDefault(); + arch.setVersion(prefs.getVersion(arch.getGroupId(), arch.getArtifactId())); } @Override @@ -151,7 +136,6 @@ public Set instantiate() throws IOException { if (archetype == NB_MODULE_ARCH) { updateToLatestKnownArchetypeVersion(NB_MODULE_ARCH); NBMNativeMWI.instantiate(vi, projFile, version, Boolean.TRUE.equals(wiz.getProperty(OSGIDEPENDENCIES)), null); - } else { updateToLatestKnownArchetypeVersion(NB_APP_ARCH); ArchetypeWizards.createFromArchetype(projFile, vi, archetype, additional, true); diff --git a/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java b/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java index 8b16d6d15fbc..56ce5ab131c6 100644 --- a/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java +++ b/apisupport/maven.apisupport/src/org/netbeans/modules/maven/apisupport/NbmWizardPanelVisual.java @@ -39,6 +39,7 @@ import org.netbeans.modules.maven.indexer.api.RepositoryPreferences; import org.netbeans.modules.maven.indexer.api.RepositoryQueries; import org.netbeans.modules.maven.indexer.api.RepositoryQueries.Result; +import org.netbeans.modules.maven.options.MavenVersionSettings; import org.netbeans.validation.api.Problems; import org.netbeans.validation.api.Severity; import org.netbeans.validation.api.Validator; @@ -152,6 +153,9 @@ private static List filterVersions(Result result) { .filter((v) -> !IGNORE_RELEASES.contains(v)) .sorted((v1, v2) -> v2.compareTo(v1)) .collect(Collectors.toCollection(ArrayList::new)); // must be mutable + if (versions.isEmpty()) { + versions.add(MavenVersionSettings.getDefault().getNBVersion()); // add a fallback version + } versions.add(NbmWizardIterator.SNAPSHOT_VERSION); return versions; } diff --git a/java/maven/manifest.mf b/java/maven/manifest.mf index 2ee0fa61622e..47135db76d60 100644 --- a/java/maven/manifest.mf +++ b/java/maven/manifest.mf @@ -6,3 +6,4 @@ OpenIDE-Module-Layer: org/netbeans/modules/maven/layer.xml AutoUpdate-Show-In-Client: false OpenIDE-Module-Package-Dependencies: com.sun.jdi[VirtualMachineManager] OpenIDE-Module-Recommends: org.netbeans.modules.maven.archetype +OpenIDE-Module-Java-Dependencies: Java > 11 diff --git a/java/maven/nbproject/org-netbeans-modules-maven.sig b/java/maven/nbproject/org-netbeans-modules-maven.sig index fe5482ecf90b..6c8239a5728a 100644 --- a/java/maven/nbproject/org-netbeans-modules-maven.sig +++ b/java/maven/nbproject/org-netbeans-modules-maven.sig @@ -2383,13 +2383,12 @@ supr java.lang.Enum properties) { - List libs = new ArrayList(); - List reps = new ArrayList(); + List libs = new ArrayList<>(); + List reps = new ArrayList<>(); String dependencies = properties.get(LIBRARY_PROP_DEPENDENCIES); if (dependencies != null) { 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 6f13408efdaf..9185f8ec7988 100644 --- a/java/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java +++ b/java/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java @@ -21,14 +21,9 @@ import java.awt.Color; import java.awt.Component; -import java.awt.Cursor; import java.awt.EventQueue; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.net.MalformedURLException; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -67,8 +62,6 @@ import org.netbeans.spi.project.AuxiliaryProperties; import org.openide.DialogDisplayer; import org.openide.NotifyDescriptor; -import org.openide.awt.HtmlBrowser; -import org.openide.util.Exceptions; import org.openide.util.HelpCtx; import org.openide.util.NbBundle; import org.openide.util.Pair; @@ -511,33 +504,33 @@ public String getValue() { @Override public void performOperation(POMModel model) { - Plugin old = null; - Plugin plugin; - Build bld = model.getProject().getBuild(); - if (bld != null) { - old = bld.findPluginById(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER); - } else { - bld = model.getFactory().createBuild(); - model.getProject().setBuild(bld); - } - if (old != null) { - plugin = old; - } else { - plugin = model.getFactory().createPlugin(); - plugin.setGroupId(Constants.GROUP_APACHE_PLUGINS); - plugin.setArtifactId(Constants.PLUGIN_COMPILER); - plugin.setVersion(MavenVersionSettings.getDefault().getVersion(MavenVersionSettings.VERSION_COMPILER)); - bld.addPlugin(plugin); - } - Configuration config = plugin.getConfiguration(); - if (config == null) { - config = model.getFactory().createConfiguration(); - plugin.setConfiguration(config); + Plugin old = null; + Plugin plugin; + Build bld = model.getProject().getBuild(); + if (bld != null) { + old = bld.findPluginById(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER); + } else { + bld = model.getFactory().createBuild(); + model.getProject().setBuild(bld); + } + if (old != null) { + plugin = old; + } else { + plugin = model.getFactory().createPlugin(); + plugin.setGroupId(Constants.GROUP_APACHE_PLUGINS); + plugin.setArtifactId(Constants.PLUGIN_COMPILER); + plugin.setVersion(MavenVersionSettings.getDefault().getVersion(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER)); + bld.addPlugin(plugin); + } + Configuration config = plugin.getConfiguration(); + if (config == null) { + config = model.getFactory().createConfiguration(); + plugin.setConfiguration(config); + } + config.setSimpleParameter(param, value); } - config.setSimpleParameter(param, value); - } - } + } String getCompilerParam(ModelHandle2 handle, String param) { CompilerParamOperation oper = operations.get(param); diff --git a/java/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java b/java/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java index 2b6203e9b3a1..c388131e4dfe 100644 --- a/java/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java +++ b/java/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java @@ -20,8 +20,6 @@ package org.netbeans.modules.maven.customizer; import java.awt.Font; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.io.File; import java.nio.charset.Charset; import java.util.ArrayList; @@ -150,7 +148,7 @@ public void performOperation(POMModel model) { plugin = fact.createPlugin(); plugin.setGroupId(Constants.GROUP_APACHE_PLUGINS); plugin.setArtifactId(Constants.PLUGIN_COMPILER); - plugin.setVersion(MavenVersionSettings.getDefault().getVersion(MavenVersionSettings.VERSION_COMPILER)); + plugin.setVersion(MavenVersionSettings.getDefault().getVersion(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER)); bld.addPlugin(plugin); } if (plugin != null) { @@ -169,7 +167,7 @@ public void performOperation(POMModel model) { plugin2 = fact.createPlugin(); plugin2.setGroupId(Constants.GROUP_APACHE_PLUGINS); plugin2.setArtifactId(Constants.PLUGIN_RESOURCES); - plugin2.setVersion(MavenVersionSettings.getDefault().getVersion(MavenVersionSettings.VERSION_RESOURCES)); + plugin2.setVersion(MavenVersionSettings.getDefault().getVersion(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_RESOURCES)); bld.addPlugin(plugin2); } if (plugin2 != null) { @@ -225,20 +223,10 @@ public SourcesPanel( ModelHandle2 handle, NbMavenProjectImpl project, MavenProje comEncoding.setModel(ProjectCustomizer.encodingModel(oldEncoding)); comEncoding.setRenderer(ProjectCustomizer.encodingRenderer()); - - comSourceLevel.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - handleSourceLevelChange(); - } - }); - - comEncoding.addActionListener(new ActionListener () { - @Override - public void actionPerformed(ActionEvent e) { - handleEncodingChange(); - } - }); + + comSourceLevel.addActionListener(e -> handleSourceLevelChange()); + comEncoding.addActionListener(e -> handleEncodingChange()); + txtSrc.setText(handle.getProject().getBuild().getSourceDirectory()); txtTestSrc.setText(handle.getProject().getBuild().getTestSourceDirectory()); } @@ -502,7 +490,7 @@ private synchronized String[] getSourceLevels() { current = incJavaSpecVersion(current); } } - sourceLevelCache = sourceLevels.toArray(new String[sourceLevels.size()]); + sourceLevelCache = sourceLevels.toArray(String[]::new); } return sourceLevelCache; } diff --git a/java/maven/src/org/netbeans/modules/maven/nodes/BootCPNodeFactory.java b/java/maven/src/org/netbeans/modules/maven/nodes/BootCPNodeFactory.java index 508cbd3b856c..ba3ec09f889a 100644 --- a/java/maven/src/org/netbeans/modules/maven/nodes/BootCPNodeFactory.java +++ b/java/maven/src/org/netbeans/modules/maven/nodes/BootCPNodeFactory.java @@ -69,7 +69,7 @@ public class BootCPNodeFactory implements NodeFactory { Collections.emptyList() : Collections.singletonList(null); } - @Override public Node node(Void _) { + @Override public Node node(Void v) { return new BootCPNode(p); } }; diff --git a/java/maven/src/org/netbeans/modules/maven/options/MavenVersionSettings.java b/java/maven/src/org/netbeans/modules/maven/options/MavenVersionSettings.java index 9f58ab7b0d08..08259d0c70a3 100644 --- a/java/maven/src/org/netbeans/modules/maven/options/MavenVersionSettings.java +++ b/java/maven/src/org/netbeans/modules/maven/options/MavenVersionSettings.java @@ -18,59 +18,92 @@ */ package org.netbeans.modules.maven.options; -import java.util.prefs.Preferences; -import org.openide.util.NbPreferences; +import java.util.Map; +import org.apache.maven.artifact.versioning.ComparableVersion; +import org.netbeans.modules.maven.api.Constants; +import org.netbeans.modules.maven.indexer.api.NBVersionInfo; +import org.netbeans.modules.maven.indexer.api.RepositoryQueries; + +import static java.util.Map.entry; /** - * Preferences class for externalizing the hardwired plugin versions to - * allow changes by advanced users? - * @author mkleint + * Utility class for internal artifact version queries. + * + * Will usually return the latest known version. + * + * @author mbien */ public final class MavenVersionSettings { + + @Deprecated + public static final String VERSION_COMPILER = Constants.PLUGIN_COMPILER; //NOI18N + @Deprecated + public static final String VERSION_RESOURCES = Constants.PLUGIN_RESOURCES; //NOI18N + + private static final Map fallback; + + static { + // TODO update periodically - modifications might require unit test adjustments + String nb_version = "RELEASE200"; + String nb_utilities_version = "14.0"; + fallback = Map.ofEntries( + entry(key("org.netbeans.api", "org-netbeans-modules-editor"), nb_version), // represents all other nb artifacts + entry(key(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER), "3.11.0"), + entry(key(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_RESOURCES), "3.3.1"), + entry(key(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_FAILSAFE), "3.2.2"), + entry(key(Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_SUREFIRE), "3.2.2"), + entry(key("org.apache.netbeans.utilities", "utilities-parent"), nb_utilities_version), + entry(key("org.apache.netbeans.utilities", "nbm-maven-harness"), nb_utilities_version), + entry(key("org.apache.netbeans.utilities", "nbm-shared"), nb_utilities_version), + entry(key("org.apache.netbeans.utilities", "nbm-repository-plugin"), nb_utilities_version), + entry(key("org.apache.netbeans.utilities", "nbm-maven-plugin"), nb_utilities_version), + entry(key("org.apache.netbeans.archetypes", "nbm-archetype"), "1.18"), + entry(key("org.apache.netbeans.archetypes", "netbeans-platform-app-archetype"), "1.23") + ); + } + private static final MavenVersionSettings INSTANCE = new MavenVersionSettings(); - - public static final String VERSION_COMPILER = "maven-compiler-plugin"; //NOI18N - public static final String VERSION_RESOURCES = "maven-resources-plugin"; //NOI18N - + + private MavenVersionSettings() {} + public static MavenVersionSettings getDefault() { return INSTANCE; } - - protected final Preferences getPreferences() { - return NbPreferences.root().node("org/netbeans/modules/maven/pluginVersions"); //NOI18N + + public String getNBVersion() { + return getVersion("org.netbeans.api", "org-netbeans-modules-editor"); } - - protected final String putProperty(String key, String value) { - String retval = getProperty(key); - if (value != null) { - getPreferences().put(key, value); - } else { - getPreferences().remove(key); - } - return retval; + + @Deprecated + public String getVersion(String artifactId) { + return getVersion(Constants.GROUP_APACHE_PLUGINS, artifactId); } - protected final String getProperty(String key) { - return getPreferences().get(key, null); - } - - private MavenVersionSettings() { + public String getVersion(String groupId, String artifactId) { + String key = key(groupId, artifactId); + return queryLatestKnownArtifactVersion(groupId, artifactId, fallback.get(key)); } - - public String getVersion(String plugin) { - String toRet = getProperty(plugin); - if (toRet == null) { - // XXX these should rather read the most recent version from the repository index (agreed) - if (VERSION_RESOURCES.equals(plugin)) { - toRet = "3.3.1"; //NOI18N - } else if (VERSION_COMPILER.equals(plugin)) { - toRet = "3.11.0"; //NOI18N - } + + // non blocking query, might not succeed if index not available + private static String queryLatestKnownArtifactVersion(String gid, String aid, String min) { + RepositoryQueries.Result query = RepositoryQueries.getVersionsResult(gid, aid, null); + // Versions are sorted in descending order + return query.getResults().stream() + .map(NBVersionInfo::getVersion) + .filter(v -> !v.endsWith("-SNAPSHOT")) + .findFirst() + .filter(v -> min == null || new ComparableVersion(v).compareTo(new ComparableVersion(min)) > 0) // don't downgrade + .orElse(min); + } + + private static String key(String gid, String aid) { + if (gid == null || gid.isBlank()) { + throw new IllegalArgumentException("empty group id"); } - if (toRet == null) { - toRet = "RELEASE"; // this is wrong for 2.1 + if (aid == null || aid.isBlank()) { + throw new IllegalArgumentException("empty artifact id"); } - return toRet; + return gid + ":" + aid; } - + }