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

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

commit b665ae9e8268c9e04b7a707c3bff4950b8569e1e
Author: Jeff Maxwell <jeff.maxw...@uptake.com>
AuthorDate: Wed Jan 6 14:38:42 2021 -0600

    [MNG-6727] Changed check to project.version and project.parent.version
    
    Closes #427
---
 .../project/DefaultMavenProjectBuilderTest.java    | 89 +++++++++++++++++++++-
 .../pom.xml                                        |  4 +-
 .../pom.xml                                        |  4 +-
 .../pom.xml                                        |  6 +-
 .../child/pom.xml                                  |  4 +-
 .../pom.xml                                        |  0
 .../child/pom.xml                                  |  4 +-
 .../pom.xml                                        |  0
 .../child/pom.xml                                  |  6 +-
 .../pom.xml                                        |  0
 .../maven/model/building/DefaultModelBuilder.java  | 26 +++++--
 11 files changed, 119 insertions(+), 24 deletions(-)

diff --git 
a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java
 
b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java
index 1bb66ab..41af2bd 100644
--- 
a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java
+++ 
b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java
@@ -284,11 +284,11 @@ public class DefaultMavenProjectBuilderTest
      *
      * @throws Exception
      */
-    public void testBuildParentVersionRangeLocallyWithChildVersionExpression() 
throws Exception
+    public void 
testBuildParentVersionRangeLocallyWithChildProjectVersionExpression() throws 
Exception
     {
         File f1 =
             getTestFile(
-                
"src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml"
 );
+                
"src/test/resources/projects/parent-version-range-local-child-project-version-expression/child/pom.xml"
 );
 
         try
         {
@@ -301,7 +301,47 @@ public class DefaultMavenProjectBuilderTest
             assertThat( e.getMessage(), containsString( "Version must be a 
constant" ) );
         }
     }
+    
+    /**
+     * Tests whether local version range parent references are build correctly.
+     *
+     * @throws Exception
+     */
+    public void 
testBuildParentVersionRangeLocallyWithChildProjectParentVersionExpression() 
throws Exception
+    {
+        File f1 =
+            getTestFile(
+                
"src/test/resources/projects/parent-version-range-local-child-project-parent-version-expression/child/pom.xml"
 );
 
+        try
+        {
+            getProject( f1 );
+            fail( "Expected 'ProjectBuildingException' not thrown." );
+        }
+        catch ( final ProjectBuildingException e )
+        {
+            assertNotNull( e.getMessage() );
+            assertThat( e.getMessage(), containsString( "Version must be a 
constant" ) );
+        }
+    }
+    
+    /**
+     * Tests whether local version range parent references are build correctly.
+     *
+     * @throws Exception
+     */
+    public void 
testBuildParentVersionRangeLocallyWithChildRevisionExpression() throws Exception
+    {
+        File f1 =
+            getTestFile(
+                
"src/test/resources/projects/parent-version-range-local-child-revision-expression/child/pom.xml"
 );
+
+        MavenProject mp =  this.getProjectFromRemoteRepository( f1 );
+        
+        assertEquals("1.0-SNAPSHOT", mp.getVersion());
+      
+    }
+    
     /**
      * Tests whether external version range parent references are build 
correctly.
      *
@@ -349,11 +389,11 @@ public class DefaultMavenProjectBuilderTest
      *
      * @throws Exception
      */
-    public void 
testBuildParentVersionRangeExternallyWithChildVersionExpression() throws 
Exception
+    public void 
testBuildParentVersionRangeExternallyWithChildProjectVersionExpression() throws 
Exception
     {
         File f1 =
             getTestFile(
-                
"src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml"
 );
+                
"src/test/resources/projects/parent-version-range-external-child-project-version-expression/pom.xml"
 );
 
         try
         {
@@ -366,5 +406,46 @@ public class DefaultMavenProjectBuilderTest
             assertThat( e.getMessage(), containsString( "Version must be a 
constant" ) );
         }
     }
+    
+    /**
+     * Tests whether external version range parent references are build 
correctly.
+     *
+     * @throws Exception
+     */
+    public void 
testBuildParentVersionRangeExternallyWithChildProjectParentVersionExpression() 
throws Exception
+    {
+        File f1 =
+            getTestFile(
+                
"src/test/resources/projects/parent-version-range-external-child-project-parent-version-expression/pom.xml"
 );
 
+        try
+        {
+            this.getProjectFromRemoteRepository( f1 );
+            fail( "Expected 'ProjectBuildingException' not thrown." );
+        }
+        catch ( final ProjectBuildingException e )
+        {
+            assertNotNull( e.getMessage() );
+            assertThat( e.getMessage(), containsString( "Version must be a 
constant" ) );
+        }
+    }
+    
+    /**
+     * Tests whether external version range parent references are build 
correctly.
+     *
+     * @throws Exception
+     */
+    public void 
testBuildParentVersionRangeExternallyWithChildRevisionExpression() throws 
Exception
+    {
+        File f1 =
+            getTestFile(
+                
"src/test/resources/projects/parent-version-range-external-child-revision-expression/pom.xml"
 );
+
+       
+        MavenProject mp =  this.getProjectFromRemoteRepository( f1 );
+          
+        assertEquals("1.0-SNAPSHOT", mp.getVersion());
+      
+       
+    }
 }
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-external-child-project-parent-version-expression/pom.xml
similarity index 67%
copy from 
maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
copy to 
maven-core/src/test/resources/projects/parent-version-range-external-child-project-parent-version-expression/pom.xml
index d07ad6e..a8cf950 100644
--- 
a/maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
+++ 
b/maven-core/src/test/resources/projects/parent-version-range-external-child-project-parent-version-expression/pom.xml
@@ -6,7 +6,7 @@
     <version>[1,1]</version>
   </parent>
   <artifactId>child</artifactId>
-  <!-- Must not use expressions from parent due to version range. -->
-  <version>${some.property}</version>
+  <!-- Must not use ${project.parent.version} due to version range. -->
+  <version>${project.parent.version}</version>
   <packaging>pom</packaging>
 </project>
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-external-child-project-version-expression/pom.xml
similarity index 69%
copy from 
maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
copy to 
maven-core/src/test/resources/projects/parent-version-range-external-child-project-version-expression/pom.xml
index d07ad6e..bd30a9a 100644
--- 
a/maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
+++ 
b/maven-core/src/test/resources/projects/parent-version-range-external-child-project-version-expression/pom.xml
@@ -6,7 +6,7 @@
     <version>[1,1]</version>
   </parent>
   <artifactId>child</artifactId>
-  <!-- Must not use expressions from parent due to version range. -->
-  <version>${some.property}</version>
+  <!-- Must not use ${project.version} due to version range. -->
+  <version>${project.version}</version>
   <packaging>pom</packaging>
 </project>
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-external-child-revision-expression/pom.xml
similarity index 69%
rename from 
maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
rename to 
maven-core/src/test/resources/projects/parent-version-range-external-child-revision-expression/pom.xml
index d07ad6e..aa724d8 100644
--- 
a/maven-core/src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml
+++ 
b/maven-core/src/test/resources/projects/parent-version-range-external-child-revision-expression/pom.xml
@@ -6,7 +6,9 @@
     <version>[1,1]</version>
   </parent>
   <artifactId>child</artifactId>
-  <!-- Must not use expressions from parent due to version range. -->
-  <version>${some.property}</version>
+  <version>${revision}</version>
   <packaging>pom</packaging>
+  <properties>
+    <revision>1.0-SNAPSHOT</revision>
+  </properties>
 </project>
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-project-parent-version-expression/child/pom.xml
similarity index 68%
copy from 
maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
copy to 
maven-core/src/test/resources/projects/parent-version-range-local-child-project-parent-version-expression/child/pom.xml
index 066a11e..ac42c93 100644
--- 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
+++ 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-project-parent-version-expression/child/pom.xml
@@ -6,7 +6,7 @@
     <version>[1,10]</version>
   </parent>
   <artifactId>child</artifactId>
-  <!-- Must not use expressions from parent due to version range. -->
-  <version>${some.property}</version>
+  <!-- Must not use ${project.parent.version} due to version range. -->
+  <version>${project.parent.version}</version>
   <packaging>pom</packaging>
 </project>
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-project-parent-version-expression/pom.xml
similarity index 100%
copy from 
maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/pom.xml
copy to 
maven-core/src/test/resources/projects/parent-version-range-local-child-project-parent-version-expression/pom.xml
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-project-version-expression/child/pom.xml
similarity index 70%
copy from 
maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
copy to 
maven-core/src/test/resources/projects/parent-version-range-local-child-project-version-expression/child/pom.xml
index 066a11e..1435070 100644
--- 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
+++ 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-project-version-expression/child/pom.xml
@@ -6,7 +6,7 @@
     <version>[1,10]</version>
   </parent>
   <artifactId>child</artifactId>
-  <!-- Must not use expressions from parent due to version range. -->
-  <version>${some.property}</version>
+  <!-- Must not use ${project.version} due to version range. -->
+  <version>${project.version}</version>
   <packaging>pom</packaging>
 </project>
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-project-version-expression/pom.xml
similarity index 100%
copy from 
maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/pom.xml
copy to 
maven-core/src/test/resources/projects/parent-version-range-local-child-project-version-expression/pom.xml
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-revision-expression/child/pom.xml
similarity index 70%
rename from 
maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
rename to 
maven-core/src/test/resources/projects/parent-version-range-local-child-revision-expression/child/pom.xml
index 066a11e..b29b5d2 100644
--- 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml
+++ 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-revision-expression/child/pom.xml
@@ -6,7 +6,9 @@
     <version>[1,10]</version>
   </parent>
   <artifactId>child</artifactId>
-  <!-- Must not use expressions from parent due to version range. -->
-  <version>${some.property}</version>
+  <version>${revision}</version>
   <packaging>pom</packaging>
+  <properties>
+    <revision>1.0-SNAPSHOT</revision>
+  </properties>
 </project>
diff --git 
a/maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/pom.xml
 
b/maven-core/src/test/resources/projects/parent-version-range-local-child-revision-expression/pom.xml
similarity index 100%
rename from 
maven-core/src/test/resources/projects/parent-version-range-local-child-version-expression/pom.xml
rename to 
maven-core/src/test/resources/projects/parent-version-range-local-child-revision-expression/pom.xml
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 7233cf5..47c500e 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
@@ -1191,7 +1191,9 @@ public class DefaultModelBuilder
                 }
 
                 // Validate versions aren't inherited when using parent ranges 
the same way as when read externally.
-                if ( childModel.getVersion() == null )
+                String rawChildModelVersion = childModel.getVersion();
+                
+                if ( rawChildModelVersion == null )
                 {
                     // Message below is checked for in the MNG-2199 core IT.
                     problems.add( new ModelProblemCollectorRequest( 
Severity.FATAL, Version.V31 )
@@ -1200,7 +1202,7 @@ public class DefaultModelBuilder
                 }
                 else
                 {
-                    if ( childModel.getVersion().contains( "${" ) )
+                    if ( rawChildVersionReferencesParent( rawChildModelVersion 
) )
                     {
                         // Message below is checked for in the MNG-2199 core 
IT.
                         problems.add( new ModelProblemCollectorRequest( 
Severity.FATAL, Version.V31 )
@@ -1231,6 +1233,12 @@ public class DefaultModelBuilder
         return new ModelData( candidateSource, candidateModel, groupId, 
artifactId, version );
     }
 
+    private boolean rawChildVersionReferencesParent( String 
rawChildModelVersion )
+    {
+        return rawChildModelVersion.equals( "${project.version}" ) 
+                || rawChildModelVersion.equals( "${project.parent.version}" );
+    }
+
     private ModelSource getParentPomFile( Model childModel, Source source )
     {
         if ( !( source instanceof ModelSource2 ) )
@@ -1327,7 +1335,9 @@ public class DefaultModelBuilder
 
         if ( !parent.getVersion().equals( version ) )
         {
-            if ( childModel.getVersion() == null )
+            String rawChildModelVersion = childModel.getVersion();
+            
+            if ( rawChildModelVersion == null )
             {
                 // Message below is checked for in the MNG-2199 core IT.
                 problems.add( new ModelProblemCollectorRequest( 
Severity.FATAL, Version.V31 )
@@ -1336,7 +1346,7 @@ public class DefaultModelBuilder
             }
             else
             {
-                if ( childModel.getVersion().contains( "${" ) )
+                if ( rawChildVersionReferencesParent( rawChildModelVersion )  )
                 {
                     // Message below is checked for in the MNG-2199 core IT.
                     problems.add( new ModelProblemCollectorRequest( 
Severity.FATAL, Version.V31 )
@@ -1801,11 +1811,11 @@ public class DefaultModelBuilder
                 = new ConcurrentHashMap<>( 64 );
 
         /**
-         * If an interface could be extracted, DefaultModelProblemCollector 
should be ModelProblemCollectorExt
+         * If an interface could be extracted, DefaultModelProblemCollector 
should be ModelProblemCollectorExt.
          *
-         * @param request
-         * @param collector
-         * @return
+         * @param request the request
+         * @param collector the collector
+         * @return the transformer context
          */
         @Override
         public TransformerContext initialize( ModelBuildingRequest request, 
ModelProblemCollector collector )

Reply via email to