You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by bi...@apache.org on 2012/05/22 00:52:45 UTC

svn commit: r1341247 - in /maven/plugins/trunk/maven-shade-plugin: ./ rel-path-test-files/ rel-path-test-files/a/ rel-path-test-files/a/b/ rel-path-test-files/a/b/c/ rel-path-test-files/a/b/c/d/ rel-path-test-files/a/b/c/d1/ rel-path-test-files/a/b/c1/...

Author: bimargulies
Date: Mon May 21 22:52:44 2012
New Revision: 1341247

URL: http://svn.apache.org/viewvc?rev=1341247&view=rev
Log:
MSHADE-115: Temporary dependency-reduced-pom.xml in basedir causes problems within shared build trees
o the fix here is to generate the drp in the final location, but to patch the parent relative path to 
compensate for the difference between that location and the basedir.

Added:
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d/pom
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d1/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d1/pom
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/pom
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c1/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c1/d/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c1/pom
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/pom
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b1/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b1/c/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b1/c/d/
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b1/pom
    maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/pom
    maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-relocated-result/
      - copied from r1340162, maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-with-local-parent/
    maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java   (with props)
    maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java   (with props)
Modified:
    maven/plugins/trunk/maven-shade-plugin/pom.xml
    maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-relocated-result/child/pom.xml
    maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java

Modified: maven/plugins/trunk/maven-shade-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/pom.xml?rev=1341247&r1=1341246&r2=1341247&view=diff
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-shade-plugin/pom.xml Mon May 21 22:52:44 2012
@@ -128,7 +128,7 @@ under the License.
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
-      <version>3.8.2</version>
+      <version>4.10</version>
       <scope>test</scope>
     </dependency>
 

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d1/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/d1/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c1/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/c1/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b1/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/b1/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Added: maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/pom
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/rel-path-test-files/a/pom?rev=1341247&view=auto
==============================================================================
    (empty)

Modified: maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-relocated-result/child/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-relocated-result/child/pom.xml?rev=1341247&r1=1340162&r2=1341247&view=diff
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-relocated-result/child/pom.xml (original)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/dep-reduced-pom-relocated-result/child/pom.xml Mon May 21 22:52:44 2012
@@ -63,6 +63,7 @@ under the License.
             <configuration>
               <shadedArtifactAttached>false</shadedArtifactAttached>
               <createDependencyReducedPom>true</createDependencyReducedPom>
+              <dependencyReducedPomLocation>target/drp.xml</dependencyReducedPomLocation>
             </configuration>
           </execution>
         </executions>

Added: maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java?rev=1341247&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java (added)
+++ maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java Mon May 21 22:52:44 2012
@@ -0,0 +1,106 @@
+/*
+ * 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.
+ */
+
+package org.apache.maven.plugins.shade.mojo;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ *
+ */
+public final class RelativizePath
+{
+    private RelativizePath() {
+        //
+    }
+
+    /**
+     * relativize a pathname. 
+     * @param thing Absolute File of something. (e.g., a parent pom)
+     * @param relativeTo base to relativize it do. (e.g., a pom into which a relative pathname to the 'thing' is to be installed).
+     * @return
+     */
+    static String convertToRelativePath(File thing, File relativeTo) {
+        StringBuilder relativePath = null;
+        
+        if ( thing.getParentFile().equals( relativeTo.getParentFile() ))
+        {
+            return thing.getName(); // a very simple relative path.
+        }
+        
+        List<String> thingDirectories = RelativizePath.parentDirs( thing );
+        List<String> relativeToDirectories = RelativizePath.parentDirs( relativeTo );
+    
+        //Get the shortest of the two paths
+        int length = thingDirectories.size() < relativeToDirectories.size() ? thingDirectories.size() : relativeToDirectories.size();
+    
+        int lastCommonRoot = -1; // index of the lowest directory down from the root that the two have in common.
+        int index;
+    
+        //Find common root
+        for ( index = 0; index < length; index++ ) 
+        {
+            if ( thingDirectories.get( index ).equals(relativeToDirectories.get( index )))
+            {
+                lastCommonRoot = index;
+            } else {
+                break;
+            }
+        }
+        if (lastCommonRoot != -1) { // possible on Windows or other multi-root cases.
+            //Build up the relative path
+            relativePath = new StringBuilder();
+            // add ..'s to get from the base up to the common point
+            for ( index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++ ) 
+            {
+                relativePath.append( "../" );
+            }
+            
+            // now add down from the common point to the actual 'thing' item. 
+            for ( index = lastCommonRoot + 1; index < thingDirectories.size(); index++ ) 
+            {
+                relativePath.append( thingDirectories.get( index ) + "/" );
+            }
+            relativePath.append( thing.getName() );
+            return relativePath.toString();
+        }
+        return null;
+    }
+
+    static List<String> parentDirs( File of )
+    {
+        List<String> results = new ArrayList<String>();
+        for ( File p = of.getParentFile() ; p != null ; p = p.getParentFile() )
+        {
+            if ( !"".equals(p.getName()) )
+            {
+                results.add( p.getName() );
+            }
+        }
+        
+        Collections.reverse( results );
+        return results;
+    }
+    
+    
+
+}

Propchange: maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/RelativizePath.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java?rev=1341247&r1=1341246&r2=1341247&view=diff
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java (original)
+++ maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java Mon May 21 22:52:44 2012
@@ -62,6 +62,7 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.Writer;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -308,11 +309,18 @@ public class ShadeMojo
      * Flag whether to generate a simplified POM for the shaded artifact. If set to <code>true</code>, dependencies that
      * have been included into the uber JAR will be removed from the <code>&lt;dependencies&gt;</code> section of the
      * generated POM. The reduced POM will be named <code>dependency-reduced-pom.xml</code> and is stored into the same
-     * directory as the shaded artifact.
+     * directory as the shaded artifact. Unless you also specify dependencyReducedPomLocation, the plugin will
+     * create a temporary file named <code>dependency-reduced-pom.xml</code> in the project basedir. 
      *
      * @parameter expression="${createDependencyReducedPom}" default-value="true"
      */
     private boolean createDependencyReducedPom;
+    
+    
+    /**
+     *  @parameter expression="${dependencyReducedPomLocation}" defaultValue="${basedir}/dependency-reduced-pom.xml"
+     */
+    private File dependencyReducedPomLocation;
 
     /**
      * When true, dependencies are kept in the pom but with scope 'provided'; when false,
@@ -862,11 +870,12 @@ public class ShadeMojo
 
                 model.setDependencies( dependencies );
 
-                /*
-                 * NOTE: Be sure to create the POM in the original base directory to be able to resolve the relativePath
-                 * to local parent POMs when invoking the project builder below.
-                 */
-                File f = new File( project.getBasedir(), "dependency-reduced-pom.xml" );
+                if ( dependencyReducedPomLocation == null )
+                {
+                    dependencyReducedPomLocation = new File ( outputDirectory, "dependency-reduced-pom.xml" );
+                }
+                
+                File f = dependencyReducedPomLocation;
                 if ( f.exists() )
                 {
                     f.delete();
@@ -874,12 +883,44 @@ public class ShadeMojo
 
                 Writer w = WriterFactory.newXmlWriter( f );
 
+                String origRelativePath = null;
+                String replaceRelativePath = null;
+                if ( model.getParent() != null)
+                {
+                    origRelativePath = model.getParent().getRelativePath();
+                    
+                }
+                replaceRelativePath = origRelativePath;
+
+                if ( origRelativePath == null )
+                {
+                    origRelativePath = "../pom.xml";
+                }
+
+                if ( model.getParent() != null ) 
+                {
+                    File parentFile = new File( project.getBasedir(), model.getParent().getRelativePath() ).getCanonicalFile();
+                    if ( !parentFile.isFile() )
+                    {
+                        parentFile = new File( parentFile, "pom.xml");
+                    }
+                    
+                    parentFile = parentFile.getCanonicalFile();
+                    
+                    String relPath = RelativizePath.convertToRelativePath( parentFile, f );
+                    model.getParent().setRelativePath( relPath );
+                }
+                
                 try
                 {
                     PomWriter.write( w, model, true );
                 }
                 finally
                 {
+                    if ( model.getParent() != null )
+                    {
+                        model.getParent().setRelativePath( replaceRelativePath );
+                    }
                     w.close();
                 }
 
@@ -888,20 +929,10 @@ public class ShadeMojo
 
             }
 
-            /*
-             * NOTE: Although the dependency reduced POM in the project directory is temporary build output, we have to
-             * use that for the file of the project instead of something in target to avoid messing up the base
-             * directory of the project. We'll delete this file on exit to make sure it gets cleaned up but keep a copy
-             * for inspection in the target directory as well.
-             */
-            File f = new File( project.getBasedir(), "dependency-reduced-pom.xml" );
-            File f2 = new File( outputDirectory, "dependency-reduced-pom.xml" );
-            FileUtils.copyFile( f, f2 );
-            FileUtils.forceDeleteOnExit( f );
-            project.setFile( f );
+            project.setFile( dependencyReducedPomLocation );
         }
     }
-
+    
     private String getId( Artifact artifact )
     {
         return getId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(), artifact.getClassifier() );

Added: maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java?rev=1341247&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java (added)
+++ maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java Mon May 21 22:52:44 2012
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+package org.apache.maven.plugins.shade.mojo;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class RelativizePathTest
+{
+    final static String[] PARENTS = { "rel-path-test-files/a/pom", "rel-path-test-files/a/b/pom",
+        "rel-path-test-files/a/b/c/pom", "rel-path-test-files/a/c/d/pom" };
+
+    final static String[] CHILDREN = { "rel-path-test-files/a/b/pom", "rel-path-test-files/a/pom",
+        "rel-path-test-files/a/b/c1/pom", "rel-path-test-files/a/c/d/pom" };
+
+    final static String[] ANSWER = { "../pom", "b/pom", "../c/pom", "pom" };
+
+    @Test
+    public void runTests() throws IOException
+    {
+        for ( int x = 0; x < PARENTS.length; x++ )
+        {
+            File parent = new File(PARENTS[x]).getCanonicalFile();
+            File child = new File(CHILDREN[x]).getCanonicalFile();
+            String answer = ANSWER[x];
+            String r = RelativizePath.convertToRelativePath( parent, child );
+            assertEquals(String.format("parent %s child %s", parent.toString(), child.toString()), answer, r );
+        }
+    }
+
+}

Propchange: maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-shade-plugin/src/test/java/org/apache/maven/plugins/shade/mojo/RelativizePathTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain