1 package org.apache.maven.shared.jar.identification.exposers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
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
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 }