From cc86fe1e2320dc5d1fb3ba54d10e102d8137aa33 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 and preferences for overwrites --- .../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 | 16 ++- java/maven/nbproject/project.properties | 1 - .../modules/maven/api/ModelUtils.java | 11 +- .../maven/customizer/CompilePanel.java | 59 ++++----- .../maven/customizer/SourcesPanel.java | 28 ++-- .../maven/nodes/BootCPNodeFactory.java | 2 +- .../maven/options/MavenVersionSettings.java | 76 ----------- .../options/NbArtifactVersionPreferences.java | 124 ++++++++++++++++++ 12 files changed, 187 insertions(+), 176 deletions(-) delete mode 100644 java/maven/src/org/netbeans/modules/maven/options/MavenVersionSettings.java create mode 100644 java/maven/src/org/netbeans/modules/maven/options/NbArtifactVersionPreferences.java 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..bf6a46f53f45 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.NbArtifactVersionPreferences; 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 NbArtifactVersionPreferences.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..11f3fbc84fe0 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.NbArtifactVersionPreferences; 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) { + NbArtifactVersionPreferences prefs = NbArtifactVersionPreferences.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 df4eaca110c5..5042d8fefb94 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 @@ -38,6 +38,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.NbArtifactVersionPreferences; import org.netbeans.validation.api.Problems; import org.netbeans.validation.api.Severity; import org.netbeans.validation.api.Validator; @@ -144,6 +145,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(NbArtifactVersionPreferences.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 7431a5d01a5e..0c2c7ef7727c 100644 --- a/java/maven/nbproject/org-netbeans-modules-maven.sig +++ b/java/maven/nbproject/org-netbeans-modules-maven.sig @@ -1,5 +1,5 @@ #Signature file v4.1 -#Version 2.159.0 +#Version 2.160.0 CLSS public abstract java.awt.Component cons protected init() @@ -159,6 +159,8 @@ meth public java.awt.im.InputMethodRequests getInputMethodRequests() meth public java.awt.image.ColorModel getColorModel() meth public java.awt.image.VolatileImage createVolatileImage(int,int) meth public java.awt.image.VolatileImage createVolatileImage(int,int,java.awt.ImageCapabilities) throws java.awt.AWTException +meth public java.awt.peer.ComponentPeer getPeer() + anno 0 java.lang.Deprecated() meth public java.beans.PropertyChangeListener[] getPropertyChangeListeners() meth public java.beans.PropertyChangeListener[] getPropertyChangeListeners(java.lang.String) meth public java.lang.String getName() @@ -2361,7 +2363,7 @@ meth public void setUseBestMaven(boolean) meth public void setUseBestMavenAltLocation(boolean) meth public void setVMOptionsWrap(boolean) supr java.lang.Object -hfds INSTANCE,MAVEN_CORE_JAR_PATTERN,PROP_ALWAYS_OUTPUT,PROP_BINARY_DOWNLOAD,PROP_CHECKSUM_POLICY,PROP_COLLAPSE_FOLDS,PROP_DEBUG,PROP_DEFAULT_JDK,PROP_DEFAULT_OPTIONS,PROP_ERRORS,PROP_EXPERIMENTAL_ALTERNATE_LOCATION,PROP_EXPERIMENTAL_USE_ALTERNATE_LOCATION,PROP_EXPERIMENTAL_USE_BEST_MAVEN,PROP_FAILURE_BEHAVIOUR,PROP_JAVADOC_DOWNLOAD,PROP_LAST_ARCHETYPE_GROUPID,PROP_LAST_ARCHETYPE_VERSION,PROP_MAVEN_RUNTIMES,PROP_OUTPUT_TAB_CONFIG,PROP_OUTPUT_TAB_NAME,PROP_PLUGIN_POLICY,PROP_PREFER_WRAPPER,PROP_REUSE_OUTPUT,PROP_SHOW_LOGGING_LEVEL,PROP_SKIP_TESTS,PROP_SOURCE_DOWNLOAD,PROP_USE_REGISTRY,PROP_VM_OPTIONS_WRAP,listeners +hfds DEFAULT_PROXY_BEHAVIOUR,INSTANCE,MAVEN_CORE_JAR_PATTERN,PROP_ALWAYS_OUTPUT,PROP_BINARY_DOWNLOAD,PROP_CHECKSUM_POLICY,PROP_COLLAPSE_FOLDS,PROP_DEBUG,PROP_DEFAULT_JDK,PROP_DEFAULT_OPTIONS,PROP_ERRORS,PROP_EXPERIMENTAL_ALTERNATE_LOCATION,PROP_EXPERIMENTAL_USE_ALTERNATE_LOCATION,PROP_EXPERIMENTAL_USE_BEST_MAVEN,PROP_FAILURE_BEHAVIOUR,PROP_JAVADOC_DOWNLOAD,PROP_LAST_ARCHETYPE_GROUPID,PROP_LAST_ARCHETYPE_VERSION,PROP_MAVEN_RUNTIMES,PROP_OUTPUT_TAB_CONFIG,PROP_OUTPUT_TAB_NAME,PROP_PLUGIN_POLICY,PROP_PREFER_WRAPPER,PROP_REUSE_OUTPUT,PROP_SHOW_LOGGING_LEVEL,PROP_SKIP_TESTS,PROP_SOURCE_DOWNLOAD,PROP_USE_REGISTRY,PROP_VM_OPTIONS_WRAP,SYSPROP_DEFAULT_PROXY_BEHAVIOUR,listeners CLSS public final static !enum org.netbeans.modules.maven.options.MavenSettings$DownloadStrategy outer org.netbeans.modules.maven.options.MavenSettings @@ -2380,16 +2382,16 @@ meth public static org.netbeans.modules.maven.options.MavenSettings$OutputTabNam meth public static org.netbeans.modules.maven.options.MavenSettings$OutputTabName[] values() supr java.lang.Enum -CLSS public final org.netbeans.modules.maven.options.MavenVersionSettings -fld public final static java.lang.String VERSION_COMPILER = "maven-compiler-plugin" -fld public final static java.lang.String VERSION_RESOURCES = "maven-resources-plugin" +CLSS public final org.netbeans.modules.maven.options.NbArtifactVersionPreferences meth protected final java.lang.String getProperty(java.lang.String) meth protected final java.lang.String putProperty(java.lang.String,java.lang.String) meth protected final java.util.prefs.Preferences getPreferences() +meth public java.lang.String getNBVersion() meth public java.lang.String getVersion(java.lang.String) -meth public static org.netbeans.modules.maven.options.MavenVersionSettings getDefault() +meth public java.lang.String getVersion(java.lang.String,java.lang.String) +meth public static org.netbeans.modules.maven.options.NbArtifactVersionPreferences getDefault() supr java.lang.Object -hfds INSTANCE +hfds INSTANCE,fallback CLSS public final !enum org.netbeans.modules.maven.options.NetworkProxySettings fld public final static org.netbeans.modules.maven.options.NetworkProxySettings ASK diff --git a/java/maven/nbproject/project.properties b/java/maven/nbproject/project.properties index 871f4bacb8d7..7f1644e4a799 100644 --- a/java/maven/nbproject/project.properties +++ b/java/maven/nbproject/project.properties @@ -17,7 +17,6 @@ javac.source=1.8 javac.compilerargs=-Xlint -Xlint:-serial -cp.extra=${tools.jar} javadoc.apichanges=${basedir}/apichanges.xml javadoc.arch=${basedir}/arch.xml javahelp.hs=maven.hs diff --git a/java/maven/src/org/netbeans/modules/maven/api/ModelUtils.java b/java/maven/src/org/netbeans/modules/maven/api/ModelUtils.java index 8cd39a621600..a4cdeb219e3a 100644 --- a/java/maven/src/org/netbeans/modules/maven/api/ModelUtils.java +++ b/java/maven/src/org/netbeans/modules/maven/api/ModelUtils.java @@ -24,7 +24,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -56,7 +55,7 @@ import org.netbeans.modules.maven.model.pom.Project; import org.netbeans.modules.maven.model.pom.Repository; import org.netbeans.modules.maven.options.MavenSettings; -import org.netbeans.modules.maven.options.MavenVersionSettings; +import org.netbeans.modules.maven.options.NbArtifactVersionPreferences; import org.netbeans.modules.maven.spi.nodes.NodeUtils; import org.openide.cookies.EditCookie; import org.openide.cookies.LineCookie; @@ -121,7 +120,7 @@ public void performOperation(POMModel model) { } } }; - Utilities.performPOMModelOperations(pom, Collections.singletonList(operation)); + Utilities.performPOMModelOperations(pom, List.of(operation)); } public static Dependency checkModelDependency(POMModel pom, String groupId, String artifactId, boolean add) { @@ -331,7 +330,7 @@ public static void setSourceLevel(POMModel mdl, String sourceLevel) { plugin = mdl.getFactory().createPlugin(); plugin.setGroupId(Constants.GROUP_APACHE_PLUGINS); plugin.setArtifactId(Constants.PLUGIN_COMPILER); - plugin.setVersion(MavenVersionSettings.getDefault().getVersion(MavenVersionSettings.VERSION_COMPILER)); + plugin.setVersion(NbArtifactVersionPreferences.getDefault().getVersion(Constants.PLUGIN_COMPILER)); mdl.getProject().getBuild().addPlugin(plugin); } Configuration conf = plugin.getConfiguration(); @@ -435,8 +434,8 @@ public static Descriptor checkLibraries(Library library) { } //for tests static Descriptor checkLibraries(Map 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..50de3eeb8112 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; @@ -63,12 +58,10 @@ import org.netbeans.modules.maven.model.pom.Plugin; import org.netbeans.modules.maven.model.pom.Properties; import org.netbeans.modules.maven.options.DontShowAgainSettings; -import org.netbeans.modules.maven.options.MavenVersionSettings; +import org.netbeans.modules.maven.options.NbArtifactVersionPreferences; 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(NbArtifactVersionPreferences.getDefault().getVersion(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..b276e9d63cc8 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; @@ -48,7 +46,7 @@ import org.netbeans.modules.maven.model.pom.Plugin; import org.netbeans.modules.maven.model.pom.Project; import org.netbeans.modules.maven.model.pom.Properties; -import org.netbeans.modules.maven.options.MavenVersionSettings; +import org.netbeans.modules.maven.options.NbArtifactVersionPreferences; import org.netbeans.spi.project.ui.support.ProjectCustomizer; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; @@ -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(NbArtifactVersionPreferences.getDefault().getVersion(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(NbArtifactVersionPreferences.getDefault().getVersion(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 deleted file mode 100644 index 9f58ab7b0d08..000000000000 --- a/java/maven/src/org/netbeans/modules/maven/options/MavenVersionSettings.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.netbeans.modules.maven.options; - -import java.util.prefs.Preferences; -import org.openide.util.NbPreferences; - -/** - * Preferences class for externalizing the hardwired plugin versions to - * allow changes by advanced users? - * @author mkleint - */ -public final class MavenVersionSettings { - 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 - - public static MavenVersionSettings getDefault() { - return INSTANCE; - } - - protected final Preferences getPreferences() { - return NbPreferences.root().node("org/netbeans/modules/maven/pluginVersions"); //NOI18N - } - - 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; - } - - protected final String getProperty(String key) { - return getPreferences().get(key, null); - } - - private MavenVersionSettings() { - } - - 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 - } - } - if (toRet == null) { - toRet = "RELEASE"; // this is wrong for 2.1 - } - return toRet; - } - -} diff --git a/java/maven/src/org/netbeans/modules/maven/options/NbArtifactVersionPreferences.java b/java/maven/src/org/netbeans/modules/maven/options/NbArtifactVersionPreferences.java new file mode 100644 index 000000000000..a469d8bf1be2 --- /dev/null +++ b/java/maven/src/org/netbeans/modules/maven/options/NbArtifactVersionPreferences.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.maven.options; + +import java.util.Map; +import java.util.prefs.Preferences; +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 org.openide.util.NbPreferences; + +import static java.util.Map.entry; + +/** + * Preferences class for internal artifact version queries. + * + * Generally returns the latest known version unless overwritten by preference storage. + * + * @author mkleint + */ +public final class NbArtifactVersionPreferences { + + private static final Map fallback; + + static { + // TODO update periodically - modifications might require test updates + 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 NbArtifactVersionPreferences INSTANCE = new NbArtifactVersionPreferences(); + + private NbArtifactVersionPreferences() {} + + public static NbArtifactVersionPreferences getDefault() { + return INSTANCE; + } + + protected final Preferences getPreferences() { + return NbPreferences.root().node("org/netbeans/modules/maven/artifactVersions"); + } + + 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; + } + + protected final String getProperty(String key) { + return getPreferences().get(key, null); + } + + public String getNBVersion() { + return getVersion("org.netbeans.api", "org-netbeans-modules-editor"); + } + + public String getVersion(String aid) { + return getVersion(Constants.GROUP_APACHE_PLUGINS, aid); + } + + public String getVersion(String gid, String aid) { + String key = key(gid, aid); + String custom = getProperty(key); + return custom != null ? custom : queryLatestKnownArtifactVersion(gid, aid, fallback.get(key)); + } + + // 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 (aid == null || aid.isBlank()) { + throw new IllegalArgumentException("empty artifact id"); + } + return gid + ":" + aid; + } + +}