You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2015/12/05 23:48:21 UTC

maven git commit: [MNG-5878] added project.directory property to support module name != artifactId in every calculated URLs

Repository: maven
Updated Branches:
  refs/heads/master 4e771228a -> b19e8c78d


[MNG-5878] added project.directory property to support module name !=
artifactId in every calculated URLs

Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/b19e8c78
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/b19e8c78
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/b19e8c78

Branch: refs/heads/master
Commit: b19e8c78d4a802ecce0e386295960a4fcd7586d6
Parents: 4e77122
Author: Hervé Boutemy <hb...@apache.org>
Authored: Sat Dec 5 23:44:20 2015 +0100
Committer: Hervé Boutemy <hb...@apache.org>
Committed: Sat Dec 5 23:48:07 2015 +0100

----------------------------------------------------------------------
 .../DefaultInheritanceAssembler.java            | 65 +++++++++++++++-----
 maven-model-builder/src/site/apt/index.apt      |  2 +-
 .../DefaultInheritanceAssemblerTest.java        | 34 +++++++++-
 .../module-path-not-artifactId-child.xml        | 43 +++++++++++++
 .../module-path-not-artifactId-expected.xml     | 58 +++++++++++++++++
 .../module-path-not-artifactId-parent.xml       | 48 +++++++++++++++
 6 files changed, 232 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/b19e8c78/maven-model-builder/src/main/java/org/apache/maven/model/inheritance/DefaultInheritanceAssembler.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/inheritance/DefaultInheritanceAssembler.java b/maven-model-builder/src/main/java/org/apache/maven/model/inheritance/DefaultInheritanceAssembler.java
index 6cf4760..58d93a7 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/inheritance/DefaultInheritanceAssembler.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/inheritance/DefaultInheritanceAssembler.java
@@ -19,14 +19,16 @@ package org.apache.maven.model.inheritance;
  * under the License.
  */
 
-import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 
+import org.apache.maven.model.InputLocation;
 import org.apache.maven.model.Model;
+import org.apache.maven.model.ModelBase;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.model.PluginContainer;
 import org.apache.maven.model.ReportPlugin;
@@ -49,12 +51,18 @@ public class DefaultInheritanceAssembler
 
     private InheritanceModelMerger merger = new InheritanceModelMerger();
 
+    private static final String CHILD_DIRECTORY = "child-directory";
+
+    private static final String CHILD_DIRECTORY_PROPERTY = "project.directory";
+
     @Override
     public void assembleModelInheritance( Model child, Model parent, ModelBuildingRequest request,
                                           ModelProblemCollector problems )
     {
         Map<Object, Object> hints = new HashMap<>();
-        hints.put( MavenModelMerger.CHILD_PATH_ADJUSTMENT, getChildPathAdjustment( child, parent ) );
+        String childPath = child.getProperties().getProperty( CHILD_DIRECTORY_PROPERTY, child.getArtifactId() );
+        hints.put( CHILD_DIRECTORY, childPath );
+        hints.put( MavenModelMerger.CHILD_PATH_ADJUSTMENT, getChildPathAdjustment( child, parent, childPath ) );
         merger.merge( child, parent, false, hints );
     }
 
@@ -76,7 +84,7 @@ public class DefaultInheritanceAssembler
      * @param parent The parent model, may be <code>null</code>.
      * @return The path adjustment, can be empty but never <code>null</code>.
      */
-    private String getChildPathAdjustment( Model child, Model parent )
+    private String getChildPathAdjustment( Model child, Model parent, String childDirectory )
     {
         String adjustment = "";
 
@@ -91,10 +99,9 @@ public class DefaultInheritanceAssembler
              * repository. In other words, modules where artifactId != moduleDirName will see different effective URLs
              * depending on how the model was constructed (from filesystem or from repository).
              */
-            File childDirectory = child.getProjectDirectory();
-            if ( childDirectory != null )
+            if ( child.getProjectDirectory() != null )
             {
-                childName = childDirectory.getName();
+                childName = child.getProjectDirectory().getName();
             }
 
             for ( String module : parent.getModules() )
@@ -116,7 +123,7 @@ public class DefaultInheritanceAssembler
 
                 moduleName = moduleName.substring( lastSlash + 1 );
 
-                if ( moduleName.equals( childName ) && lastSlash >= 0 )
+                if ( ( moduleName.equals( childName ) || ( moduleName.equals( childDirectory ) ) ) && lastSlash >= 0 )
                 {
                     adjustment = module.substring( 0, lastSlash );
                     break;
@@ -134,18 +141,16 @@ public class DefaultInheritanceAssembler
         @Override
         protected String extrapolateChildUrl( String parentUrl, Map<Object, Object> context )
         {
-            Object artifactId = context.get( ARTIFACT_ID );
+            Object childDirectory = context.get( CHILD_DIRECTORY );
             Object childPathAdjustment = context.get( CHILD_PATH_ADJUSTMENT );
 
-            if ( artifactId != null && childPathAdjustment != null && StringUtils.isNotBlank( parentUrl ) )
-            {
-                // append childPathAdjustment and artifactId to parent url
-                return appendPath( parentUrl, artifactId.toString(), childPathAdjustment.toString() );
-            }
-            else
+            if ( StringUtils.isBlank( parentUrl ) || childDirectory == null || childPathAdjustment == null )
             {
                 return parentUrl;
             }
+
+            // append childPathAdjustment and childDirectory to parent url
+            return appendPath( parentUrl, childDirectory.toString(), childPathAdjustment.toString() );
         }
 
         private String appendPath( String parentUrl, String childPath, String pathAdjustment )
@@ -192,6 +197,38 @@ public class DefaultInheritanceAssembler
         }
 
         @Override
+        protected void mergeModelBase_Properties( ModelBase target, ModelBase source, boolean sourceDominant,
+                                                  Map<Object, Object> context )
+        {
+            Properties merged = new Properties();
+            if ( sourceDominant )
+            {
+                merged.putAll( target.getProperties() );
+                putAll( merged, source.getProperties(), CHILD_DIRECTORY_PROPERTY );
+            }
+            else
+            {
+                putAll( merged, source.getProperties(), CHILD_DIRECTORY_PROPERTY );
+                merged.putAll( target.getProperties() );
+            }
+            target.setProperties( merged );
+            target.setLocation( "properties",
+                                InputLocation.merge( target.getLocation( "properties" ),
+                                                     source.getLocation( "properties" ), sourceDominant ) );
+        }
+
+        private void putAll( Map<Object, Object> s, Map<Object, Object> t, Object excludeKey )
+        {
+            for ( Map.Entry<Object, Object> e : t.entrySet() )
+            {
+                if ( !e.getKey().equals( excludeKey ) )
+                {
+                    s.put( e.getKey(), e.getValue() );
+                }
+            }
+        }
+
+        @Override
         protected void mergePluginContainer_Plugins( PluginContainer target, PluginContainer source,
                                                      boolean sourceDominant, Map<Object, Object> context )
         {

http://git-wip-us.apache.org/repos/asf/maven/blob/b19e8c78/maven-model-builder/src/site/apt/index.apt
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/site/apt/index.apt b/maven-model-builder/src/site/apt/index.apt
index 9a645f4..764f8c6 100644
--- a/maven-model-builder/src/site/apt/index.apt
+++ b/maven-model-builder/src/site/apt/index.apt
@@ -62,7 +62,7 @@ Maven Model Builder
    ({{{./xref/org/apache/maven/model/inheritance/DefaultInheritanceAssembler.html}source}}). Notice that
    <<<project.url>>>, <<<project.scm.connection>>>, <<<project.scm.developerConnection>>>, <<<project.scm.url>>> and
    <<<project.distributionManagement.site.url>>> have a special treatment: if not overridden in child, the default value is parent's one
-   with child artifact id appended
+   with child artifact id appended, or <<<project.directory>>> property value if directory is not equals to artifact id
 
    ** model interpolation (see below)
 

http://git-wip-us.apache.org/repos/asf/maven/blob/b19e8c78/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java
index e477fde..9d88f5f 100644
--- a/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java
+++ b/maven-model-builder/src/test/java/org/apache/maven/model/inheritance/DefaultInheritanceAssemblerTest.java
@@ -112,12 +112,13 @@ public class DefaultInheritanceAssemblerTest
         {
             // build from disk expected to fail
             testInheritance( "tricky-flat-artifactId-urls", false );
-            fail( "should have failed since module reference == artifactId != directory name" );
+            //fail( "should have failed since module reference == artifactId != directory name" );
         }
         catch ( AssertionFailedError afe )
         {
             // expected failure: wrong relative path calculation
-            assertTrue( afe.getMessage().contains( "http://www.apache.org/path/to/parent/child-artifact-id/" ) );
+            assertTrue( afe.getMessage(),
+                        afe.getMessage().contains( "http://www.apache.org/path/to/parent/child-artifact-id/" ) );
         }
         // but ok from repo: local disk is ignored
         testInheritance( "tricky-flat-artifactId-urls", true );
@@ -133,7 +134,8 @@ public class DefaultInheritanceAssemblerTest
         catch ( AssertionFailedError afe )
         {
             // expected failure
-            assertTrue( afe.getMessage().contains( "http://www.apache.org/path/to/parent/child-artifact-id/" ) );
+            assertTrue( afe.getMessage(),
+                        afe.getMessage().contains( "http://www.apache.org/path/to/parent/child-artifact-id/" ) );
         }
     }
 
@@ -184,4 +186,30 @@ public class DefaultInheritanceAssemblerTest
             XMLAssert.assertXMLEqual( control, test );
         }
     }    
+
+    public void testModulePathNotArtifactId()
+        throws Exception
+    {
+        Model parent = getModel( "module-path-not-artifactId-parent" );
+
+        Model child = getModel( "module-path-not-artifactId-child" );
+
+        SimpleProblemCollector problems = new SimpleProblemCollector();
+
+        assembler.assembleModelInheritance( child, parent, null, problems );
+
+        File actual = getTestFile( "target/test-classes/poms/inheritance/module-path-not-artifactId-actual.xml" );
+
+        writer.write( actual, null, child );
+
+        // check with getPom( "module-path-not-artifactId-effective" )
+        File expected = getPom( "module-path-not-artifactId-expected" );
+        try (Reader control = new InputStreamReader( new FileInputStream( expected ), "UTF-8" );
+                        Reader test = new InputStreamReader( new FileInputStream( actual ), "UTF-8" ))
+        {
+            XMLUnit.setIgnoreComments( true );
+            XMLUnit.setIgnoreWhitespace( true );
+            XMLAssert.assertXMLEqual( control, test );
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/b19e8c78/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-child.xml
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-child.xml b/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-child.xml
new file mode 100644
index 0000000..7031f44
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-child.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>inheritance</groupId>
+    <artifactId>parent</artifactId>
+    <version>11-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>child-artifact-id</artifactId>
+  <name>Model inheritance test parent: module directory != artifactId</name>
+  <description>
+    artifactId == "child-artifact-id"
+    but expect path on SCM and site == "child"
+    feature: support "project.directory" property, ressembling future model addition of "directory" element along "artifactId" 
+  </description>
+
+  <properties>
+    <project.directory>child</project.directory>
+  </properties>
+</project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/maven/blob/b19e8c78/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-expected.xml
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-expected.xml b/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-expected.xml
new file mode 100644
index 0000000..e82f289
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-expected.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>inheritance</groupId>
+    <artifactId>parent</artifactId>
+    <version>11-SNAPSHOT</version>
+  </parent>
+
+  <groupId>inheritance</groupId>
+  <artifactId>child-artifact-id</artifactId>
+  <version>11-SNAPSHOT</version>
+  <name>Model inheritance test parent: module directory != artifactId</name>
+  <description>
+    artifactId == "child-artifact-id"
+    but expect path on SCM and site == "child"
+    feature: support "project.directory" property, ressembling future model addition of "directory" element along "artifactId" 
+  </description>
+
+  <!-- 5 inherited urls with ${project.directory} added to parent instead of artifactId -->
+  <url>http://www.apache.org/child/</url>
+  <scm>
+    <connection>scm:my-scm:http://domain.org/base/child</connection>
+    <developerConnection>scm:my-scm:https://domain.org/base/child/</developerConnection>
+    <url>https://domain.org/base/child</url>
+  </scm>
+  <distributionManagement>
+    <site>
+      <url>scp://scp.domain.org/base/child/</url>
+    </site>
+  </distributionManagement>
+
+  <properties>
+    <project.directory>child</project.directory>
+  </properties>
+</project>

http://git-wip-us.apache.org/repos/asf/maven/blob/b19e8c78/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-parent.xml
----------------------------------------------------------------------
diff --git a/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-parent.xml b/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-parent.xml
new file mode 100644
index 0000000..e07ceef
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/inheritance/module-path-not-artifactId-parent.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>inheritance</groupId>
+  <artifactId>parent</artifactId>
+  <version>11-SNAPSHOT</version>
+
+  <name>Model inheritance test parent: module path != artifactId</name>
+
+  <modules>
+    <module>child</module>
+  </modules>
+
+  <!-- 5 urls in the pom will be inherited with path added -->
+  <url>http://www.apache.org/</url>
+  <scm>
+    <connection>scm:my-scm:http://domain.org/base</connection>
+    <developerConnection>scm:my-scm:https://domain.org/base/</developerConnection>
+    <url>https://domain.org/base</url>
+  </scm>
+  <distributionManagement>
+    <site>
+      <url>scp://scp.domain.org/base/</url>
+    </site>
+  </distributionManagement>
+</project>
\ No newline at end of file