Coverage Report - org.apache.maven.shared.jar.identification.exposers.EmbeddedMavenModelExposer
 
Classes in this File Line Coverage Branch Coverage Complexity
EmbeddedMavenModelExposer
0%
0/28
0%
0/8
8
 
 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  0
 public class EmbeddedMavenModelExposer
 45  
     extends AbstractLogEnabled
 46  
     implements JarIdentificationExposer
 47  
 {
 48  
     public void expose( JarIdentification identification, JarAnalyzer jarAnalyzer )
 49  
     {
 50  0
         List entries = jarAnalyzer.getMavenPomEntries();
 51  0
         if ( entries.isEmpty() )
 52  
         {
 53  0
             return;
 54  
         }
 55  
 
 56  0
         if ( entries.size() > 1 )
 57  
         {
 58  0
             getLogger().warn(
 59  
                 "More than one Maven model entry was found in the JAR, using only the first of: " + entries );
 60  
         }
 61  
 
 62  0
         JarEntry pom = (JarEntry) entries.get( 0 );
 63  0
         MavenXpp3Reader pomreader = new MavenXpp3Reader();
 64  0
         InputStream is = null;
 65  
         try
 66  
         {
 67  0
             is = jarAnalyzer.getEntryInputStream( pom );
 68  0
             InputStreamReader isreader = new InputStreamReader( is );
 69  0
             Model model = pomreader.read( isreader );
 70  
 
 71  0
             identification.addAndSetGroupId( model.getGroupId() );
 72  0
             identification.addAndSetArtifactId( model.getArtifactId() );
 73  0
             identification.addAndSetVersion( model.getVersion() );
 74  0
             identification.addAndSetName( model.getName() );
 75  
 
 76  
             // TODO: suboptimal - we are reproducing Maven's built in default
 77  0
             if ( model.getName() == null )
 78  
             {
 79  0
                 identification.addAndSetName( model.getArtifactId() );
 80  
             }
 81  
 
 82  0
             Organization org = model.getOrganization();
 83  0
             if ( org != null )
 84  
             {
 85  0
                 identification.addAndSetVendor( org.getName() );
 86  
             }
 87  
         }
 88  0
         catch ( IOException e )
 89  
         {
 90  0
             getLogger().error( "Unable to read model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e );
 91  
         }
 92  0
         catch ( XmlPullParserException e )
 93  
         {
 94  0
             getLogger().error( "Unable to parse model " + pom.getName() + " in " + jarAnalyzer.getFile() + ".", e );
 95  
         }
 96  
         finally
 97  
         {
 98  0
             IOUtil.close( is );
 99  0
         }
 100  0
     }
 101  
 }