View Javadoc

1   package org.apache.maven.plugin.war.packaging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.war.Overlay;
26  import org.codehaus.plexus.interpolation.InterpolationException;
27  
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Set;
33  
34  /**
35   * Handles the artifacts that needs to be packaged in the web application.
36   *
37   * @author Stephane Nicoll
38   * 
39   * @version $Id: ArtifactsPackagingTask.java 985593 2010-08-14 22:12:09Z dennisl $
40   */
41  public class ArtifactsPackagingTask
42      extends AbstractWarPackagingTask
43  {
44  
45      public static final String TLD_PATH = "WEB-INF/tld/";
46  
47      public static final String SERVICES_PATH = "WEB-INF/services/";
48  
49      private final Set artifacts;
50  
51      private final String id;
52  
53  
54      public ArtifactsPackagingTask( Set artifacts, Overlay currentProjectOverlay )
55      {
56          this.artifacts = artifacts;
57          this.id = currentProjectOverlay.getId();
58      }
59  
60  
61      public void performPackaging( WarPackagingContext context )
62          throws MojoExecutionException
63      {
64          try
65          {
66          final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
67          final List duplicates = findDuplicates( context, artifacts );
68  
69          for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
70          {
71              Artifact artifact = (Artifact) iter.next();
72              String targetFileName = getArtifactFinalName( context, artifact );
73  
74              context.getLog().debug( "Processing: " + targetFileName );
75  
76              if ( duplicates.contains( targetFileName ) )
77              {
78                  context.getLog().debug( "Duplicate found: " + targetFileName );
79                  targetFileName = artifact.getGroupId() + "-" + targetFileName;
80                  context.getLog().debug( "Renamed to: " + targetFileName );
81              }
82              context.getWebappStructure().registerTargetFileName( artifact, targetFileName );
83  
84              if ( !artifact.isOptional() && filter.include( artifact ) )
85              {
86                  try
87                  {
88                      String type = artifact.getType();
89                      if ( "tld".equals( type ) )
90                      {
91                          copyFile( id, context, artifact.getFile(), TLD_PATH + targetFileName );
92                      }
93                      else if ( "aar".equals( type ) )
94                      {
95                          copyFile( id, context, artifact.getFile(), SERVICES_PATH + targetFileName );
96                      }
97                      else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
98                          || "test-jar".equals( type ) )
99                      {
100                         copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
101                     }
102                     else if ( "par".equals( type ) )
103                     {
104                         targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
105                         copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
106                     }
107                     else if ( "war".equals( type ) )
108                     {
109                         // Nothing to do here, it is an overlay and it's already handled
110                         context.getLog().debug( "war artifacts are handled as overlays, ignoring [" + artifact + "]" );
111                     }
112                     else if ( "zip".equals( type ) )
113                     {
114                         // Nothing to do here, it is an overlay and it's already handled
115                         context.getLog().debug( "zip artifacts are handled as overlays, ignoring [" + artifact + "]" );
116                     }
117                     else
118                     {
119                         context.getLog().debug(
120                             "Artifact of type [" + type + "] is not supported, ignoring [" + artifact + "]" );
121                     }
122                 }
123                 catch ( IOException e )
124                 {
125                     throw new MojoExecutionException( "Failed to copy file for artifact [" + artifact + "]", e );
126                 }
127             }
128         }
129         }
130         catch ( InterpolationException e )
131         {
132             throw new MojoExecutionException( e.getMessage(), e );
133         }
134     }
135 
136     /**
137      * Searches a set of artifacts for duplicate filenames and returns a list
138      * of duplicates.
139      *
140      * @param context   the packaging context
141      * @param artifacts set of artifacts
142      * @return List of duplicated artifacts as bundling file names
143      */
144     private List findDuplicates( WarPackagingContext context, Set artifacts ) 
145         throws InterpolationException
146     {
147         List duplicates = new ArrayList();
148         List identifiers = new ArrayList();
149         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
150         {
151             Artifact artifact = (Artifact) iter.next();
152             String candidate = getArtifactFinalName( context, artifact );
153             if ( identifiers.contains( candidate ) )
154             {
155                 duplicates.add( candidate );
156             }
157             else
158             {
159                 identifiers.add( candidate );
160             }
161         }
162         return duplicates;
163     }
164 }