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