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