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 | 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 | |
|
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 | |
} |