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.shared.jar.identification.exposers;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import org.apache.maven.shared.jar.JarAnalyzer;
28  import org.apache.maven.shared.jar.identification.JarIdentification;
29  import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Exposer that examines a JAR file to derive Maven metadata from the pattern of the JAR's filename.
34   * Will match the format <i>artifactId</i>-<i>version</i>.jar.
35   */
36  @Singleton
37  @Named("filename")
38  public class FilenameExposer implements JarIdentificationExposer {
39      private static final Pattern VERSION_PATTERN = Pattern.compile("-\\d");
40  
41      @Override
42      public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
43          String filename = FileUtils.removeExtension(jarAnalyzer.getFile().getName());
44          Matcher mat = VERSION_PATTERN.matcher(filename);
45          if (mat.find()) {
46              String prefix = filename.substring(0, mat.start());
47              identification.addArtifactId(prefix);
48              identification.addName(prefix);
49              identification.addVersion(filename.substring(mat.end() - 1));
50          } else {
51              identification.addArtifactId(filename);
52              identification.addName(filename);
53          }
54      }
55  }