You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gb...@apache.org on 2018/02/14 21:41:20 UTC

[maven-shade-plugin] 01/01: [MSHADE-273] Shading of paths starting with / ignores excludes

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

gboue pushed a commit to branch MSHADE-273
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git

commit 36bb8762400ce3fe76048c677d7e9422d60a6977
Author: Guillaume Boué <gb...@apache.org>
AuthorDate: Wed Feb 14 22:10:22 2018 +0100

    [MSHADE-273] Shading of paths starting with / ignores excludes
    
    Exclude and include patterns need to be taken into account when handling
    a potential extra leading slash. This works by simply removing it when
    there's one in the considered path, before matching against
    include/excludes/path pattern.
---
 src/it/reloc-abs-resource-path-exclude/pom.xml     | 94 ++++++++++++++++++++++
 .../src/main/java/Main.java                        | 42 ++++++++++
 .../org/apache/maven/from/a/test.properties        | 18 +++++
 .../org/apache/maven/from/b/test.properties        | 18 +++++
 .../plugins/shade/relocation/SimpleRelocator.java  | 10 +--
 .../shade/relocation/SimpleRelocatorTest.java      | 23 +++++-
 6 files changed, 198 insertions(+), 7 deletions(-)

diff --git a/src/it/reloc-abs-resource-path-exclude/pom.xml b/src/it/reloc-abs-resource-path-exclude/pom.xml
new file mode 100644
index 0000000..ce5941a
--- /dev/null
+++ b/src/it/reloc-abs-resource-path-exclude/pom.xml
@@ -0,0 +1,94 @@
+<?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/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.plugins.shade.its</groupId>
+    <artifactId>shade-parent</artifactId>
+    <version>1.0</version>
+    <relativePath>../setup-parent</relativePath>
+  </parent>
+
+  <groupId>org.apache.maven.its.shade</groupId>
+  <artifactId>reloc-abs-resource-path-exclude</artifactId>
+  <version>1.0</version>
+
+  <name>MSHADE-119</name>
+  <description>
+    Test that classes in strings like "/org/apache/velocity/my.properties" get properly relocated
+    in calls to Class.getResource(), taking into account excluded patterns.
+  </description>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>shade</id>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <createDependencyReducedPom>false</createDependencyReducedPom>
+              <relocations>
+                <relocation>
+                  <pattern>org.apache.maven.from</pattern>
+                  <shadedPattern>org.apache.maven.to</shadedPattern>
+                  <excludes>
+                    <exclude>org.apache.maven.from.a.*</exclude>
+                  </excludes>
+                </relocation>
+              </relocations>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>exec-maven-plugin</artifactId>
+        <version>1.1.1</version>
+        <executions>
+          <execution>
+            <id>run</id>
+            <phase>verify</phase>
+            <goals>
+              <goal>exec</goal>
+            </goals>
+            <configuration>
+              <executable>${JAVA_HOME}/bin/java</executable>
+              <arguments>
+                <argument>-classpath</argument>
+                <argument>${project.build.directory}/reloc-abs-resource-path-exclude-1.0.jar</argument>
+                <argument>Main</argument>
+              </arguments>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/reloc-abs-resource-path-exclude/src/main/java/Main.java b/src/it/reloc-abs-resource-path-exclude/src/main/java/Main.java
new file mode 100644
index 0000000..679f70e
--- /dev/null
+++ b/src/it/reloc-abs-resource-path-exclude/src/main/java/Main.java
@@ -0,0 +1,42 @@
+import java.net.URL;
+
+/*
+ * 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.
+ */
+
+public class Main
+{
+
+    public static void main( String[] args )
+        throws Exception
+    {
+        new Main().testAbsResource();
+    }
+
+    private void testAbsResource() throws Exception
+    {
+        URL r = getClass().getResource("/org/apache/maven/from/a/test.properties");
+        // expect NPE if from/a was shaded even though it was excluded
+        System.out.println( r.toString() );
+
+        r = getClass().getResource("/org/apache/maven/to/b/test.properties");
+        // expect NPE if from/b was not shaded
+        System.out.println( r.toString() );
+    }
+
+}
diff --git a/src/it/reloc-abs-resource-path-exclude/src/main/resources/org/apache/maven/from/a/test.properties b/src/it/reloc-abs-resource-path-exclude/src/main/resources/org/apache/maven/from/a/test.properties
new file mode 100644
index 0000000..75c155e
--- /dev/null
+++ b/src/it/reloc-abs-resource-path-exclude/src/main/resources/org/apache/maven/from/a/test.properties
@@ -0,0 +1,18 @@
+# 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.
+
+testprop=proptest
diff --git a/src/it/reloc-abs-resource-path-exclude/src/main/resources/org/apache/maven/from/b/test.properties b/src/it/reloc-abs-resource-path-exclude/src/main/resources/org/apache/maven/from/b/test.properties
new file mode 100644
index 0000000..ab80429
--- /dev/null
+++ b/src/it/reloc-abs-resource-path-exclude/src/main/resources/org/apache/maven/from/b/test.properties
@@ -0,0 +1,18 @@
+# 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.
+
+testprop2=proptest2
diff --git a/src/main/java/org/apache/maven/plugins/shade/relocation/SimpleRelocator.java b/src/main/java/org/apache/maven/plugins/shade/relocation/SimpleRelocator.java
index b399b04..5606269 100644
--- a/src/main/java/org/apache/maven/plugins/shade/relocation/SimpleRelocator.java
+++ b/src/main/java/org/apache/maven/plugins/shade/relocation/SimpleRelocator.java
@@ -176,14 +176,14 @@ public class SimpleRelocator
             path = path.substring( 0, path.length() - 6 );
         }
 
-        if ( !isIncluded( path ) || isExcluded( path ) )
+        // Allow for annoying option of an extra / on the front of a path. See MSHADE-119; comes from
+        // getClass().getResource("/a/b/c.properties").
+        if ( !path.isEmpty() && path.charAt( 0 ) == '/' )
         {
-            return false;
+            path = path.substring( 1 );
         }
 
-        // Allow for annoying option of an extra / on the front of a path. See MSHADE-119; comes from
-        // getClass().getResource("/a/b/c.properties").
-        return path.startsWith( pathPattern ) || path.startsWith ( "/" + pathPattern );
+        return isIncluded( path ) && !isExcluded( path ) && path.startsWith( pathPattern );
     }
 
     public boolean canRelocateClass( String clazz )
diff --git a/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java b/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java
index e0c91b6..13dabac 100644
--- a/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java
@@ -101,13 +101,32 @@ public class SimpleRelocatorTest
         relocator = new SimpleRelocator( "^META-INF/org.foo.xml$", null, null, null, true );
         assertEquals( true, relocator.canRelocatePath( "META-INF/org.foo.xml" ) );
     }
-    
+
     //MSHADE-119, make sure that the easy part of this works.
     public void testCanRelocateAbsClassPath() 
     {
         SimpleRelocator relocator = new SimpleRelocator( "org.apache.velocity", "org.apache.momentum", null, null );
         assertEquals("/org/apache/momentum/mass.properties", relocator.relocatePath( "/org/apache/velocity/mass.properties" ) );
-        
+    }
+
+    public void testCanRelocateAbsClassPathWithExcludes()
+    {
+        SimpleRelocator relocator = new SimpleRelocator( "org/apache/velocity", "org/apache/momentum", null,
+                                                         Arrays.asList( "org/apache/velocity/excluded/*" ) );
+        assertTrue( relocator.canRelocatePath( "/org/apache/velocity/mass.properties" ) );
+        assertTrue( relocator.canRelocatePath( "org/apache/velocity/mass.properties" ) );
+        assertFalse( relocator.canRelocatePath( "/org/apache/velocity/excluded/mass.properties" ) );
+        assertFalse( relocator.canRelocatePath( "org/apache/velocity/excluded/mass.properties" ) );
+    }
+
+    public void testCanRelocateAbsClassPathWithIncludes()
+    {
+        SimpleRelocator relocator = new SimpleRelocator( "org/apache/velocity", "org/apache/momentum",
+                                                         Arrays.asList( "org/apache/velocity/included/*" ), null );
+        assertFalse( relocator.canRelocatePath( "/org/apache/velocity/mass.properties" ) );
+        assertFalse( relocator.canRelocatePath( "org/apache/velocity/mass.properties" ) );
+        assertTrue( relocator.canRelocatePath( "/org/apache/velocity/included/mass.properties" ) );
+        assertTrue( relocator.canRelocatePath( "org/apache/velocity/included/mass.properties" ) );
     }
 
     public void testRelocatePath()

-- 
To stop receiving notification emails like this one, please contact
gboue@apache.org.