[maven] branch MNG-6957 updated: Improve parentFilter, change requirement getRawModel by Path

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven] branch MNG-6957 updated: Improve parentFilter, change requirement getRawModel by Path

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

rfscholte pushed a commit to branch MNG-6957
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/MNG-6957 by this push:
     new 11153cf  Improve parentFilter, change requirement getRawModel by Path
11153cf is described below

commit 11153cfcdbcec79f9a509b869ee3b901d2bd65a1
Author: rfscholte <[hidden email]>
AuthorDate: Fri Oct 16 18:27:27 2020 +0200

    Improve parentFilter, change requirement getRawModel by Path
---
 .../maven/model/building/DefaultModelBuilder.java  | 13 ++++-------
 .../maven/model/building/TransformerContext.java   |  4 ++--
 .../maven/xml/sax/filter/ParentXMLFilter.java      | 21 ++++++++++++++---
 .../maven/xml/sax/filter/ParentXMLFilterTest.java  | 27 ++++++++++++++++++++--
 4 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
index 5d56617..f5e7aec 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
@@ -1873,19 +1873,14 @@ public class DefaultModelBuilder
                 
                 private Model findRawModel( Path p )
                 {
-                    File pomFile;
-                    if ( Files.isDirectory( p ) )
+                    if ( !Files.isRegularFile( p ) )
                     {
-                        pomFile = modelLocator.locatePom( p.toFile() );
-                    }
-                    else
-                    {
-                        pomFile = p.toFile();
+                        throw new IllegalArgumentException( "Not a regular file" + p );
                     }
                     
                     DefaultModelBuildingRequest req = new DefaultModelBuildingRequest( request )
-                                    .setPomFile( pomFile )
-                                    .setModelSource( new FileModelSource( pomFile ) );
+                                    .setPomFile( p.toFile() )
+                                    .setModelSource( new FileModelSource( p.toFile() ) );
                     
                     try
                     {
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/TransformerContext.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/TransformerContext.java
index af1fe8c..62e6e80 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/building/TransformerContext.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/TransformerContext.java
@@ -47,10 +47,10 @@ public interface TransformerContext
     /**
      * Get the model based on the path, will be used to resolve the parent based on relativePath
      *
-     * @param p the path
+     * @param pomFile the path to the pomFile
      * @return the model, otherwise {@code null}
      */
-    Model getRawModel( Path p );
+    Model getRawModel( Path pomFile );
     
     /**
      * Get the model from the reactor based on the groupId and artifactId, will be used for reactor dependencies
diff --git a/maven-xml/src/main/java/org/apache/maven/xml/sax/filter/ParentXMLFilter.java b/maven-xml/src/main/java/org/apache/maven/xml/sax/filter/ParentXMLFilter.java
index 8296e74..38112b8 100644
--- a/maven-xml/src/main/java/org/apache/maven/xml/sax/filter/ParentXMLFilter.java
+++ b/maven-xml/src/main/java/org/apache/maven/xml/sax/filter/ParentXMLFilter.java
@@ -19,6 +19,7 @@ package org.apache.maven.xml.sax.filter;
  * under the License.
  */
 
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
@@ -60,6 +61,8 @@ class ParentXMLFilter
 
     private boolean hasVersion;
     
+    private boolean hasRelativePath;
+    
     private Optional<RelativeProject> resolvedParent;
 
     private final Function<Path, Optional<RelativeProject>> relativePathMapper;
@@ -107,6 +110,9 @@ class ParentXMLFilter
             state = localName;
             
             hasVersion |= "version".equals( localName );
+            
+            // can be set to empty on purpose to enforce repository download
+            hasRelativePath |= "relativePath".equals( localName );
         }
         
         super.startElement( uri, localName, qName, atts );
@@ -153,11 +159,15 @@ class ParentXMLFilter
             switch ( localName )
             {
                 case "parent":
-                    if ( !hasVersion || relativePath != null )
+                    if ( !hasVersion && ( !hasRelativePath || relativePath != null ) )
                     {
                         resolvedParent =
                             resolveRelativePath( Paths.get( Objects.toString( relativePath, "../pom.xml" ) ) );
                     }
+                    else
+                    {
+                        resolvedParent = Optional.empty();
+                    }
                     
                     if ( !hasVersion && resolvedParent.isPresent() )
                     {
@@ -194,8 +204,13 @@ class ParentXMLFilter
 
     protected Optional<RelativeProject> resolveRelativePath( Path relativePath )
     {
-        Optional<RelativeProject> mappedProject =
-            relativePathMapper.apply( projectPath.resolve( relativePath ).normalize() );
+        Path pomPath = projectPath.resolve( relativePath );
+        if ( Files.isDirectory( pomPath ) )
+        {
+            pomPath = pomPath.resolve( "pom.xml" );
+        }
+        
+        Optional<RelativeProject> mappedProject = relativePathMapper.apply( pomPath.normalize() );
         
         if ( mappedProject.isPresent() )
         {
diff --git a/maven-xml/src/test/java/org/apache/maven/xml/sax/filter/ParentXMLFilterTest.java b/maven-xml/src/test/java/org/apache/maven/xml/sax/filter/ParentXMLFilterTest.java
index bb00222..17cdd57 100644
--- a/maven-xml/src/test/java/org/apache/maven/xml/sax/filter/ParentXMLFilterTest.java
+++ b/maven-xml/src/test/java/org/apache/maven/xml/sax/filter/ParentXMLFilterTest.java
@@ -27,8 +27,6 @@ import java.util.Optional;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
 
-import org.apache.maven.xml.sax.filter.ParentXMLFilter;
-import org.apache.maven.xml.sax.filter.RelativeProject;
 import org.junit.Test;
 import org.xml.sax.SAXException;
 
@@ -88,6 +86,31 @@ public class ParentXMLFilterTest extends AbstractXMLFilterTests
         assertEquals( expected, actual );
     }
 
+    /**
+     * An empty relative path means it must downloaded from a repository.
+     * That implies that the version cannot be solved (if missing, Maven should complain)
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testEmptyRelativePathNoVersion() throws Exception
+    {
+        String input = "<parent>"
+            + "<groupId>GROUPID</groupId>"
+            + "<artifactId>ARTIFACTID</artifactId>"
+            + "<relativePath></relativePath>"
+            + "</parent>";
+        String expected = "<parent>"
+                        + "<groupId>GROUPID</groupId>"
+                        + "<artifactId>ARTIFACTID</artifactId>"
+                        + "<relativePath/>" // SAX optimization, however "" != null ...
+                        + "</parent>";;
+
+        String actual = transform( input );
+
+        assertEquals( expected, actual );
+    }
+
     @Test
     public void testNoVersion() throws Exception
     {