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..6af80cecf958 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.setGroupId(g.getValue()); item.setLocation(PROP_GROUP_ID, (InputLocation)g.getInputLocation()); } + + Dependency dependencyFromDependencyManagement = null; + if(dependencyManagement != null && dependencyManagement.getDependencies() != null) { + dependencyFromDependencyManagement = + dependencyManagement + .getDependencies() + .stream() + .filter(d -> { + return Objects.equals(item.getGroupId(), d.getGroupId()) + && Objects.equals(item.getArtifactId(), d.getArtifactId()); + }) + .findFirst() + .orElse(null); + } + if (v != null) { item.setVersion(v.getValue()); item.setLocation(PROP_VERSION, (InputLocation)v.getInputLocation()); + } else if (dependencyFromDependencyManagement != null) { + item.setVersion(dependencyFromDependencyManagement.getVersion()); + item.setLocation(PROP_VERSION, dependencyFromDependencyManagement.getLocation(PROP_VERSION)); } if (c != null) { item.setClassifier(c.getValue()); item.setLocation(PROP_CLASSIFIER, (InputLocation)c.getInputLocation()); + } else if (dependencyFromDependencyManagement != null) { + item.setClassifier(dependencyFromDependencyManagement.getClassifier()); + item.setLocation(PROP_CLASSIFIER, dependencyFromDependencyManagement.getLocation(PROP_CLASSIFIER)); } if (t != null) { item.setType(t.getValue()); item.setLocation(PROP_TYPE, (InputLocation)t.getInputLocation()); + } else if (dependencyFromDependencyManagement != null) { + item.setType(dependencyFromDependencyManagement.getType()); + item.setLocation(PROP_TYPE, dependencyFromDependencyManagement.getLocation(PROP_TYPE)); } if (s != null) { item.setScope(s.getValue()); item.setLocation(PROP_SCOPE, (InputLocation)s.getInputLocation()); + } else if (dependencyFromDependencyManagement != null) { + item.setScope(dependencyFromDependencyManagement.getType()); + item.setLocation(PROP_TYPE, dependencyFromDependencyManagement.getLocation(PROP_TYPE)); } coords.add(item); } @@ -793,7 +825,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 +865,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..bf1cfd328ca9 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,18 @@ 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.apache.maven.project.MavenProject; 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 +208,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 +273,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 + dummy + + + + + + + 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(1, artifacts.size()); + assertEquals("org.projectlombok", artifacts.get(0).getGroupId()); + assertEquals("lombok", artifacts.get(0).getArtifactId()); + assertEquals("1.18.36", artifacts.get(0).getVersion()); + assertEquals("dummy", 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()); + } }