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.index.locator;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  
26  import org.apache.maven.index.artifact.ArtifactPackagingMapper;
27  import org.apache.maven.index.artifact.Gav;
28  import org.apache.maven.index.artifact.GavCalculator;
29  import org.apache.maven.model.Model;
30  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
31  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Artifact locator.
37   *
38   * @author Damian Bradicich
39   */
40  public class ArtifactLocator implements GavHelpedLocator {
41  
42      private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactLocator.class);
43  
44      private final ArtifactPackagingMapper mapper;
45  
46      public ArtifactLocator(ArtifactPackagingMapper mapper) {
47          this.mapper = mapper;
48      }
49  
50      public File locate(File source, GavCalculator gavCalculator, Gav gav) {
51          // if we don't have this data, nothing we can do
52          if (source == null //
53                  || !source.exists() //
54                  || gav == null //
55                  || gav.getArtifactId() == null //
56                  || gav.getVersion() == null) {
57              return null;
58          }
59  
60          try (InputStream inputStream = Files.newInputStream(source.toPath())) {
61              // need to read the pom model to get packaging
62              final Model model = new MavenXpp3Reader().read(inputStream, false);
63  
64              if (model == null) {
65                  return null;
66              }
67  
68              // now generate the artifactname
69              String artifactName = gav.getArtifactId() + "-" + gav.getVersion() + "."
70                      + mapper.getExtensionForPackaging(model.getPackaging());
71  
72              File artifact = new File(source.getParent(), artifactName);
73  
74              if (!artifact.exists()) {
75                  return null;
76              }
77  
78              return artifact;
79          } catch (XmlPullParserException | IOException e) {
80              LOGGER.warn("skip error reading pom from file:" + source, e);
81              return null;
82          }
83      }
84  }