This is an automated email from the ASF dual-hosted git repository.

mbien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 71172ed00a try to infer compiler plugin version from active maven 
version.
     new 6c21c0619e Merge pull request #5693 from 
mbien/fix-maven-compiler-plugin-checks
71172ed00a is described below

commit 71172ed00aec10f18e05575066f94bad1f901482
Author: Michael Bien <[email protected]>
AuthorDate: Tue Mar 21 17:51:54 2023 +0100

    try to infer compiler plugin version from active maven version.
    
    The plugin version query returns the version of the embedded maven
    distribution and not the verison of the active distribution.
    
    This worked fine until maven updated the versions of their implicitly
    provided lifecycle plugins (which is a good thing), then it broke.
    
    We can workaround this problem by comparing maven versions, since
    we can infer the plugin version from it under certain conditions.
    
    Fixes the release option and the module-info hint.
---
 .../hints/pom/CompilerPluginVersionError.java      | 53 +++++++++++++++-------
 .../modules/maven/hints/pom/PomModelUtils.java     | 17 +++++++
 .../maven/hints/pom/UseReleaseOptionHint.java      | 50 +++++++++++++++-----
 .../maven/hints/pom/UseReleaseOptionHintTest.java  | 19 ++++++--
 java/maven/nbproject/project.xml                   |  1 +
 .../modules/maven/api/ModuleInfoUtils.java         |  4 +-
 6 files changed, 111 insertions(+), 33 deletions(-)

diff --git 
a/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/CompilerPluginVersionError.java
 
b/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/CompilerPluginVersionError.java
index b498940718..e48591e9a9 100644
--- 
a/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/CompilerPluginVersionError.java
+++ 
b/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/CompilerPluginVersionError.java
@@ -18,17 +18,19 @@
  */
 package org.netbeans.modules.maven.hints.pom;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.prefs.Preferences;
 import javax.swing.JComponent;
 import javax.swing.text.Document;
+import org.apache.maven.artifact.versioning.ComparableVersion;
 import org.netbeans.api.project.Project;
 import org.netbeans.modules.editor.NbEditorUtilities;
 import org.netbeans.modules.maven.api.Constants;
 import org.netbeans.modules.maven.api.ModelUtils;
 import org.netbeans.modules.maven.api.ModuleInfoUtils;
+import org.netbeans.modules.maven.api.NbMavenProject;
+import org.netbeans.modules.maven.api.PluginPropertyUtils;
 import org.netbeans.modules.maven.hints.pom.spi.Configuration;
 import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
 import org.netbeans.modules.maven.model.pom.Build;
@@ -50,6 +52,9 @@ import org.openide.util.NbBundle;
 public class CompilerPluginVersionError implements POMErrorFixProvider {
     private final Configuration configuration;
     
+    private static final ComparableVersion COMPILER_PLUGIN_VERSION = new 
ComparableVersion("3.6.0"); // min version for module-info support
+    private static final ComparableVersion MAVEN_VERSION = new 
ComparableVersion("3.9.0"); // added the required compiler plugin implicitly
+    
     @NbBundle.Messages({
         "TIT_WrongCompilerVersion=Wrong maven-compiler-plugin version.",
         "DESC_ModulesNotSupported=Modules are not supported with 
maven-compiler-plugin < 3.6."})
@@ -64,22 +69,39 @@ public class CompilerPluginVersionError implements 
POMErrorFixProvider {
     @Override
     public List<ErrorDescription> getErrorsForDocument(POMModel model, Project 
prj) {
         assert model != null;
-        List<ErrorDescription> toRet = new ArrayList<ErrorDescription>();
         
         if(prj == null) {
-            return toRet;
+            return Collections.emptyList();
         }
         
-        if(ModuleInfoUtils.checkModuleInfoAndCompilerFit(prj)) {
-            return toRet;
+        NbMavenProject nbproject = 
prj.getLookup().lookup(NbMavenProject.class);
+        if (nbproject == null || !ModuleInfoUtils.hasModuleInfo(nbproject)) {
+            return Collections.emptyList();
         }
-                
+
+        // check implicit version by infering it from the maven version
+        ComparableVersion mavenVersion = PomModelUtils.getActiveMavenVersion();
+        if (mavenVersion != null && mavenVersion.compareTo(MAVEN_VERSION) >= 
0) {
+            // note: this is the embedded plugin version
+            // however, if this version here is compatible too we can exit, 
since we know it is not a downgrade and maven itself is compatible
+            String version = 
PluginPropertyUtils.getPluginVersion(nbproject.getMavenProject(), 
Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER);
+            if (new 
ComparableVersion(version).compareTo(COMPILER_PLUGIN_VERSION) >= 0) {
+                return Collections.emptyList();
+            }
+        }
+        
         int pos = -1;
         org.netbeans.modules.maven.model.pom.Project p = model.getProject();
         Build bld = p.getBuild();
         if (bld != null) {
             Plugin plg = bld.findPluginById(Constants.GROUP_APACHE_PLUGINS, 
Constants.PLUGIN_COMPILER);
+            
             if (plg != null) {
+                String version = plg.getVersion();
+                if (version != null && new 
ComparableVersion(version).compareTo(COMPILER_PLUGIN_VERSION) >= 0) {
+                    return Collections.emptyList();
+                }
+                
                 pos = plg.findPosition();
             }
         }    
@@ -89,14 +111,14 @@ public class CompilerPluginVersionError implements 
POMErrorFixProvider {
         }
         
         if(pos == -1) {
-            return toRet;
+            return Collections.emptyList();
         }        
                
         Document baseDocument = model.getBaseDocument();        
         Line line = NbEditorUtilities.getLine(baseDocument, pos, false);
-        
toRet.add(ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, 
Bundle.DESC_ModulesNotSupported(), Collections.<Fix>singletonList(new 
UpdatePluginVersion(model)), baseDocument, line.getLineNumber() + 1));
-        
-        return toRet;
+        return Collections.singletonList(
+                ErrorDescriptionFactory.createErrorDescription(
+                        Severity.ERROR, Bundle.DESC_ModulesNotSupported(), 
Collections.singletonList(new UpdatePluginVersion(model)), baseDocument, 
line.getLineNumber() + 1));
     }
 
     @Override
@@ -138,13 +160,10 @@ public class CompilerPluginVersionError implements 
POMErrorFixProvider {
                 return info;
             }
             
-            PomModelUtils.implementInTransaction(mdl, new Runnable() {
-                @Override
-                public void run() {
-                    org.netbeans.modules.maven.model.pom.Project prj = 
mdl.getProject();
-                    
ModelUtils.updatePluginVersion(Constants.GROUP_APACHE_PLUGINS, 
Constants.PLUGIN_COMPILER, "3.6.1", prj);
-                    ModelUtils.openAtPlugin(mdl, 
Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER);
-                }
+            PomModelUtils.implementInTransaction(mdl, () -> {
+                org.netbeans.modules.maven.model.pom.Project prj = 
mdl.getProject();
+                ModelUtils.updatePluginVersion(Constants.GROUP_APACHE_PLUGINS, 
Constants.PLUGIN_COMPILER, "3.6.1", prj);
+                ModelUtils.openAtPlugin(mdl, Constants.GROUP_APACHE_PLUGINS, 
Constants.PLUGIN_COMPILER);
             });
             
             return info;
diff --git 
a/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/PomModelUtils.java 
b/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/PomModelUtils.java
index 0e97c096a1..f8f44e3aed 100644
--- 
a/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/PomModelUtils.java
+++ 
b/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/PomModelUtils.java
@@ -37,6 +37,7 @@ import javax.swing.text.Document;
 import org.apache.maven.DefaultMaven;
 import org.apache.maven.Maven;
 import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.versioning.ComparableVersion;
 import org.apache.maven.building.Source;
 import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.model.building.ModelBuildingException;
@@ -60,6 +61,7 @@ import 
org.netbeans.modules.maven.indexer.api.RepositoryPreferences;
 import org.netbeans.modules.maven.model.pom.POMComponent;
 import org.netbeans.modules.maven.model.pom.POMModel;
 import org.netbeans.modules.maven.model.pom.Properties;
+import org.netbeans.modules.maven.options.MavenSettings;
 import org.netbeans.modules.xml.xam.Model;
 import org.netbeans.spi.editor.hints.ErrorDescription;
 import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
@@ -315,5 +317,20 @@ public final class PomModelUtils {
         }
         return null;
     }
+        
+    /*tests*/ static ComparableVersion activeMavenVersion = null;
+    private static File lastHome = null;
+    
+    static ComparableVersion getActiveMavenVersion() {
+        File home = EmbedderFactory.getMavenHome();
+        if (home != null && !home.equals(lastHome)) {
+            lastHome = home;
+            String version = MavenSettings.getCommandLineMavenVersion(home);
+            if (version != null) {
+                activeMavenVersion = new ComparableVersion(version);
+            }
+        }
+        return activeMavenVersion;
+    }
 
 }
diff --git 
a/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java
 
b/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java
index b51a3b9139..41464f8919 100644
--- 
a/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java
+++ 
b/java/maven.hints/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHint.java
@@ -28,6 +28,9 @@ import javax.xml.namespace.QName;
 import org.apache.maven.artifact.versioning.ComparableVersion;
 import org.netbeans.api.project.Project;
 import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.maven.api.Constants;
+import org.netbeans.modules.maven.api.NbMavenProject;
+import org.netbeans.modules.maven.api.PluginPropertyUtils;
 import org.netbeans.modules.maven.hints.pom.spi.Configuration;
 import org.netbeans.modules.maven.hints.pom.spi.POMErrorFixProvider;
 import org.netbeans.modules.maven.model.pom.Build;
@@ -62,7 +65,11 @@ public class UseReleaseOptionHint implements 
POMErrorFixProvider {
     private static final String SOURCE_TAG = "source";
     private static final String RELEASE_TAG = "release";
 
-    private static final ComparableVersion MIN_VERSION = new 
ComparableVersion("3.6");
+    // min compiler plugin version for release option support
+    private static final ComparableVersion COMPILER_PLUGIN_VERSION = new 
ComparableVersion("3.6.0");
+
+    // maven version which added the required compiler plugin implicitly
+    private static final ComparableVersion MAVEN_VERSION = new 
ComparableVersion("3.9.0");
 
     private static final Configuration config = new 
Configuration(UseReleaseOptionHint.class.getName(),
                 TIT_UseReleaseVersionHint(), DESC_UseReleaseVersionHint(), 
true, Configuration.HintSeverity.WARNING);
@@ -70,36 +77,57 @@ public class UseReleaseOptionHint implements 
POMErrorFixProvider {
     @Override
     public List<ErrorDescription> getErrorsForDocument(POMModel model, Project 
prj) {
 
+        if (prj == null) {
+            return Collections.emptyList();
+        }
+
+        // no hints if plugin was downgraded
+        NbMavenProject nbproject = 
prj.getLookup().lookup(NbMavenProject.class);
+        if (nbproject != null) {
+            // note: this is the embedded plugin version, only useful for 
downgrade checks
+            String pluginVersion = 
PluginPropertyUtils.getPluginVersion(nbproject.getMavenProject(), 
Constants.GROUP_APACHE_PLUGINS, Constants.PLUGIN_COMPILER);
+            if (new 
ComparableVersion(pluginVersion).compareTo(COMPILER_PLUGIN_VERSION) <= 0) {
+                return Collections.emptyList();
+            }
+        }
+
         Build build = model.getProject().getBuild();
 
-        if (build != null && build.getPlugins() != null) {
+        List<ErrorDescription> hints = new ArrayList<>();
 
-            List<ErrorDescription> hints = new ArrayList<>();
+        boolean releaseSupportedByDeclaredPlugin = false;
+
+        if (build != null && build.getPlugins() != null) {
             Optional<Plugin> compilerPlugin = build.getPlugins().stream()
                     .filter((p) -> 
"maven-compiler-plugin".equals(p.getArtifactId()))
                     .filter(this::isPluginCompatible)
                     .findFirst();
 
             if (compilerPlugin.isPresent()) {
+                releaseSupportedByDeclaredPlugin = true;
                 hints.addAll(createHintsForParent("", 
compilerPlugin.get().getConfiguration()));
                 if (compilerPlugin.get().getExecutions() != null) {
                     for (PluginExecution exec : 
compilerPlugin.get().getExecutions()) {
                         hints.addAll(createHintsForParent("", 
exec.getConfiguration()));
                     }
                 }
-            } else {
-                return Collections.emptyList();
             }
+        }
 
-            Properties properties = model.getProject().getProperties();
-            if (properties != null) {
-                hints.addAll(createHintsForParent("maven.compiler.", 
properties));
+        // no hints if required version not declared and also not provided by 
maven
+        if (!releaseSupportedByDeclaredPlugin) {
+            ComparableVersion mavenVersion = 
PomModelUtils.getActiveMavenVersion();
+            if (mavenVersion == null || mavenVersion.compareTo(MAVEN_VERSION) 
<= 0) {
+                return Collections.emptyList();
             }
+        }
 
-            return hints;
+        Properties properties = model.getProject().getProperties();
+        if (properties != null) {
+            hints.addAll(createHintsForParent("maven.compiler.", properties));
         }
 
-        return Collections.emptyList();
+        return hints;
     }
 
     private List<ErrorDescription> createHintsForParent(String prefix, 
POMComponent parent) {
@@ -164,7 +192,7 @@ public class UseReleaseOptionHint implements 
POMErrorFixProvider {
         if (version == null || version.isEmpty()) {
             return false;
         }
-        return new ComparableVersion(version).compareTo(MIN_VERSION) >= 0;
+        return new 
ComparableVersion(version).compareTo(COMPILER_PLUGIN_VERSION) >= 0;
     }
 
     private static class ConvertToReleaseOptionFix implements Fix {
diff --git 
a/java/maven.hints/test/unit/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHintTest.java
 
b/java/maven.hints/test/unit/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHintTest.java
index 40ac031648..08d875dc47 100644
--- 
a/java/maven.hints/test/unit/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHintTest.java
+++ 
b/java/maven.hints/test/unit/src/org/netbeans/modules/maven/hints/pom/UseReleaseOptionHintTest.java
@@ -19,6 +19,7 @@
 package org.netbeans.modules.maven.hints.pom;
 
 import java.util.List;
+import org.apache.maven.artifact.versioning.ComparableVersion;
 import org.netbeans.api.project.Project;
 import org.netbeans.api.project.ProjectManager;
 import org.netbeans.junit.NbTestCase;
@@ -38,6 +39,12 @@ import static junit.framework.TestCase.assertEquals;
  */
 public class UseReleaseOptionHintTest extends NbTestCase {
 
+    // contains JDK 9+ compatible compiler plugin (3.10.1)
+    private static final ComparableVersion JDK_9_PLUS_COMPATIBLE = new 
ComparableVersion("3.9.1");
+
+    // contains old default compiler plugin, not supporting the release javac 
option
+    private static final ComparableVersion JDK_8_COMPATIBLE = new 
ComparableVersion("3.8.0");
+
     private FileObject work;
 
     public UseReleaseOptionHintTest(String name) {
@@ -48,9 +55,10 @@ public class UseReleaseOptionHintTest extends NbTestCase {
     protected void setUp() throws Exception {
         clearWorkDir();
         work = FileUtil.toFileObject(getWorkDir());
+        PomModelUtils.activeMavenVersion = JDK_9_PLUS_COMPATIBLE;
     }
 
-    public void testNoPluginNegative() throws Exception {
+    public void testImplicitCompilerPlugin() throws Exception {
         FileObject pom = TestFileUtils.writeFile(work, "pom.xml",
             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
             "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"; 
xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd\";>\n" +
@@ -70,8 +78,13 @@ public class UseReleaseOptionHintTest extends NbTestCase {
         POMModel model = 
POMModelFactory.getDefault().getModel(Utilities.createModelSource(pom));
         Project project = 
ProjectManager.getDefault().findProject(pom.getParent());
 
+        PomModelUtils.activeMavenVersion = JDK_8_COMPATIBLE;
         List<ErrorDescription> hints = new 
UseReleaseOptionHint().getErrorsForDocument(model, project);
         assertEquals(0, hints.size());
+        
+        PomModelUtils.activeMavenVersion = JDK_9_PLUS_COMPATIBLE;
+        hints = new UseReleaseOptionHint().getErrorsForDocument(model, 
project);
+        assertEquals(2, hints.size());
     }
 
     private static final String COMPILER_POM =
@@ -128,7 +141,7 @@ public class UseReleaseOptionHintTest extends NbTestCase {
         assertEquals(6, hints.size());
     }
 
-    public void testOldCompilerPluginNegative() throws Exception {
+    public void testOldCompilerPlugin() throws Exception {
         FileObject pom = TestFileUtils.writeFile(work, "pom.xml", 
COMPILER_POM.replaceFirst("3.10.1", "3.5"));
 
         POMModel model = 
POMModelFactory.getDefault().getModel(Utilities.createModelSource(pom));
@@ -138,7 +151,7 @@ public class UseReleaseOptionHintTest extends NbTestCase {
         assertEquals(0, hints.size());
     }
 
-    public void testCompilerPluginButOldTargetNegative() throws Exception {
+    public void testCompilerPluginWithOldTarget() throws Exception {
         FileObject pom = TestFileUtils.writeFile(work, "pom.xml", 
COMPILER_POM.replace("11", "5").replace("17", "1.4"));
 
         POMModel model = 
POMModelFactory.getDefault().getModel(Utilities.createModelSource(pom));
diff --git a/java/maven/nbproject/project.xml b/java/maven/nbproject/project.xml
index c975a4473c..abbc01d500 100644
--- a/java/maven/nbproject/project.xml
+++ b/java/maven/nbproject/project.xml
@@ -664,6 +664,7 @@
                 <package>org.netbeans.modules.maven.api.output</package>
                 <package>org.netbeans.modules.maven.api.problem</package>
                 <package>org.netbeans.modules.maven.execute</package>
+                <package>org.netbeans.modules.maven.options</package>
                 <package>org.netbeans.modules.maven.execute.model</package>
                 
<package>org.netbeans.modules.maven.execute.model.io.jdom</package>
                 
<package>org.netbeans.modules.maven.execute.model.io.xpp3</package>
diff --git a/java/maven/src/org/netbeans/modules/maven/api/ModuleInfoUtils.java 
b/java/maven/src/org/netbeans/modules/maven/api/ModuleInfoUtils.java
index e8992ae633..155df8779d 100644
--- a/java/maven/src/org/netbeans/modules/maven/api/ModuleInfoUtils.java
+++ b/java/maven/src/org/netbeans/modules/maven/api/ModuleInfoUtils.java
@@ -56,7 +56,7 @@ public final class ModuleInfoUtils {
      * there is a main module-info.java in the given project (has to be >= 
3.6).
      * 
      * @param prj the project to be checked
-     * @return <code>true</code> if there is no mofule-info file or if the 
m-c-p version is new enough (>= 3.6). Otherwise <code>false</code>
+     * @return <code>true</code> if there is no module-info file or if the 
m-c-p version is new enough (>= 3.6). Otherwise <code>false</code>
      */
     public static boolean checkModuleInfoAndCompilerFit(Project prj) {
         NbMavenProject nbprj = prj.getLookup().lookup(NbMavenProject.class);
@@ -76,7 +76,7 @@ public final class ModuleInfoUtils {
         return new ComparableVersion(version).compareTo(new 
ComparableVersion(Constants.PLUGIN_COMPILER_VERSION_SUPPORTING_JDK9)) >= 0;
     }    
     
-    private static boolean hasModuleInfo(NbMavenProject nbprj) {
+    public static boolean hasModuleInfo(NbMavenProject nbprj) {
         MavenProject mavenProject = nbprj.getMavenProject();        
         return hasModuleInfoInSourceRoot(mavenProject.getCompileSourceRoots()) 
|| 
                
hasModuleInfoInSourceRoot(mavenProject.getTestCompileSourceRoots());


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to