1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.codehaus.mojo.nbm; |
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.FileReader; |
21 | |
import java.io.IOException; |
22 | |
import java.io.Reader; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Arrays; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import org.apache.maven.artifact.Artifact; |
28 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
29 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
30 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
31 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
32 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
33 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
34 | |
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; |
35 | |
import org.apache.maven.plugin.MojoExecutionException; |
36 | |
import org.apache.maven.plugin.logging.Log; |
37 | |
import org.apache.maven.project.MavenProject; |
38 | |
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder; |
39 | |
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException; |
40 | |
import org.apache.maven.shared.dependency.graph.DependencyNode; |
41 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
42 | |
import org.codehaus.mojo.nbm.model.Dependency; |
43 | |
import org.codehaus.mojo.nbm.model.NetBeansModule; |
44 | |
import org.codehaus.mojo.nbm.model.io.xpp3.NetBeansModuleXpp3Reader; |
45 | |
import org.codehaus.mojo.nbm.utils.ExamineManifest; |
46 | |
import org.codehaus.plexus.util.IOUtil; |
47 | |
|
48 | 0 | public abstract class AbstractNbmMojo |
49 | |
extends org.codehaus.mojo.nbm.utils.AbstractNetbeansMojo |
50 | |
{ |
51 | |
|
52 | |
static boolean matchesLibrary( Artifact artifact, List<String> libraries, ExamineManifest depExaminator, |
53 | |
Log log, boolean useOsgiDependencies ) |
54 | |
{ |
55 | 0 | String artId = artifact.getArtifactId(); |
56 | 0 | String grId = artifact.getGroupId(); |
57 | 0 | String id = grId + ":" + artId; |
58 | 0 | boolean explicit = libraries.remove( id ); |
59 | 0 | if ( explicit ) |
60 | |
{ |
61 | 0 | log.debug( |
62 | |
id + " included as module library, explicitly declared in module descriptor." ); |
63 | 0 | return explicit; |
64 | |
} |
65 | 0 | if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) || Artifact.SCOPE_SYSTEM.equals( |
66 | |
artifact.getScope() ) ) |
67 | |
{ |
68 | 0 | log.debug( |
69 | |
id + " omitted as module library, has scope 'provided/system'" ); |
70 | 0 | return false; |
71 | |
} |
72 | 0 | if ( "nbm".equals( artifact.getType() ) ) |
73 | |
{ |
74 | 0 | return false; |
75 | |
} |
76 | 0 | if ( depExaminator.isNetBeansModule() || ( useOsgiDependencies && depExaminator.isOsgiBundle() ) ) |
77 | |
{ |
78 | |
|
79 | |
|
80 | 0 | return false; |
81 | |
} |
82 | 0 | log.debug( |
83 | |
id + " included as module library, squeezed through all the filters." ); |
84 | 0 | return true; |
85 | |
} |
86 | |
|
87 | |
static Dependency resolveNetBeansDependency( Artifact artifact, List<Dependency> deps, |
88 | |
ExamineManifest manifest, Log log ) |
89 | |
{ |
90 | 0 | String artId = artifact.getArtifactId(); |
91 | 0 | String grId = artifact.getGroupId(); |
92 | 0 | String id = grId + ":" + artId; |
93 | 0 | for ( Dependency dep : deps ) |
94 | |
{ |
95 | 0 | if ( id.equals( dep.getId() ) ) |
96 | |
{ |
97 | 0 | if ( manifest.isNetBeansModule() ) |
98 | |
{ |
99 | 0 | return dep; |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | if ( dep.getExplicitValue() != null ) |
104 | |
{ |
105 | 0 | return dep; |
106 | |
} |
107 | 0 | log.warn( |
108 | |
id + " declared as module dependency in descriptor, but not a NetBeans module" ); |
109 | 0 | return null; |
110 | |
} |
111 | |
} |
112 | 0 | } |
113 | 0 | if ( "nbm".equals( artifact.getType() ) ) |
114 | |
{ |
115 | 0 | Dependency dep = new Dependency(); |
116 | 0 | dep.setId( id ); |
117 | 0 | dep.setType( "spec" ); |
118 | 0 | log.debug( "Adding nbm module dependency - " + id ); |
119 | 0 | return dep; |
120 | |
} |
121 | 0 | if ( manifest.isNetBeansModule() ) |
122 | |
{ |
123 | 0 | Dependency dep = new Dependency(); |
124 | 0 | dep.setId( id ); |
125 | 0 | dep.setType( "spec" ); |
126 | 0 | log.debug( "Adding direct NetBeans module dependency - " + id ); |
127 | 0 | return dep; |
128 | |
} |
129 | 0 | return null; |
130 | |
} |
131 | |
|
132 | |
protected final NetBeansModule readModuleDescriptor( File descriptor ) |
133 | |
throws MojoExecutionException |
134 | |
{ |
135 | 0 | if ( descriptor == null ) |
136 | |
{ |
137 | 0 | throw new MojoExecutionException( |
138 | |
"The module descriptor has to be configured." ); |
139 | |
} |
140 | 0 | if ( !descriptor.exists() ) |
141 | |
{ |
142 | 0 | throw new MojoExecutionException( |
143 | |
"The module descriptor is missing: '" + descriptor + "'." ); |
144 | |
} |
145 | 0 | Reader r = null; |
146 | |
try |
147 | |
{ |
148 | 0 | r = new FileReader( descriptor ); |
149 | 0 | NetBeansModuleXpp3Reader reader = new NetBeansModuleXpp3Reader(); |
150 | 0 | NetBeansModule module = reader.read( r ); |
151 | 0 | return module; |
152 | |
} |
153 | 0 | catch ( IOException exc ) |
154 | |
{ |
155 | 0 | throw new MojoExecutionException( |
156 | |
"Error while reading module descriptor '" + descriptor + "'.", |
157 | |
exc ); |
158 | |
} |
159 | 0 | catch ( XmlPullParserException xml ) |
160 | |
{ |
161 | 0 | throw new MojoExecutionException( |
162 | |
"Error while reading module descriptor '" + descriptor + "'.", |
163 | |
xml ); |
164 | |
} |
165 | |
finally |
166 | |
{ |
167 | 0 | IOUtil.close( r ); |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
protected final NetBeansModule createDefaultDescriptor( MavenProject project, boolean log ) |
172 | |
{ |
173 | |
|
174 | 0 | if ( log ) |
175 | |
{ |
176 | 0 | getLog().info( |
177 | |
"No Module Descriptor defined, trying to fallback to generated values:" ); |
178 | |
} |
179 | 0 | NetBeansModule module = new NetBeansModule(); |
180 | 0 | return module; |
181 | |
} |
182 | |
|
183 | |
static List<Artifact> getLibraryArtifacts( DependencyNode treeRoot, NetBeansModule module, |
184 | |
List<Artifact> runtimeArtifacts, |
185 | |
Map<Artifact, ExamineManifest> examinerCache, Log log, |
186 | |
boolean useOsgiDependencies ) |
187 | |
throws MojoExecutionException |
188 | |
{ |
189 | 0 | List<Artifact> include = new ArrayList<Artifact>(); |
190 | 0 | if ( module != null ) |
191 | |
{ |
192 | 0 | List<String> librList = new ArrayList<String>(); |
193 | 0 | if ( module.getLibraries() != null ) |
194 | |
{ |
195 | 0 | librList.addAll( module.getLibraries() ); |
196 | |
} |
197 | 0 | CollectLibrariesNodeVisitor visitor = new CollectLibrariesNodeVisitor( librList, |
198 | |
runtimeArtifacts, examinerCache, log, treeRoot, useOsgiDependencies ); |
199 | 0 | treeRoot.accept( visitor ); |
200 | 0 | include.addAll( visitor.getArtifacts() ); |
201 | |
} |
202 | 0 | return include; |
203 | |
} |
204 | |
|
205 | |
static List<ModuleWrapper> getModuleDependencyArtifacts( DependencyNode treeRoot, NetBeansModule module, |
206 | |
Dependency[] customDependencies, MavenProject project, |
207 | |
Map<Artifact, ExamineManifest> examinerCache, |
208 | |
List<Artifact> libraryArtifacts, Log log, |
209 | |
boolean useOsgiDependencies ) |
210 | |
throws MojoExecutionException |
211 | |
{ |
212 | 0 | List<Dependency> deps = new ArrayList<Dependency>(); |
213 | 0 | if (customDependencies != null) { |
214 | 0 | deps.addAll( Arrays.asList( customDependencies )); |
215 | |
} |
216 | 0 | if (module != null && !module.getDependencies().isEmpty()) { |
217 | 0 | log.warn( "dependencies in module descriptor are deprecated, use the plugin's parameter moduleDependencies"); |
218 | |
|
219 | |
|
220 | 0 | for (Dependency d : module.getDependencies()) { |
221 | 0 | Dependency found = null; |
222 | 0 | for (Dependency d2 : deps) { |
223 | 0 | if (d2.getId().equals(d.getId())) { |
224 | 0 | found = d2; |
225 | 0 | break; |
226 | |
} |
227 | 0 | } |
228 | 0 | if (found != null) { |
229 | 0 | deps.remove( found ); |
230 | |
} |
231 | 0 | deps.add(d); |
232 | 0 | } |
233 | |
} |
234 | 0 | List<ModuleWrapper> include = new ArrayList<ModuleWrapper>(); |
235 | |
|
236 | |
@SuppressWarnings( "unchecked" ) |
237 | 0 | List<Artifact> artifacts = project.getCompileArtifacts(); |
238 | 0 | for ( Artifact artifact : artifacts ) |
239 | |
{ |
240 | 0 | if ( libraryArtifacts.contains( artifact ) ) |
241 | |
{ |
242 | 0 | continue; |
243 | |
} |
244 | 0 | ExamineManifest depExaminator = examinerCache.get( artifact ); |
245 | 0 | if ( depExaminator == null ) |
246 | |
{ |
247 | 0 | depExaminator = new ExamineManifest( log ); |
248 | 0 | depExaminator.setArtifactFile( artifact.getFile() ); |
249 | 0 | depExaminator.checkFile(); |
250 | 0 | examinerCache.put( artifact, depExaminator ); |
251 | |
} |
252 | 0 | Dependency dep = resolveNetBeansDependency( artifact, deps, depExaminator, log ); |
253 | 0 | if ( dep != null ) |
254 | |
{ |
255 | 0 | ModuleWrapper wr = new ModuleWrapper(); |
256 | 0 | wr.dependency = dep; |
257 | 0 | wr.artifact = artifact; |
258 | 0 | wr.transitive = false; |
259 | |
|
260 | 0 | if ( depExaminator.isNetBeansModule() && artifact.getDependencyTrail().size() > 2 ) |
261 | |
{ |
262 | 0 | log.debug( |
263 | |
artifact.getId() + " omitted as NetBeans module dependency, not a direct one. Declare it in the pom for inclusion." ); |
264 | 0 | wr.transitive = true; |
265 | |
|
266 | |
} |
267 | 0 | include.add( wr ); |
268 | 0 | } |
269 | |
else |
270 | |
{ |
271 | 0 | if ( useOsgiDependencies && depExaminator.isOsgiBundle() ) |
272 | |
{ |
273 | 0 | ModuleWrapper wr = new ModuleWrapper(); |
274 | 0 | wr.osgi = true; |
275 | 0 | String id = artifact.getGroupId() + ":" + artifact.getArtifactId(); |
276 | 0 | for ( Dependency depe : deps ) |
277 | |
{ |
278 | 0 | if ( id.equals( depe.getId() ) ) |
279 | |
{ |
280 | 0 | wr.dependency = depe; |
281 | |
} |
282 | 0 | } |
283 | 0 | boolean print = false; |
284 | 0 | if ( wr.dependency == null ) |
285 | |
{ |
286 | 0 | Dependency depe = new Dependency(); |
287 | 0 | depe.setId( id ); |
288 | 0 | depe.setType( "spec" ); |
289 | 0 | wr.dependency = depe; |
290 | 0 | print = true; |
291 | |
} |
292 | |
|
293 | 0 | wr.artifact = artifact; |
294 | 0 | wr.transitive = false; |
295 | |
|
296 | 0 | if ( artifact.getDependencyTrail().size() > 2 ) |
297 | |
{ |
298 | 0 | log.debug( |
299 | |
artifact.getId() + " omitted as NetBeans module OSGi dependency, not a direct one. Declare it in the pom for inclusion." ); |
300 | 0 | wr.transitive = true; |
301 | |
|
302 | |
} |
303 | |
else |
304 | |
{ |
305 | 0 | if ( print ) |
306 | |
{ |
307 | 0 | log.info( "Adding OSGi bundle dependency - " + id ); |
308 | |
} |
309 | |
} |
310 | |
|
311 | 0 | include.add( wr ); |
312 | |
} |
313 | |
} |
314 | 0 | } |
315 | 0 | return include; |
316 | |
} |
317 | |
|
318 | 0 | static class ModuleWrapper |
319 | |
{ |
320 | |
|
321 | |
Dependency dependency; |
322 | |
|
323 | |
Artifact artifact; |
324 | |
|
325 | 0 | boolean transitive = true; |
326 | |
|
327 | 0 | boolean osgi = false; |
328 | |
|
329 | |
} |
330 | |
|
331 | |
|
332 | |
protected DependencyNode createDependencyTree( MavenProject project, DependencyGraphBuilder dependencyGraphBuilder, |
333 | |
String scope ) |
334 | |
throws MojoExecutionException |
335 | |
{ |
336 | 0 | ArtifactFilter artifactFilter = createResolvingArtifactFilter( scope ); |
337 | |
try |
338 | |
{ |
339 | 0 | return dependencyGraphBuilder.buildDependencyGraph( project, artifactFilter ); |
340 | |
} |
341 | 0 | catch ( DependencyGraphBuilderException exception ) |
342 | |
{ |
343 | 0 | throw new MojoExecutionException( "Cannot build project dependency tree", exception ); |
344 | |
} |
345 | |
|
346 | |
} |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
private ArtifactFilter createResolvingArtifactFilter( String scope ) |
355 | |
{ |
356 | |
ArtifactFilter filter; |
357 | |
|
358 | |
|
359 | 0 | if ( scope != null ) |
360 | |
{ |
361 | 0 | getLog().debug( "+ Resolving dependency tree for scope '" + scope + "'" ); |
362 | |
|
363 | 0 | filter = new ScopeArtifactFilter( scope ); |
364 | |
} |
365 | |
else |
366 | |
{ |
367 | 0 | filter = null; |
368 | |
} |
369 | |
|
370 | 0 | return filter; |
371 | |
} |
372 | |
|
373 | |
protected final ArtifactResult turnJarToNbmFile( Artifact art, ArtifactFactory artifactFactory, |
374 | |
ArtifactResolver artifactResolver, MavenProject project, |
375 | |
ArtifactRepository localRepository ) |
376 | |
throws MojoExecutionException |
377 | |
{ |
378 | 0 | if ( "jar".equals( art.getType() ) || "nbm".equals( art.getType() ) ) |
379 | |
{ |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | 0 | ExamineManifest mnf = new ExamineManifest( getLog() ); |
385 | 0 | File jar = art.getFile(); |
386 | 0 | if ( !jar.isFile() ) |
387 | |
{ |
388 | |
|
389 | |
|
390 | |
|
391 | 0 | String path = localRepository.pathOf( art ); |
392 | 0 | File jar2 = new File(localRepository.getBasedir(), path.replace( "/", File.separator)); |
393 | 0 | File manifest = new File(jar, "META-INF/MANIFEST.MF" ); |
394 | |
|
395 | 0 | if (! jar2.isFile() || !manifest.isFile() ) { |
396 | 0 | getLog().warn( "MNBMODULE-131: need to at least run install phase on " + jar2 ); |
397 | 0 | return new ArtifactResult( null, null ); |
398 | |
} |
399 | 0 | mnf.setManifestFile( manifest ); |
400 | 0 | } else { |
401 | 0 | mnf.setJarFile( jar ); |
402 | |
} |
403 | 0 | mnf.checkFile(); |
404 | 0 | if ( mnf.isNetBeansModule() ) |
405 | |
{ |
406 | 0 | Artifact nbmArt = artifactFactory.createDependencyArtifact( |
407 | |
art.getGroupId(), |
408 | |
art.getArtifactId(), |
409 | |
art.getVersionRange(), |
410 | |
"nbm-file", |
411 | |
art.getClassifier(), |
412 | |
art.getScope() ); |
413 | |
try |
414 | |
{ |
415 | 0 | artifactResolver.resolve( nbmArt, project.getRemoteArtifactRepositories(), localRepository ); |
416 | |
} |
417 | |
|
418 | 0 | catch ( ArtifactResolutionException ex ) |
419 | |
{ |
420 | |
|
421 | 0 | checkReactor( art, nbmArt ); |
422 | 0 | if ( !nbmArt.isResolved() ) |
423 | |
{ |
424 | 0 | throw new MojoExecutionException( "Failed to retrieve the nbm file from repository", ex ); |
425 | |
} |
426 | |
} |
427 | 0 | catch ( ArtifactNotFoundException ex ) |
428 | |
{ |
429 | |
|
430 | 0 | checkReactor( art, nbmArt ); |
431 | 0 | if ( !nbmArt.isResolved() ) |
432 | |
{ |
433 | 0 | throw new MojoExecutionException( "Failed to retrieve the nbm file from repository", ex ); |
434 | |
} |
435 | 0 | } |
436 | 0 | return new ArtifactResult( nbmArt, mnf ); |
437 | |
} |
438 | 0 | if ( mnf.isOsgiBundle() ) |
439 | |
{ |
440 | 0 | return new ArtifactResult( null, mnf ); |
441 | |
} |
442 | |
} |
443 | 0 | return new ArtifactResult( null, null ); |
444 | |
} |
445 | |
|
446 | 0 | protected static final class ArtifactResult |
447 | |
{ |
448 | |
private final Artifact converted; |
449 | |
private final ExamineManifest manifest; |
450 | |
|
451 | |
ArtifactResult( Artifact conv, ExamineManifest manifest ) |
452 | 0 | { |
453 | 0 | converted = conv; |
454 | 0 | this.manifest = manifest; |
455 | 0 | } |
456 | |
|
457 | |
boolean hasConvertedArtifact() |
458 | |
{ |
459 | 0 | return converted != null; |
460 | |
} |
461 | |
|
462 | |
Artifact getConvertedArtifact() |
463 | |
{ |
464 | 0 | return converted; |
465 | |
} |
466 | |
|
467 | |
public boolean isOSGiBundle() |
468 | |
{ |
469 | 0 | return manifest != null && manifest.isOsgiBundle(); |
470 | |
} |
471 | |
|
472 | |
public ExamineManifest getExaminedManifest() |
473 | |
{ |
474 | 0 | return manifest; |
475 | |
} |
476 | |
} |
477 | |
|
478 | |
private void checkReactor( Artifact art, Artifact nbmArt ) |
479 | |
{ |
480 | 0 | if ( art.getFile().getName().endsWith( ".jar" ) ) |
481 | |
{ |
482 | 0 | String name = art.getFile().getName(); |
483 | 0 | name = name.substring( 0, name.length() - ".jar".length() ) + ".nbm"; |
484 | 0 | File fl = new File( art.getFile().getParentFile(), name ); |
485 | 0 | if ( fl.exists() ) |
486 | |
{ |
487 | 0 | nbmArt.setFile( fl ); |
488 | 0 | nbmArt.setResolved( true ); |
489 | |
} |
490 | |
} |
491 | 0 | } |
492 | |
|
493 | |
} |