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 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.plugin.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: ArtifactsPackagingTask.html 925069 2014-10-08 17:03:57Z khmarbaise $
38   */
39  public class ArtifactsPackagingTask
40      extends AbstractWarPackagingTask
41  {
42  
43      public static final String TLD_PATH = "WEB-INF/tld/";
44  
45      public static final String SERVICES_PATH = "WEB-INF/services/";
46  
47      public static final String MODULES_PATH = "WEB-INF/modules/";
48  
49      public static final String EXTENSIONS_PATH = "WEB-INF/extensions/";
50  
51      private final Set<Artifact> artifacts;
52  
53      private final String id;
54  
55      public ArtifactsPackagingTask( Set<Artifact> artifacts, Overlay currentProjectOverlay )
56      {
57          this.artifacts = artifacts;
58          this.id = currentProjectOverlay.getId();
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<String> duplicates = findDuplicates( context, artifacts );
68  
69              for ( Artifact artifact : artifacts )
70              {
71                  String targetFileName = getArtifactFinalName( context, artifact );
72  
73                  context.getLog().debug( "Processing: " + targetFileName );
74  
75                  if ( duplicates.contains( targetFileName ) )
76                  {
77                      context.getLog().debug( "Duplicate found: " + targetFileName );
78                      targetFileName = artifact.getGroupId() + "-" + targetFileName;
79                      context.getLog().debug( "Renamed to: " + targetFileName );
80                  }
81                  context.getWebappStructure().registerTargetFileName( artifact, targetFileName );
82  
83                  if ( !artifact.isOptional() && filter.include( artifact ) )
84                  {
85                      try
86                      {
87                          String type = artifact.getType();
88                          if ( "tld".equals( type ) )
89                          {
90                              copyFile( id, context, artifact.getFile(), TLD_PATH + targetFileName );
91                          }
92                          else if ( "aar".equals( type ) )
93                          {
94                              copyFile( id, context, artifact.getFile(), SERVICES_PATH + targetFileName );
95                          }
96                          else if ( "mar".equals( type ) )
97                          {
98                              copyFile( id, context, artifact.getFile(), MODULES_PATH + targetFileName );
99                          }
100                         else if ( "xar".equals( type ) )
101                         {
102                             copyFile( id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName );
103                         }
104                         else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
105                             || "test-jar".equals( type ) || "bundle".equals( type ) )
106                         {
107                             copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
108                         }
109                         else if ( "par".equals( type ) )
110                         {
111                             targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
112                             copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
113                         }
114                         else if ( "war".equals( type ) )
115                         {
116                             // Nothing to do here, it is an overlay and it's already handled
117                             context.getLog().debug( "war artifacts are handled as overlays, ignoring [" + artifact
118                                                         + "]" );
119                         }
120                         else if ( "zip".equals( type ) )
121                         {
122                             // Nothing to do here, it is an overlay and it's already handled
123                             context.getLog().debug( "zip artifacts are handled as overlays, ignoring [" + artifact
124                                                         + "]" );
125                         }
126                         else
127                         {
128                             context.getLog().debug( "Artifact of type [" + type + "] is not supported, ignoring ["
129                                                         + artifact + "]" );
130                         }
131                     }
132                     catch ( IOException e )
133                     {
134                         throw new MojoExecutionException( "Failed to copy file for artifact [" + artifact + "]", e );
135                     }
136                 }
137             }
138         }
139         catch ( InterpolationException e )
140         {
141             throw new MojoExecutionException( e.getMessage(), e );
142         }
143     }
144 
145     /**
146      * Searches a set of artifacts for duplicate filenames and returns a list of duplicates.
147      *
148      * @param context the packaging context
149      * @param artifacts set of artifacts
150      * @return List of duplicated artifacts as bundling file names
151      */
152     private List<String> findDuplicates( WarPackagingContext context, Set<Artifact> artifacts )
153         throws InterpolationException
154     {
155         List<String> duplicates = new ArrayList<String>();
156         List<String> identifiers = new ArrayList<String>();
157         for ( Artifact artifact : artifacts )
158         {
159             String candidate = getArtifactFinalName( context, artifact );
160             if ( identifiers.contains( candidate ) )
161             {
162                 duplicates.add( candidate );
163             }
164             else
165             {
166                 identifiers.add( candidate );
167             }
168         }
169         return duplicates;
170     }
171 }