You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2021/05/21 07:37:14 UTC

[maven-shade-plugin] branch master updated: [MSHARED-390] Sisu Index transformer

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e6902dd  [MSHARED-390] Sisu Index transformer
e6902dd is described below

commit e6902dd34a917014864162ca05d06fee14f4e4c5
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Fri May 21 09:36:46 2021 +0200

    [MSHARED-390] Sisu Index transformer
    
    https://issues.apache.org/jira/browse/MSHADE-390
    
    Adds SISU index transformer to existing ones + IT.
---
 pom.xml                                            |  1 +
 .../MSHADE-390-sisu-index/invoker.properties       | 18 ++++
 src/it/projects/MSHADE-390-sisu-index/one/pom.xml  | 30 +++++++
 .../one/src/main/java/org/apache/one/One.java      |  5 ++
 .../resources/META-INF/sisu/javax.inject.Named     |  1 +
 src/it/projects/MSHADE-390-sisu-index/pom.xml      | 48 +++++++++++
 src/it/projects/MSHADE-390-sisu-index/two/pom.xml  | 67 +++++++++++++++
 .../two/src/main/java/org/apache/two/Two.java      |  5 ++
 .../resources/META-INF/sisu/javax.inject.Named     |  1 +
 .../projects/MSHADE-390-sisu-index/verify.groovy   | 24 ++++++
 .../resource/SisuIndexResourceTransformer.java     | 98 ++++++++++++++++++++++
 11 files changed, 298 insertions(+)

diff --git a/pom.xml b/pom.xml
index ec4dba7..c428afb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -274,6 +274,7 @@
               <exclude>src/it/projects/rerun-with-reloc/src/main/resources/some-ordinary-resource.txt</exclude>
               <exclude>src/it/projects/rerun-without-reloc/src/main/resources/some-ordinary-resource.txt</exclude>
               <exclude>src/it/projects/MSHADE-182/src/main/resources/META-INF/services/relocateme.Service</exclude>
+              <exclude>src/it/projects/MSHADE-390-sisu-index/**</exclude>
             </excludes>
          </configuration>
         </plugin>
diff --git a/src/it/projects/MSHADE-390-sisu-index/invoker.properties b/src/it/projects/MSHADE-390-sisu-index/invoker.properties
new file mode 100644
index 0000000..bdf5c61
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/invoker.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.
+
+invoker.goals=clean package
diff --git a/src/it/projects/MSHADE-390-sisu-index/one/pom.xml b/src/it/projects/MSHADE-390-sisu-index/one/pom.xml
new file mode 100644
index 0000000..deab2b0
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/one/pom.xml
@@ -0,0 +1,30 @@
+<?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>org.apache.maven.its.shade.pp</groupId>
+    <artifactId>mshade-390</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>one</artifactId>
+
+</project>
diff --git a/src/it/projects/MSHADE-390-sisu-index/one/src/main/java/org/apache/one/One.java b/src/it/projects/MSHADE-390-sisu-index/one/src/main/java/org/apache/one/One.java
new file mode 100644
index 0000000..48f19dc
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/one/src/main/java/org/apache/one/One.java
@@ -0,0 +1,5 @@
+package org.apache.one;
+
+public class One {
+
+}
\ No newline at end of file
diff --git a/src/it/projects/MSHADE-390-sisu-index/one/src/main/resources/META-INF/sisu/javax.inject.Named b/src/it/projects/MSHADE-390-sisu-index/one/src/main/resources/META-INF/sisu/javax.inject.Named
new file mode 100644
index 0000000..9b1cbb7
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/one/src/main/resources/META-INF/sisu/javax.inject.Named
@@ -0,0 +1 @@
+org.apache.one.One
\ No newline at end of file
diff --git a/src/it/projects/MSHADE-390-sisu-index/pom.xml b/src/it/projects/MSHADE-390-sisu-index/pom.xml
new file mode 100644
index 0000000..c80b6fd
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/pom.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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.shade.pp</groupId>
+  <artifactId>mshade-390</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+
+  <dependencyManagement>
+    <dependencies>
+      <dependency>
+        <groupId>org.apache.maven.its.shade.pp</groupId>
+        <artifactId>one</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+    </dependencies>
+  </dependencyManagement>
+
+  <modules>
+    <module>one</module>
+    <module>two</module>
+  </modules>
+
+</project>
\ No newline at end of file
diff --git a/src/it/projects/MSHADE-390-sisu-index/two/pom.xml b/src/it/projects/MSHADE-390-sisu-index/two/pom.xml
new file mode 100644
index 0000000..a196a94
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/two/pom.xml
@@ -0,0 +1,67 @@
+<?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>org.apache.maven.its.shade.pp</groupId>
+    <artifactId>mshade-390</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>two</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.its.shade.pp</groupId>
+      <artifactId>one</artifactId>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>attach-shade</id>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <shadedArtifactAttached>false</shadedArtifactAttached>
+              <transformers>
+                <transformer implementation="org.apache.maven.plugins.shade.resource.SisuIndexResourceTransformer"/>
+              </transformers>
+              <relocations>
+                <relocation>
+                  <pattern>org.apache.one</pattern>
+                  <shadedPattern>org.apache.two.org.apache.one</shadedPattern>
+                </relocation>
+              </relocations>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/MSHADE-390-sisu-index/two/src/main/java/org/apache/two/Two.java b/src/it/projects/MSHADE-390-sisu-index/two/src/main/java/org/apache/two/Two.java
new file mode 100644
index 0000000..da4e5b2
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/two/src/main/java/org/apache/two/Two.java
@@ -0,0 +1,5 @@
+package org.apache.two;
+
+public class Two {
+
+}
\ No newline at end of file
diff --git a/src/it/projects/MSHADE-390-sisu-index/two/src/main/resources/META-INF/sisu/javax.inject.Named b/src/it/projects/MSHADE-390-sisu-index/two/src/main/resources/META-INF/sisu/javax.inject.Named
new file mode 100644
index 0000000..d633f18
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/two/src/main/resources/META-INF/sisu/javax.inject.Named
@@ -0,0 +1 @@
+org.apache.two.Two
\ No newline at end of file
diff --git a/src/it/projects/MSHADE-390-sisu-index/verify.groovy b/src/it/projects/MSHADE-390-sisu-index/verify.groovy
new file mode 100644
index 0000000..a1a7517
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/verify.groovy
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+def jarFile = new java.util.jar.JarFile(new File(basedir, "two/target/two-1.0-SNAPSHOT.jar"))
+def jarEntry = jarFile.getJarEntry("META-INF/sisu/javax.inject.Named")
+def merged = jarFile.getInputStream(jarEntry).getText()
+assert merged.contains( 'org.apache.two.org.apache.one.One' )
+assert merged.contains( 'org.apache.two.Two' )
diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/SisuIndexResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/SisuIndexResourceTransformer.java
new file mode 100644
index 0000000..546970d
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/SisuIndexResourceTransformer.java
@@ -0,0 +1,98 @@
+package org.apache.maven.plugins.shade.resource;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.plugins.shade.relocation.Relocator;
+
+/**
+ * Resource transformer that relocates classes in {@code META-INF/sisu/javax.inject.Named} and appends resources
+ * into a single resource.
+ *
+ * @since 3.3.0
+ */
+public class SisuIndexResourceTransformer
+    extends AbstractCompatibilityTransformer
+{
+    private static final String SISU_INDEX_PATH = "META-INF/sisu/javax.inject.Named";
+
+    private final ArrayList<String> indexEntries = new ArrayList<>();
+
+    private long time = Long.MIN_VALUE;
+
+    @Override
+    public boolean canTransformResource( final String resource )
+    {
+        return resource.equals( SISU_INDEX_PATH );
+    }
+
+    @Override
+    public void processResource( final String resource,
+                                 final InputStream is,
+                                 final List<Relocator> relocators,
+                                 long time ) throws IOException
+    {
+        Scanner scanner = new Scanner( is, StandardCharsets.UTF_8.name() );
+        while ( scanner.hasNextLine() )
+        {
+            String relContent = scanner.nextLine();
+            for ( Relocator relocator : relocators )
+            {
+                if ( relocator.canRelocateClass( relContent ) )
+                {
+                    relContent = relocator.applyToSourceContent( relContent );
+                }
+            }
+            indexEntries.add( relContent );
+        }
+
+        if ( time > this.time )
+        {
+            this.time = time;        
+        }
+    }
+
+    @Override
+    public boolean hasTransformedResource()
+    {
+        return !indexEntries.isEmpty();
+    }
+
+    @Override
+    public void modifyOutputStream( final JarOutputStream jos )
+        throws IOException
+    {
+        JarEntry jarEntry = new JarEntry( SISU_INDEX_PATH );
+        jarEntry.setTime( time );
+        jos.putNextEntry( jarEntry );
+        IOUtils.writeLines( indexEntries, "\n", jos, StandardCharsets.UTF_8 );
+        jos.flush();
+        indexEntries.clear();
+   }
+}