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

svn commit: r925876 - in /maven/plugins/trunk/maven-shade-plugin/src: it/manifest-transformed/ main/java/org/apache/maven/plugins/shade/ main/java/org/apache/maven/plugins/shade/resource/

Author: bentmann
Date: Sun Mar 21 19:56:02 2010
New Revision: 925876

URL: http://svn.apache.org/viewvc?rev=925876&view=rev
Log:
[MSHADE-77] The generated file is not a valid jar

Added:
    maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/
    maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml   (with props)
    maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh   (with props)
Modified:
    maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
    maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java

Added: maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml?rev=925876&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml (added)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml Sun Mar 21 19:56:02 2010
@@ -0,0 +1,108 @@
+<?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>
+
+  <groupId>org.apache.maven.its.shade.mt</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <name>MSHADE-77</name>
+  <description>
+    Test that manifest is properly transformed and ends up as the first entry in the JAR.
+  </description>
+
+  <dependencies>
+    <dependency>
+      <!-- dummy dependency to test interaction of multiple manifests -->
+      <groupId>org.apache.maven.plugins</groupId>
+      <artifactId>maven-shade-plugin</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.0.2</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-install-plugin</artifactId>
+        <version>2.2</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>2.2</version>
+        <configuration>
+          <archive>
+            <manifest>
+              <mainClass>org.apache.maven.Main</mainClass>
+            </manifest>
+            <manifestEntries>
+              <Test-Entry>PASSED</Test-Entry>
+            </manifestEntries>
+          </archive>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-resources-plugin</artifactId>
+        <version>2.2</version>
+      </plugin>
+      <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.ManifestResourceTransformer">
+                  <mainClass>org.apache.maven.Shade</mainClass>
+                  <manifestEntries>
+                    <Test-Entry>PASSED</Test-Entry>
+                  </manifestEntries>
+                </transformer>
+              </transformers>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <version>2.3.1</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh?rev=925876&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh (added)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh Sun Mar 21 19:56:02 2010
@@ -0,0 +1,22 @@
+import java.io.*;
+import java.util.jar.*;
+
+// NOTE: We deliberately use JarInputStream and not JarFile here!
+JarInputStream jarStream = new JarInputStream( new FileInputStream( new File( basedir, "target/test-1.0.jar" ) ) );
+Manifest mf = jarStream.getManifest();
+jarStream.close();
+
+if ( mf == null )
+{
+    throw new IllegalStateException( "META-INF/MANIFEST.MF is missing" );
+}
+
+if ( !"PASSED".equals( mf.getMainAttributes().getValue( "Test-Entry" ) ) )
+{
+    throw new IllegalStateException( "META-INF/MANIFEST.MF is incomplete" );
+}
+
+if ( !"org.apache.maven.Shade".equals( mf.getMainAttributes().getValue( "Main-Class" ) ) )
+{
+    throw new IllegalStateException( "META-INF/MANIFEST.MF is incomplete" );
+}

Propchange: maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-shade-plugin/src/it/manifest-transformed/verify.bsh
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java?rev=925876&r1=925875&r2=925876&view=diff
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java (original)
+++ maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java Sun Mar 21 19:56:02 2010
@@ -37,6 +37,7 @@ import java.util.regex.Pattern;
 import java.util.zip.ZipException;
 
 import org.apache.maven.plugins.shade.relocation.Relocator;
+import org.apache.maven.plugins.shade.resource.ManifestResourceTransformer;
 import org.apache.maven.plugins.shade.resource.ResourceTransformer;
 import org.apache.maven.plugins.shade.filter.Filter;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
@@ -60,29 +61,54 @@ public class DefaultShader
     {
         Set resources = new HashSet();
 
+        ResourceTransformer manifestTransformer = null;
+        List transformers = new ArrayList( resourceTransformers );
+        for ( Iterator it = transformers.iterator(); it.hasNext(); )
+        {
+            ResourceTransformer transformer = (ResourceTransformer) it.next();
+            if ( transformer instanceof ManifestResourceTransformer )
+            {
+                manifestTransformer = transformer;
+                it.remove();
+            }
+        }
+
         RelocatorRemapper remapper = new RelocatorRemapper( relocators );
 
         uberJar.getParentFile().mkdirs();
         JarOutputStream jos = new JarOutputStream( new FileOutputStream( uberJar ) );
 
+        if ( manifestTransformer != null )
+        {
+            for ( Iterator it = jars.iterator(); it.hasNext(); )
+            {
+                File jar = (File) it.next();
+                JarFile jarFile = newJarFile( jar );
+                for ( Enumeration en = jarFile.entries(); en.hasMoreElements(); )
+                {
+                    JarEntry entry = (JarEntry) en.nextElement();
+                    String resource = entry.getName();
+                    if ( manifestTransformer.canTransformResource( resource ) )
+                    {
+                        resources.add( resource );
+                        manifestTransformer.processResource( resource, jarFile.getInputStream( entry ), relocators );
+                        break;
+                    }
+                }
+            }
+            if ( manifestTransformer.hasTransformedResource() )
+            {
+                manifestTransformer.modifyOutputStream( jos );
+            }
+        }
+
         for ( Iterator i = jars.iterator(); i.hasNext(); )
         {
             File jar = (File) i.next();
 
             List jarFilters = getFilters( jar, filters );
 
-            JarFile jarFile;
-            
-            try 
-            {
-                jarFile = new JarFile( jar );
-            }
-            catch (ZipException zex)
-            {
-                // JarFile is not very verbose and doesn't tell the user which file it was
-                // so we will create a new Exception instead
-                throw new ZipException( "error in opening zip file " + jar );
-            }
+            JarFile jarFile = newJarFile( jar );
 
             for ( Enumeration j = jarFile.entries(); j.hasMoreElements(); )
             {
@@ -121,7 +147,7 @@ public class DefaultShader
                     }
                     else
                     {
-                        if ( !resourceTransformed( resourceTransformers, mappedName, is, relocators ) )
+                        if ( !resourceTransformed( transformers, mappedName, is, relocators ) )
                         {
                             // Avoid duplicates that aren't accounted for by the resource transformers
                             if ( resources.contains( mappedName ) )
@@ -140,7 +166,7 @@ public class DefaultShader
             jarFile.close();
         }
 
-        for ( Iterator i = resourceTransformers.iterator(); i.hasNext(); )
+        for ( Iterator i = transformers.iterator(); i.hasNext(); )
         {
             ResourceTransformer transformer = (ResourceTransformer) i.next();
 
@@ -153,6 +179,21 @@ public class DefaultShader
         IOUtil.close( jos );
     }
 
+    private JarFile newJarFile( File jar )
+        throws IOException
+    {
+        try
+        {
+            return new JarFile( jar );
+        }
+        catch ( ZipException zex )
+        {
+            // JarFile is not very verbose and doesn't tell the user which file it was
+            // so we will create a new Exception instead
+            throw new ZipException( "error in opening zip file " + jar );
+        }
+    }
+
     private List getFilters( File jar, List filters )
     {
         List list = new ArrayList();

Modified: maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java?rev=925876&r1=925875&r2=925876&view=diff
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java (original)
+++ maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/resource/ManifestResourceTransformer.java Sun Mar 21 19:56:02 2010
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 
@@ -42,7 +43,6 @@ import org.codehaus.plexus.util.IOUtil;
 public class ManifestResourceTransformer
     implements ResourceTransformer
 {
-    private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
 
     // Configuration
     private String mainClass;
@@ -54,7 +54,7 @@ public class ManifestResourceTransformer
 
     public boolean canTransformResource( String resource )
     {
-        if ( MANIFEST_PATH.equalsIgnoreCase( resource ) )
+        if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
         {
             return true;
         }
@@ -106,7 +106,7 @@ public class ManifestResourceTransformer
             }
         }
 
-        jos.putNextEntry( new JarEntry( MANIFEST_PATH ) );
+        jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
         manifest.write( jos );
     }
 }