View Javadoc
1   package org.apache.maven.plugins.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 java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.war.Overlay;
31  import org.codehaus.plexus.interpolation.InterpolationException;
32  
33  /**
34   * Handles the artifacts that needs to be packaged in the web application.
35   *
36   * @author Stephane Nicoll
37   * @version $Id$
38   */
39  public class ArtifactsPackagingTask
40      extends AbstractWarPackagingTask
41  {
42  
43      /**
44       * The {@code tld} path.
45       */
46      public static final String TLD_PATH = "WEB-INF/tld/";
47  
48      /**
49       * The {@code services} path.
50       */
51      public static final String SERVICES_PATH = "WEB-INF/services/";
52  
53      /**
54       * The {@code modules} path.
55       */
56      public static final String MODULES_PATH = "WEB-INF/modules/";
57  
58      /**
59       * The {@code extensions} path.
60       */
61      public static final String EXTENSIONS_PATH = "WEB-INF/extensions/";
62  
63      private final Set<Artifact> artifacts;
64  
65      private final String id;
66  
67      /**
68       * @param artifacts {@link #artifacts}
69       * @param currentProjectOverlay {@link #id}
70       */
71      public ArtifactsPackagingTask( Set<Artifact> artifacts, Overlay currentProjectOverlay )
72      {
73          this.artifacts = artifacts;
74          this.id = currentProjectOverlay.getId();
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      public void performPackaging( WarPackagingContext context )
81          throws MojoExecutionException
82      {
83          try
84          {
85              final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
86              final List<String> duplicates = findDuplicates( context, artifacts );
87  
88              for ( Artifact artifact : artifacts )
89              {
90                  String targetFileName = getArtifactFinalName( context, artifact );
91  
92                  context.getLog().debug( "Processing: " + targetFileName );
93  
94                  if ( duplicates.contains( targetFileName ) )
95                  {
96                      context.getLog().debug( "Duplicate found: " + targetFileName );
97                      targetFileName = artifact.getGroupId() + "-" + targetFileName;
98                      context.getLog().debug( "Renamed to: " + targetFileName );
99                  }
100                 context.getWebappStructure().registerTargetFileName( artifact, targetFileName );
101 
102                 if ( !artifact.isOptional() && filter.include( artifact ) )
103                 {
104                     try
105                     {
106                         String type = artifact.getType();
107                         if ( "tld".equals( type ) )
108                         {
109                             copyFile( id, context, artifact.getFile(), TLD_PATH + targetFileName );
110                         }
111                         else if ( "aar".equals( type ) )
112                         {
113                             copyFile( id, context, artifact.getFile(), SERVICES_PATH + targetFileName );
114                         }
115                         else if ( "mar".equals( type ) )
116                         {
117                             copyFile( id, context, artifact.getFile(), MODULES_PATH + targetFileName );
118                         }
119                         else if ( "xar".equals( type ) )
120                         {
121                             copyFile( id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName );
122                         }
123                         else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
124                             || "test-jar".equals( type ) || "bundle".equals( type ) )
125                         {
126                             copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
127                         }
128                         else if ( "par".equals( type ) )
129                         {
130                             targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
131                             copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
132                         }
133                         else if ( "war".equals( type ) )
134                         {
135                             // Nothing to do here, it is an overlay and it's already handled
136                             context.getLog().debug( "war artifacts are handled as overlays, ignoring [" + artifact
137                                                         + "]" );
138                         }
139                         else if ( "zip".equals( type ) )
140                         {
141                             // Nothing to do here, it is an overlay and it's already handled
142                             context.getLog().debug( "zip artifacts are handled as overlays, ignoring [" + artifact
143                                                         + "]" );
144                         }
145                         else
146                         {
147                             context.getLog().debug( "Artifact of type [" + type + "] is not supported, ignoring ["
148                                                         + artifact + "]" );
149                         }
150                     }
151                     catch ( IOException e )
152                     {
153                         throw new MojoExecutionException( "Failed to copy file for artifact [" + artifact + "]", e );
154                     }
155                 }
156             }
157         }
158         catch ( InterpolationException e )
159         {
160             throw new MojoExecutionException( e.getMessage(), e );
161         }
162     }
163 
164     /**
165      * Searches a set of artifacts for duplicate filenames and returns a list of duplicates.
166      *
167      * @param context the packaging context
168      * @param artifacts set of artifacts
169      * @return List of duplicated artifacts as bundling file names
170      */
171     private List<String> findDuplicates( WarPackagingContext context, Set<Artifact> artifacts )
172         throws InterpolationException
173     {
174         List<String> duplicates = new ArrayList<String>();
175         List<String> identifiers = new ArrayList<String>();
176         for ( Artifact artifact : artifacts )
177         {
178             String candidate = getArtifactFinalName( context, artifact );
179             if ( identifiers.contains( candidate ) )
180             {
181                 duplicates.add( candidate );
182             }
183             else
184             {
185                 identifiers.add( candidate );
186             }
187         }
188         return duplicates;
189     }
190 }