Author: bentmann
Date: Wed Dec 30 20:22:42 2009
New Revision: 894704

URL: http://svn.apache.org/viewvc?rev=894704&view=rev
Log:
[MSHADE-52] non-attached shade in pom package fails

Added:
    maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/
    maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/pom.xml   (with 
props)
    maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/verify.bsh   
(with props)
Modified:
    
maven/plugins/trunk/maven-shade-plugin/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java

Added: maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/pom.xml?rev=894704&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/pom.xml (added)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/pom.xml Wed Dec 
30 20:22:42 2009
@@ -0,0 +1,65 @@
+<?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>test</artifactId>
+  <version>1.0</version>
+  <packaging>pom</packaging>
+
+  <name>MSHADE-52</name>
+  <description>
+    Test that usage of the plugin within a POM project is supported. Think 
about Shade being used to create some
+    archive that is not meant to be deployed on its own but to be included in 
some final/bigger assembly.
+  </description>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.2</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            
<id>create-shaded-bundle-of-some-artifacts-for-inclusion-in-assembly</id>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <outputFile>${project.build.directory}/shaded.jar</outputFile>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

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

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

Added: maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/verify.bsh
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/verify.bsh?rev=894704&view=auto
==============================================================================
--- maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/verify.bsh 
(added)
+++ maven/plugins/trunk/maven-shade-plugin/src/it/pom-packaging/verify.bsh Wed 
Dec 30 20:22:42 2009
@@ -0,0 +1,31 @@
+import java.io.*;
+import java.util.jar.*;
+
+String[] wanted =
+{
+    "junit/framework/TestCase.class",
+};
+
+String[] unwanted =
+{
+};
+
+JarFile jarFile = new JarFile( new File( basedir, "target/shaded.jar" ) );
+
+for ( String path : wanted )
+{
+    if ( jarFile.getEntry( path ) == null )
+    {
+        throw new IllegalStateException( "wanted path is missing: " + path );
+    }
+}
+
+for ( String path : unwanted )
+{
+    if ( jarFile.getEntry( path ) != null )
+    {
+        throw new IllegalStateException( "unwanted path is present: " + path );
+    }
+}
+
+jarFile.close();

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

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

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=894704&r1=894703&r2=894704&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
 Wed Dec 30 20:22:42 2009
@@ -295,6 +295,16 @@
      */
     private boolean createSourcesJar;
 
+    /**
+     * The path to the output file for the shaded artifact. When this 
parameter is set, the created archive will neither
+     * replace the project's main artifact nor will it be attached. Hence, 
this parameter causes the parameters
+     * {...@link #finalName}, {...@link #shadedArtifactAttached}, {...@link 
#shadedClassifierName} and
+     * {...@link #createDependencyReducedPom} to be ignored when used.
+     * 
+     * @parameter
+     * @since 1.3
+     */
+    private File outputFile;
 
     /** @throws MojoExecutionException  */
     public void execute()
@@ -304,31 +314,35 @@
         Set artifactIds = new LinkedHashSet();
         Set sourceArtifacts = new LinkedHashSet();
 
-        if ( project.getArtifact().getFile() == null )
-        {
-            getLog().error( "The project main artifact does not exist. This 
could have the following" );
-            getLog().error( "reasons:" );
-            getLog().error( "- You have invoked the goal directly from the 
command line. This is not" );
-            getLog().error( "  supported. Please add the goal to the default 
lifecycle via an" );
-            getLog().error( "  <execution> element in your POM and use \"mvn 
package\" to have it run." );
-            getLog().error( "- You have bound the goal to a lifecycle phase 
before \"package\". Please" );
-            getLog().error( "  remove this binding from your POM such that the 
goal will be run in" );
-            getLog().error( "  the proper phase." );
-            throw new MojoExecutionException( "Failed to create shaded 
artifact.",
-                                              new IllegalStateException( 
"Project main artifact does not exist." ) );
-        }
-        artifacts.add( project.getArtifact().getFile() );
+        ArtifactSelector artifactSelector = new ArtifactSelector( artifactSet, 
shadedGroupFilter );
 
-        if ( createSourcesJar )
+        if ( artifactSelector.isSelected( project.getArtifact() ) && 
!"pom".equals( project.getArtifact().getType() ) )
         {
-            File file = shadedSourcesArtifactFile();
-            if ( file.exists() )
+            if ( project.getArtifact().getFile() == null )
             {
-                sourceArtifacts.add( file );
+                getLog().error( "The project main artifact does not exist. 
This could have the following" );
+                getLog().error( "reasons:" );
+                getLog().error( "- You have invoked the goal directly from the 
command line. This is not" );
+                getLog().error( "  supported. Please add the goal to the 
default lifecycle via an" );
+                getLog().error( "  <execution> element in your POM and use 
\"mvn package\" to have it run." );
+                getLog().error( "- You have bound the goal to a lifecycle 
phase before \"package\". Please" );
+                getLog().error( "  remove this binding from your POM such that 
the goal will be run in" );
+                getLog().error( "  the proper phase." );
+                throw new MojoExecutionException( "Failed to create shaded 
artifact.",
+                                                  new IllegalStateException( 
"Project main artifact does not exist." ) );
             }
-        }
 
-        ArtifactSelector artifactSelector = new ArtifactSelector( artifactSet, 
shadedGroupFilter );
+            artifacts.add( project.getArtifact().getFile() );
+
+            if ( createSourcesJar )
+            {
+                File file = shadedSourcesArtifactFile();
+                if ( file.isFile() )
+                {
+                    sourceArtifacts.add( file );
+                }
+            }
+        }
 
         for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
         {
@@ -340,8 +354,8 @@
 
                 continue;
             }
-            
-            if ( artifact.getType().equals( "pom" ) )
+
+            if ( "pom".equals( artifact.getType() ) )
             {
                 getLog().info( "Skipping pom dependency " + artifact.getId() + 
" in the shaded jar." );
                 continue;
@@ -364,7 +378,7 @@
         }
 
 
-        File outputJar = shadedArtifactFileWithClassifier();
+        File outputJar = ( outputFile != null ) ? outputFile : 
shadedArtifactFileWithClassifier();
         File sourcesJar = shadedSourceArtifactFileWithClassifier();
 
         // Now add our extra resources
@@ -383,56 +397,58 @@
                 shader.shade( sourceArtifacts, sourcesJar, filters, 
relocators, resourceTransformers );
             }
 
-            boolean renamed=false;
-            
-            // rename the output file if a specific finalName is set
-            // but don't rename if the finalName is the <build><finalName>
-            // because this will be handled implicitely later
-            if ( finalName != null && finalName.length() > 0 && 
-                 !finalName.equals( project.getBuild().getFinalName() ) )
+            if ( outputFile == null )
             {
-                String finalFileName = finalName + "." + 
project.getArtifact().getArtifactHandler().getExtension(); 
-                File finalFile = new File( outputDirectory, finalFileName );
-                replaceFile( finalFile, outputJar );                
-                outputJar = finalFile;
-                
-                renamed=true;
-            }
+                boolean renamed = false;
 
-            if ( shadedArtifactAttached )
-            {
-                getLog().info( "Attaching shaded artifact." );
-                projectHelper.attachArtifact( project, 
project.getArtifact().getType(), shadedClassifierName, outputJar );
-                if ( createSourcesJar )
+                // rename the output file if a specific finalName is set
+                // but don't rename if the finalName is the <build><finalName>
+                // because this will be handled implicitely later
+                if ( finalName != null && finalName.length() > 0 && 
!finalName.equals( project.getBuild().getFinalName() ) )
                 {
-                    projectHelper.attachArtifact( project, "jar", 
shadedClassifierName + "-sources", sourcesJar );
+                    String finalFileName = finalName + "." + 
project.getArtifact().getArtifactHandler().getExtension();
+                    File finalFile = new File( outputDirectory, finalFileName 
);
+                    replaceFile( finalFile, outputJar );
+                    outputJar = finalFile;
+
+                    renamed = true;
                 }
-            }
-            else if ( !renamed )
-            {
-                getLog().info( "Replacing original artifact with shaded 
artifact." );
-                File originalArtifact = project.getArtifact().getFile();
-                replaceFile( originalArtifact, outputJar );
 
-                if ( createSourcesJar )
+                if ( shadedArtifactAttached )
+                {
+                    getLog().info( "Attaching shaded artifact." );
+                    projectHelper.attachArtifact( project, 
project.getArtifact().getType(), shadedClassifierName,
+                                                  outputJar );
+                    if ( createSourcesJar )
+                    {
+                        projectHelper.attachArtifact( project, "jar", 
shadedClassifierName + "-sources", sourcesJar );
+                    }
+                }
+                else if ( !renamed )
                 {
-                    File shadedSources = shadedSourcesArtifactFile();
+                    getLog().info( "Replacing original artifact with shaded 
artifact." );
+                    File originalArtifact = project.getArtifact().getFile();
+                    replaceFile( originalArtifact, outputJar );
 
-                    replaceFile( shadedSources, sourcesJar );
+                    if ( createSourcesJar )
+                    {
+                        File shadedSources = shadedSourcesArtifactFile();
 
-                    projectHelper.attachArtifact( project, "jar",
-                                                  "sources", shadedSources );
-                }
+                        replaceFile( shadedSources, sourcesJar );
 
-                if ( createDependencyReducedPom )
-                {
-                    createDependencyReducedPom( artifactIds );
+                        projectHelper.attachArtifact( project, "jar", 
"sources", shadedSources );
+                    }
+
+                    if ( createDependencyReducedPom )
+                    {
+                        createDependencyReducedPom( artifactIds );
+                    }
                 }
             }
         }
         catch ( Exception e )
         {
-            throw new MojoExecutionException( "Error creating shaded jar.", e 
);
+            throw new MojoExecutionException( "Error creating shaded jar: " + 
e.getMessage(), e );
         }
     }
 


Reply via email to