From 6b86a362fda86431e667d7d15c8c095977d16a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Sat, 14 Dec 2024 23:45:11 +0100 Subject: [PATCH] Maven: Improve dependency resolution (for example for annotation processors like lombok) Resolution of processors also needs to take into account, that for example the version for an artifact could be provided by DependencyManagement. Closes: #8054 Closes: #8041 --- .../maven/api/PluginPropertyUtils.java | 27 ++++- .../maven/api/PluginPropertyUtilsTest.java | 105 ++++++++++++++++++ 2 files changed, 129 insertions(+), 3 deletions(-) diff --git a/java/maven/src/org/netbeans/modules/maven/api/PluginPropertyUtils.java b/java/maven/src/org/netbeans/modules/maven/api/PluginPropertyUtils.java index 9dd86aa3798d..86248895872a 100644 --- a/java/maven/src/org/netbeans/modules/maven/api/PluginPropertyUtils.java +++ b/java/maven/src/org/netbeans/modules/maven/api/PluginPropertyUtils.java @@ -26,6 +26,8 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.Properties; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -39,6 +41,7 @@ import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.InputLocation; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; @@ -613,8 +616,10 @@ static class DependencyListBuilder implements ConfigurationBuilder build(Xpp3Dom configRoot, ExpressionEvaluator eval) { item.setScope(s.getValue()); item.setLocation(PROP_SCOPE, (InputLocation)s.getInputLocation()); } + if (item.getVersion() == null && dependencyManagement != null && dependencyManagement.getDependencies() != null) { + dependencyManagement + .getDependencies() + .stream() + .filter(d -> { + return Objects.equals(item.getGroupId(), d.getGroupId()) + && Objects.equals(item.getArtifactId(), d.getArtifactId()) + && Objects.equals(item.getClassifier(), d.getClassifier()) + && Objects.equals(item.getType(), d.getType()); + }) + .findFirst() + .ifPresent(d -> { + item.setVersion(d.getVersion()); + item.setLocation(PROP_VERSION, d.getLocation(PROP_VERSION)); + }); + } coords.add(item); } return coords; @@ -793,7 +814,7 @@ public String getDefaultScope() { } MavenProject mavenProject = projectImpl.getOriginalMavenProject(); - DependencyListBuilder bld = new DependencyListBuilder(query.getPathProperty(), query.getArtifactType()); + DependencyListBuilder bld = new DependencyListBuilder(mavenProject.getDependencyManagement(), query.getPathProperty(), query.getArtifactType()); List coordinates = PluginPropertyUtils.getPluginPropertyBuildable(mavenProject, query.getPluginGroupId(), query.getPluginArtifactId(), query.getGoal(), bld); if (coordinates == null) { return null; @@ -833,7 +854,7 @@ public String getDefaultScope() { artifact = new DefaultArtifact( coord.getGroupId(), coord.getArtifactId(), - "", + (VersionRange) null, coord.getScope() == null ? query.getDefaultScope() : coord.getScope(), coord.getType(), coord.getClassifier(), diff --git a/java/maven/test/unit/src/org/netbeans/modules/maven/api/PluginPropertyUtilsTest.java b/java/maven/test/unit/src/org/netbeans/modules/maven/api/PluginPropertyUtilsTest.java index e35bfc12c11e..0b6cdff8ba10 100644 --- a/java/maven/test/unit/src/org/netbeans/modules/maven/api/PluginPropertyUtilsTest.java +++ b/java/maven/test/unit/src/org/netbeans/modules/maven/api/PluginPropertyUtilsTest.java @@ -20,12 +20,17 @@ package org.netbeans.modules.maven.api; import java.io.File; +import java.io.IOException; import java.io.StringReader; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.model.Dependency; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectManager; import org.netbeans.junit.NbTestCase; import org.openide.filesystems.FileObject; @@ -202,6 +207,7 @@ public void testDependencyListBuilder() throws Exception { // Matching filter for propertyItemName should yield correct result PluginPropertyUtils.DependencyListBuilder bld = new PluginPropertyUtils.DependencyListBuilder( + null, "annotationProcessorPaths", null ); @@ -266,10 +272,109 @@ public void testDependencyListBuilder() throws Exception { // Filter with null value for propertyItemName should yield full list Xpp3Dom configRoot2 = Xpp3DomBuilder.build(new StringReader(testPom2)).getChild("build").getChild("plugins").getChildren()[0].getChild("configuration"); PluginPropertyUtils.DependencyListBuilder bld2 = new PluginPropertyUtils.DependencyListBuilder( + null, "annotationProcessorPaths", null ); List dependencies3 = bld2.build(configRoot2, PluginPropertyUtils.DUMMY_EVALUATOR); assertEquals(2, dependencies3.size()); } + + public void testDependencyBuilderWithDependencyManagement() throws IOException { + TestFileUtils.writeFile(d, "pom.xml", + """ + + + 4.0.0 + let.me.reproduce + annotation-processor-netbeans-reproducer + 1.0-SNAPSHOT + jar + + + + org.projectlombok + lombok + 1.18.36 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + + org.projectlombok + lombok + jar + + + + + + + + """ + ); + Project project = ProjectManager.getDefault().findProject(d); + assert project != null; + PluginPropertyUtils.PluginConfigPathParams query = new PluginPropertyUtils.PluginConfigPathParams("org.apache.maven.plugins", "maven-compiler-plugin", "annotationProcessorPaths"); + query.setDefaultScope(Artifact.SCOPE_RUNTIME); + query.setGoal("runtime"); + List errorList = new ArrayList<>(); + List artifacts = PluginPropertyUtils.getPluginPathProperty(project, query, true, errorList); + assertNotNull(artifacts); + assert artifacts != null; + assertEquals(1, artifacts.size()); + assertEquals("org.projectlombok", artifacts.get(0).getGroupId()); + assertEquals("lombok", artifacts.get(0).getArtifactId()); + assertEquals("1.18.36", artifacts.get(0).getVersion()); + assertNull(artifacts.get(0).getClassifier()); + } + + public void testDependencyBuilderWithoutVersion() throws IOException { + TestFileUtils.writeFile(d, "pom.xml", + """ + + + 4.0.0 + let.me.reproduce + annotation-processor-netbeans-reproducer + 1.0-SNAPSHOT + jar + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + + org.projectlombok + lombok + + + + + + + + """ + ); + Project project = ProjectManager.getDefault().findProject(d); + assert project != null; + PluginPropertyUtils.PluginConfigPathParams query = new PluginPropertyUtils.PluginConfigPathParams("org.apache.maven.plugins", "maven-compiler-plugin", "annotationProcessorPaths"); + query.setDefaultScope(Artifact.SCOPE_RUNTIME); + query.setGoal("runtime"); + List errorList = new ArrayList<>(); + List artifacts = PluginPropertyUtils.getPluginPathProperty(project, query, true, errorList); + assertNotNull(artifacts); + assert artifacts != null; + assertEquals(0, artifacts.size()); + } }