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