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