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/19 13:07:10 UTC

[maven-shade-plugin] 01/01: [MSHARED-390] Sisu Index transformer

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

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

commit 09baabc1ea7eb212405e1385d5e98a6dd931e13d
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Wed May 19 15:06:22 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 ++++
 .../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  |  61 ++++++++
 .../resources/META-INF/sisu/javax.inject.Named     |   1 +
 .../projects/MSHADE-390-sisu-index/verify.groovy   |  24 +++
 .../resource/SisuIndexResourceTransformer.java     | 162 +++++++++++++++++++++
 9 files changed, 346 insertions(+)

diff --git a/pom.xml b/pom.xml
index 5e48386..f6d2af2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -220,6 +220,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/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..158662b
--- /dev/null
+++ b/src/it/projects/MSHADE-390-sisu-index/two/pom.xml
@@ -0,0 +1,61 @@
+<?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>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
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..96965d6
--- /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.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..97f05f7
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/shade/resource/SisuIndexResourceTransformer.java
@@ -0,0 +1,162 @@
+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.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.StringReader;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.plugins.shade.relocation.Relocator;
+
+/**
+ * Resources 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 ServiceStream serviceStream;
+
+    private List<Relocator> relocators;
+
+    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
+    {
+        if ( serviceStream == null )
+        {
+            serviceStream = new ServiceStream();
+        }
+
+        final String content = IOUtils.toString( is, StandardCharsets.UTF_8 );
+        StringReader reader = new StringReader( content );
+        BufferedReader lineReader = new BufferedReader( reader );
+        String line;
+        while ( ( line = lineReader.readLine() ) != null )
+        {
+            String relContent = line;
+            for ( Relocator relocator : relocators )
+            {
+                if ( relocator.canRelocateClass( relContent ) )
+                {
+                    relContent = relocator.applyToSourceContent( relContent );
+                }
+            }
+            serviceStream.append( relContent + "\n" );
+        }
+
+        if ( this.relocators == null )
+        {
+            this.relocators = relocators;
+        }
+
+        if ( time > this.time )
+        {
+            this.time = time;        
+        }
+    }
+
+    @Override
+    public boolean hasTransformedResource()
+    {
+        return serviceStream != null;
+    }
+
+    @Override
+    public void modifyOutputStream( final JarOutputStream jos )
+        throws IOException
+    {
+        ServiceStream data = serviceStream;
+
+        JarEntry jarEntry = new JarEntry( SISU_INDEX_PATH );
+        jarEntry.setTime( time );
+        jos.putNextEntry( jarEntry );
+
+
+        // read the content of service file for candidate classes for relocation.
+        // Specification requires that this file is encoded in UTF-8.
+        Writer writer = new OutputStreamWriter( jos, StandardCharsets.UTF_8 );
+        InputStreamReader streamReader = new InputStreamReader( data.toInputStream() );
+        BufferedReader reader = new BufferedReader( streamReader );
+        String className;
+
+        while ( ( className = reader.readLine() ) != null )
+        {
+            writer.write( className );
+            writer.write( System.lineSeparator() );
+            writer.flush();
+        }
+
+        reader.close();
+        data.reset();
+   }
+
+    static class ServiceStream
+        extends ByteArrayOutputStream
+    {
+
+        ServiceStream()
+        {
+            super( 1024 );
+        }
+
+        public void append( String content )
+            throws IOException
+        {
+            if ( count > 0 && buf[count - 1] != '\n' && buf[count - 1] != '\r' )
+            {
+                write( '\n' );
+            }
+
+            byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 );
+            this.write( contentBytes );
+        }
+
+        public InputStream toInputStream()
+        {
+            return new ByteArrayInputStream( buf, 0, count );
+        }
+    }
+}