1 package org.apache.maven.shared.release.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.maven.model.Model;
26 import org.apache.maven.model.Parent;
27 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
28 import org.codehaus.plexus.logging.Logger;
29 import org.codehaus.plexus.util.ReaderFactory;
30 import org.codehaus.plexus.util.xml.XmlStreamReader;
31 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class PomFinder
63 {
64
65 private Logger log;
66 private PomInfo foundPomInfo;
67
68 public PomFinder( Logger log )
69 {
70 this.log = log;
71 }
72
73
74
75
76
77
78 public boolean parsePom( File originPom )
79 {
80 if ( !originPom.exists() )
81 {
82 return false;
83 }
84
85 try
86 {
87 foundPomInfo = readPomInfo( originPom );
88 }
89 catch ( Exception e )
90 {
91 log.warn( "Error while parsing pom file", e );
92 return false;
93 }
94
95 return foundPomInfo != null;
96 }
97
98
99
100
101
102
103
104
105 public File findMatchingPom( File startDirectory )
106 {
107 if ( !startDirectory.exists() )
108 {
109 return null;
110 }
111
112 if ( !startDirectory.isDirectory() )
113 {
114 log.error( "PomFinder must be started with a directory! Got " + startDirectory.getAbsolutePath() );
115 return null;
116 }
117
118 if ( foundPomInfo == null )
119 {
120 log.error( "Please run parsePom first!" );
121 return null;
122 }
123
124
125 File matchingPom = new File( startDirectory, foundPomInfo.getFileName() );
126 if ( matchingPom.exists() )
127 {
128 PomInfo pi = null;
129 try
130 {
131 pi = readPomInfo( matchingPom );
132 }
133 catch ( Exception e )
134 {
135 log.warn( "Error while parsing pom file", e );
136
137
138
139 }
140
141 if ( pi == null || !pi.equals( foundPomInfo ) )
142 {
143 matchingPom = null;
144 }
145 }
146 else
147 {
148 matchingPom = null;
149 }
150
151 if ( matchingPom == null )
152 {
153 String[] childFiles = startDirectory.list();
154 for ( int i = 0; i < childFiles.length; i++ )
155 {
156 String childFile = childFiles[ i ];
157
158 File subDir = new File( startDirectory, childFile );
159 if ( subDir.isDirectory() && !subDir.isHidden() )
160 {
161 matchingPom = findMatchingPom( subDir );
162 }
163
164 if ( matchingPom != null )
165 {
166 break;
167 }
168 }
169 }
170
171 return matchingPom;
172 }
173
174
175
176
177
178
179
180 private PomInfo readPomInfo( File pomFile )
181 throws IOException, XmlPullParserException
182 {
183 if ( !pomFile.exists() || !pomFile.isFile() )
184 {
185 return null;
186 }
187
188 PomInfo pomInfo = null;
189
190 MavenXpp3Reader reader = new MavenXpp3Reader();
191
192 Model model = null;
193 try ( XmlStreamReader xmlReader = ReaderFactory.newXmlReader( pomFile ) )
194 {
195 model = reader.read( xmlReader );
196 }
197
198 if ( model != null )
199 {
200 pomInfo = new PomInfo();
201 pomInfo.setArtifactId( model.getArtifactId() );
202 pomInfo.setGroupId( model.getGroupId() );
203
204 Parent parent = model.getParent();
205 if ( parent != null )
206 {
207 pomInfo.setParentArtifactId( parent.getArtifactId() );
208 pomInfo.setParentGroupId( parent.getGroupId() );
209 }
210
211 pomInfo.setFileName( pomFile.getName() );
212 }
213 return pomInfo;
214 }
215
216
217
218
219
220 private static class PomInfo
221 {
222 private String fileName;
223 private String artifactId;
224 private String groupId;
225 private String parentArtifactId;
226 private String parentGroupId;
227
228 public String getFileName()
229 {
230 return fileName;
231 }
232
233 public void setFileName( String fileName )
234 {
235 this.fileName = fileName;
236 }
237
238 public void setArtifactId( String artifactId )
239 {
240 this.artifactId = artifactId;
241 }
242
243 public void setGroupId( String groupId )
244 {
245 this.groupId = groupId;
246 }
247
248 public void setParentArtifactId( String parentArtifactId )
249 {
250 this.parentArtifactId = parentArtifactId;
251 }
252
253 public void setParentGroupId( String parentGroupId )
254 {
255 this.parentGroupId = parentGroupId;
256 }
257
258 @Override
259 public boolean equals( Object o )
260 {
261 if ( this == o )
262 {
263 return true;
264 }
265
266 if ( o == null || getClass() != o.getClass() )
267 {
268 return false;
269 }
270
271 PomInfo pomInfo = (PomInfo) o;
272
273 if ( artifactId != null ? !artifactId.equals( pomInfo.artifactId ) : pomInfo.artifactId != null )
274 {
275 return false;
276 }
277 if ( groupId != null ? !groupId.equals( pomInfo.groupId ) : pomInfo.groupId != null )
278 {
279 return false;
280 }
281 if ( parentArtifactId != null ? !parentArtifactId.equals( pomInfo.parentArtifactId )
282 : pomInfo.parentArtifactId != null )
283 {
284 return false;
285 }
286 if ( parentGroupId != null ? !parentGroupId.equals( pomInfo.parentGroupId )
287 : pomInfo.parentGroupId != null )
288 {
289 return false;
290 }
291
292 return true;
293 }
294
295 @Override
296 public int hashCode()
297 {
298 int result = artifactId != null ? artifactId.hashCode() : 0;
299 result = 31 * result + ( groupId != null ? groupId.hashCode() : 0 );
300 result = 31 * result + ( parentArtifactId != null ? parentArtifactId.hashCode() : 0 );
301 result = 31 * result + ( parentGroupId != null ? parentGroupId.hashCode() : 0 );
302 return result;
303 }
304 }
305 }