View Javadoc

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