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.ant.tasks.support;
20  
21  import java.io.File;
22  import java.util.Arrays;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import org.apache.tools.ant.util.FileNameMapper;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  /**
30   * Ant filename mapper to remove version info from filename when copying dependencies.
31   *
32   * @author <a href="mailto:hboutemy@apache.org">Herve Boutemy</a>
33   */
34  public class VersionMapper implements FileNameMapper, Comparator<String> {
35      private List<String> versions;
36  
37      private String to;
38  
39      /** {@inheritDoc} */
40      @Override
41      public String[] mapFileName(String sourceFileName) {
42          String originalFileName = new File(sourceFileName).getName();
43          for (String version : versions) {
44              int index = originalFileName.indexOf(version);
45              if (index >= 0) {
46                  // remove version in artifactId-version(-classifier).type
47                  String baseFilename = originalFileName.substring(0, index - 1);
48                  String extension = originalFileName.substring(index + version.length());
49                  String path = sourceFileName.substring(0, sourceFileName.length() - originalFileName.length());
50                  if ("flatten".equals(to)) {
51                      path = "";
52                  }
53                  return new String[] {path + baseFilename + extension};
54              }
55          }
56          return new String[] {sourceFileName};
57      }
58  
59      /**
60       * Set the versions identifiers that this mapper can remove from filenames. The separator value used is path
61       * separator, as used by dependencies task when setting <code>versionsId</code> property value.
62       * @param from The string from which we set.
63       */
64      @Override
65      public void setFrom(String from) {
66          String[] split = StringUtils.split(from, File.pathSeparator);
67          // sort, from lengthiest to smallest
68          Arrays.sort(split, this);
69          versions = Arrays.asList(split);
70      }
71  
72      /**
73       * By default, only filename is changed, but if this attribute is set to <code>flatten</code>, directory is removed.
74       * @param to {@link #to}
75       */
76      @Override
77      public void setTo(String to) {
78          this.to = to;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public int compare(String s1, String s2) {
84          int lengthDiff = s2.length() - s1.length();
85          return (lengthDiff != 0) ? lengthDiff : s1.compareTo(s2);
86      }
87  }