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.io.IOException;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.util.List;
28  import java.util.jar.JarEntry;
29  
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.Organization;
32  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
33  import org.apache.maven.shared.jar.JarAnalyzer;
34  import org.apache.maven.shared.jar.identification.JarIdentification;
35  import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Exposer that examines a JAR file for any embedded Maven metadata for identification.
42   */
43  @Singleton
44  @Named("embeddedMavenModel")
45  public class EmbeddedMavenModelExposer implements JarIdentificationExposer {
46      private final Logger logger = LoggerFactory.getLogger(getClass());
47  
48      @Override
49      public void expose(JarIdentification identification, JarAnalyzer jarAnalyzer) {
50          List<JarEntry> entries = jarAnalyzer.getMavenPomEntries();
51          if (entries.isEmpty()) {
52              return;
53          }
54  
55          if (entries.size() > 1) {
56              logger.warn("More than one Maven model entry was found in the JAR, using only the first of: " + entries);
57          }
58  
59          JarEntry pom = entries.get(0);
60          MavenXpp3Reader pomreader = new MavenXpp3Reader();
61          try (InputStream is = jarAnalyzer.getEntryInputStream(pom);
62                  InputStreamReader isreader = new InputStreamReader(is)) {
63              Model model = pomreader.read(isreader);
64  
65              if (model.getParent() != null) {
66                  // use parent values only if project values not exists
67                  if (model.getGroupId() == null) {
68                      identification.addAndSetGroupId(model.getParent().getGroupId());
69                  }
70                  if (model.getVersion() == null) {
71                      identification.addAndSetVersion(model.getParent().getVersion());
72                  }
73              }
74  
75              identification.addAndSetGroupId(model.getGroupId());
76              identification.addAndSetArtifactId(model.getArtifactId());
77              identification.addAndSetVersion(model.getVersion());
78              identification.addAndSetName(model.getName());
79  
80              // TODO: suboptimal - we are reproducing Maven's built in default
81              if (model.getName() == null) {
82                  identification.addAndSetName(model.getArtifactId());
83              }
84  
85              Organization org = model.getOrganization();
86              if (org != null) {
87                  identification.addAndSetVendor(org.getName());
88              }
89          } catch (IOException e) {
90              logger.error("Unable to read model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e);
91          } catch (XmlPullParserException e) {
92              logger.error("Unable to parse model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e);
93          }
94      }
95  }