KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > MavenProject


1 package org.apache.maven.project;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.ArtifactUtils;
21 import org.apache.maven.artifact.DependencyResolutionRequiredException;
22 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
25 import org.apache.maven.model.Build;
26 import org.apache.maven.model.CiManagement;
27 import org.apache.maven.model.Contributor;
28 import org.apache.maven.model.Dependency;
29 import org.apache.maven.model.DependencyManagement;
30 import org.apache.maven.model.Developer;
31 import org.apache.maven.model.DistributionManagement;
32 import org.apache.maven.model.IssueManagement;
33 import org.apache.maven.model.License;
34 import org.apache.maven.model.MailingList;
35 import org.apache.maven.model.Model;
36 import org.apache.maven.model.Organization;
37 import org.apache.maven.model.Plugin;
38 import org.apache.maven.model.PluginExecution;
39 import org.apache.maven.model.PluginManagement;
40 import org.apache.maven.model.Prerequisites;
41 import org.apache.maven.model.ReportPlugin;
42 import org.apache.maven.model.ReportSet;
43 import org.apache.maven.model.Reporting;
44 import org.apache.maven.model.Resource;
45 import org.apache.maven.model.Scm;
46 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
47 import org.apache.maven.project.artifact.ActiveProjectArtifact;
48 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
49 import org.apache.maven.project.artifact.MavenMetadataSource;
50 import org.apache.maven.project.overlay.BuildOverlay;
51 import org.codehaus.plexus.util.xml.Xpp3Dom;
52
53 import java.io.File JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.Writer JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Collections JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62 import java.util.Properties JavaDoc;
63 import java.util.Set JavaDoc;
64
65 /**
66  * The concern of the project is provide runtime values based on the model. <p/>
67  * The values in the model remain untouched but during the process of building a
68  * project notions like inheritance and interpolation can be added. This allows
69  * to have an entity which is useful in a runtime while preserving the model so
70  * that it can be marshalled and unmarshalled without being tainted by runtime
71  * requirements. <p/>We need to leave the model intact because we don't want
72  * the following:
73  * <ol>
74  * <li>We don't want interpolated values being written back into the model.
75  * <li>We don't want inherited values being written back into the model.
76  * </ol>
77  */

78 public class MavenProject
79 {
80     public static final String JavaDoc EMPTY_PROJECT_GROUP_ID = "unknown";
81     
82     public static final String JavaDoc EMPTY_PROJECT_ARTIFACT_ID = "empty-project";
83     
84     public static final String JavaDoc EMPTY_PROJECT_VERSION = "0";
85     
86     private Model model;
87
88     private MavenProject parent;
89
90     private File JavaDoc file;
91
92     private Set artifacts;
93
94     private Artifact parentArtifact;
95
96     private Set pluginArtifacts;
97
98     private List remoteArtifactRepositories;
99
100     private List collectedProjects = Collections.EMPTY_LIST;
101
102     private List attachedArtifacts;
103
104     private MavenProject executionProject;
105
106     private List compileSourceRoots = new ArrayList JavaDoc();
107
108     private List testCompileSourceRoots = new ArrayList JavaDoc();
109
110     private List scriptSourceRoots = new ArrayList JavaDoc();
111
112     private List pluginArtifactRepositories;
113
114     private ArtifactRepository releaseArtifactRepository;
115
116     private ArtifactRepository snapshotArtifactRepository;
117
118     private List activeProfiles = new ArrayList JavaDoc();
119
120     private Set dependencyArtifacts;
121
122     private Artifact artifact;
123
124     // calculated.
125
private Map JavaDoc artifactMap;
126
127     private Model originalModel;
128
129     private Map JavaDoc pluginArtifactMap;
130
131     private Set reportArtifacts;
132
133     private Map JavaDoc reportArtifactMap;
134
135     private Set extensionArtifacts;
136
137     private Map JavaDoc extensionArtifactMap;
138
139     private Map JavaDoc projectReferences = new HashMap JavaDoc();
140
141     private Build buildOverlay;
142
143     private boolean executionRoot;
144     
145     private Map JavaDoc moduleAdjustments;
146
147     public MavenProject()
148     {
149         Model model = new Model();
150         
151         model.setGroupId( EMPTY_PROJECT_GROUP_ID );
152         model.setArtifactId( EMPTY_PROJECT_ARTIFACT_ID );
153         model.setVersion( EMPTY_PROJECT_VERSION );
154         
155         this.model = model;
156     }
157
158     public MavenProject( Model model )
159     {
160         this.model = model;
161     }
162
163     public MavenProject( MavenProject project )
164     {
165         // disown the parent
166

167         // copy fields
168
this.file = project.file;
169
170         // don't need a deep copy, they don't get modified or added/removed to/from - but make them unmodifiable to be sure!
171
if ( project.dependencyArtifacts != null )
172         {
173             this.dependencyArtifacts = Collections.unmodifiableSet( project.dependencyArtifacts );
174         }
175         
176         if ( project.artifacts != null )
177         {
178             this.artifacts = Collections.unmodifiableSet( project.artifacts );
179         }
180         
181         if ( project.pluginArtifacts != null )
182         {
183             this.pluginArtifacts = Collections.unmodifiableSet( project.pluginArtifacts );
184         }
185         
186         if ( project.reportArtifacts != null )
187         {
188             this.reportArtifacts = Collections.unmodifiableSet( project.reportArtifacts );
189         }
190         
191         if ( project.extensionArtifacts != null )
192         {
193             this.extensionArtifacts = Collections.unmodifiableSet( project.extensionArtifacts );
194         }
195         
196         this.parentArtifact = project.parentArtifact;
197
198         if ( project.remoteArtifactRepositories != null )
199         {
200             this.remoteArtifactRepositories = Collections.unmodifiableList( project.remoteArtifactRepositories );
201         }
202         
203         if ( project.pluginArtifactRepositories != null )
204         {
205             this.pluginArtifactRepositories = Collections.unmodifiableList( project.pluginArtifactRepositories );
206         }
207         
208         if ( project.collectedProjects != null )
209         {
210             this.collectedProjects = Collections.unmodifiableList( project.collectedProjects );
211         }
212         
213         if ( project.activeProfiles != null )
214         {
215             this.activeProfiles = Collections.unmodifiableList( project.activeProfiles );
216         }
217         
218         if ( project.getAttachedArtifacts() != null )
219         {
220             // clone properties modifyable by plugins in a forked lifecycle
221
this.attachedArtifacts = new ArrayList JavaDoc( project.getAttachedArtifacts() );
222         }
223         
224         if ( project.compileSourceRoots != null )
225         {
226             // clone source roots
227
this.compileSourceRoots = new ArrayList JavaDoc( project.compileSourceRoots );
228         }
229         
230         if ( project.testCompileSourceRoots != null )
231         {
232             this.testCompileSourceRoots = new ArrayList JavaDoc( project.testCompileSourceRoots );
233         }
234         
235         if ( project.scriptSourceRoots != null )
236         {
237             this.scriptSourceRoots = new ArrayList JavaDoc( project.scriptSourceRoots );
238         }
239         
240         this.model = ModelUtils.cloneModel( project.model );
241
242         if ( project.originalModel != null )
243         {
244             this.originalModel = ModelUtils.cloneModel( project.originalModel );
245         }
246
247         this.executionRoot = project.executionRoot;
248
249         if ( project.artifact != null )
250         {
251             this.artifact = ArtifactUtils.copyArtifact( project.artifact );
252         }
253     }
254     
255     public String JavaDoc getModulePathAdjustment( MavenProject moduleProject ) throws IOException JavaDoc
256     {
257         // FIXME: This is hacky. What if module directory doesn't match artifactid, and parent
258
// is coming from the repository??
259
String JavaDoc module = moduleProject.getArtifactId();
260         
261         File JavaDoc moduleFile = moduleProject.getFile();
262         
263         if ( moduleFile != null )
264         {
265             File JavaDoc moduleDir = moduleFile.getCanonicalFile().getParentFile();
266             
267             module = moduleDir.getName();
268         }
269         
270         if ( moduleAdjustments == null )
271         {
272             moduleAdjustments = new HashMap JavaDoc();
273             
274             List modules = getModules();
275             if ( modules != null )
276             {
277                 for ( Iterator JavaDoc it = modules.iterator(); it.hasNext(); )
278                 {
279                     String JavaDoc modulePath = (String JavaDoc) it.next();
280                     String JavaDoc moduleName = modulePath;
281                     
282                     if ( moduleName.endsWith( "/" ) || moduleName.endsWith( "\\" ) )
283                     {
284                         moduleName = moduleName.substring( 0, moduleName.length() - 1 );
285                     }
286                     
287                     int lastSlash = moduleName.lastIndexOf( '/' );
288                     
289                     if ( lastSlash < 0 )
290                     {
291                         lastSlash = moduleName.lastIndexOf( '\\' );
292                     }
293                     
294                     String JavaDoc adjustment = null;
295                     
296                     if ( lastSlash > -1 )
297                     {
298                         moduleName = moduleName.substring( lastSlash + 1 );
299                         adjustment = modulePath.substring( 0, lastSlash );
300                     }
301
302                     moduleAdjustments.put( moduleName, adjustment );
303                 }
304             }
305         }
306         
307         return (String JavaDoc) moduleAdjustments.get( module );
308     }
309
310     // ----------------------------------------------------------------------
311
// Accessors
312
// ----------------------------------------------------------------------
313

314     public Artifact getArtifact()
315     {
316         return artifact;
317     }
318
319     public void setArtifact( Artifact artifact )
320     {
321         this.artifact = artifact;
322     }
323
324     //@todo I would like to get rid of this. jvz.
325
public Model getModel()
326     {
327         return model;
328     }
329
330     public MavenProject getParent()
331     {
332         return parent;
333     }
334
335     public void setParent( MavenProject parent )
336     {
337         this.parent = parent;
338     }
339
340     public void setRemoteArtifactRepositories( List remoteArtifactRepositories )
341     {
342         this.remoteArtifactRepositories = remoteArtifactRepositories;
343     }
344
345     public List getRemoteArtifactRepositories()
346     {
347         return remoteArtifactRepositories;
348     }
349
350     public boolean hasParent()
351     {
352         return getParent() != null;
353     }
354
355     public File JavaDoc getFile()
356     {
357         return file;
358     }
359
360     public void setFile( File JavaDoc file )
361     {
362         this.file = file;
363     }
364
365     public File JavaDoc getBasedir()
366     {
367         if ( getFile() != null )
368         {
369             return getFile().getParentFile();
370         }
371         else
372         {
373             // repository based POM
374
return null;
375         }
376     }
377
378     public void setDependencies( List dependencies )
379     {
380         model.setDependencies( dependencies );
381     }
382
383     public List getDependencies()
384     {
385         return model.getDependencies();
386     }
387
388     public DependencyManagement getDependencyManagement()
389     {
390         return model.getDependencyManagement();
391     }
392
393     // ----------------------------------------------------------------------
394
// Test and compile sourceroots.
395
// ----------------------------------------------------------------------
396

397     public void addCompileSourceRoot( String JavaDoc path )
398     {
399         if ( path != null )
400         {
401             path = path.trim();
402             if ( path.length() != 0 )
403             {
404                 if ( !compileSourceRoots.contains( path ) )
405                 {
406                     compileSourceRoots.add( path );
407                 }
408             }
409         }
410     }
411
412     public void addScriptSourceRoot( String JavaDoc path )
413     {
414         if ( path != null )
415         {
416             path = path.trim();
417             if ( path.length() != 0 )
418             {
419                 if ( !scriptSourceRoots.contains( path ) )
420                 {
421                     scriptSourceRoots.add( path );
422                 }
423             }
424         }
425     }
426
427     public void addTestCompileSourceRoot( String JavaDoc path )
428     {
429         if ( path != null )
430         {
431             path = path.trim();
432             if ( path.length() != 0 )
433             {
434                 if ( !testCompileSourceRoots.contains( path ) )
435                 {
436                     testCompileSourceRoots.add( path );
437                 }
438             }
439         }
440     }
441
442     public List getCompileSourceRoots()
443     {
444         return compileSourceRoots;
445     }
446
447     public List getScriptSourceRoots()
448     {
449         return scriptSourceRoots;
450     }
451
452     public List getTestCompileSourceRoots()
453     {
454         return testCompileSourceRoots;
455     }
456
457     public List getCompileClasspathElements()
458         throws DependencyResolutionRequiredException
459     {
460         List list = new ArrayList JavaDoc( getArtifacts().size() );
461
462         list.add( getBuild().getOutputDirectory() );
463
464         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
465         {
466             Artifact a = (Artifact) i.next();
467
468             if ( a.getArtifactHandler().isAddedToClasspath() )
469             {
470                 // TODO: let the scope handler deal with this
471
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) ||
472                     Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
473                 {
474                     String JavaDoc refId = getProjectReferenceId( a.getGroupId(), a.getArtifactId() );
475                     MavenProject project = (MavenProject) projectReferences.get( refId );
476                     if ( project != null )
477                     {
478                         list.add( project.getBuild().getOutputDirectory() );
479                     }
480                     else
481                     {
482                         File JavaDoc file = a.getFile();
483                         if ( file == null )
484                         {
485                             throw new DependencyResolutionRequiredException( a );
486                         }
487                         list.add( file.getPath() );
488                     }
489                 }
490             }
491         }
492         return list;
493     }
494
495     public List getCompileArtifacts()
496     {
497         List list = new ArrayList JavaDoc( getArtifacts().size() );
498
499         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
500         {
501             Artifact a = (Artifact) i.next();
502
503             // TODO: classpath check doesn't belong here - that's the other method
504
if ( a.getArtifactHandler().isAddedToClasspath() )
505             {
506                 // TODO: let the scope handler deal with this
507
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) ||
508                     Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
509                 {
510                     list.add( a );
511                 }
512             }
513         }
514         return list;
515     }
516
517     public List getCompileDependencies()
518     {
519         Set artifacts = getArtifacts();
520
521         if ( artifacts == null || artifacts.isEmpty() )
522         {
523             return Collections.EMPTY_LIST;
524         }
525
526         List list = new ArrayList JavaDoc( artifacts.size() );
527
528         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
529         {
530             Artifact a = (Artifact) i.next();
531
532             // TODO: let the scope handler deal with this
533
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) ||
534                 Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
535             {
536                 Dependency dependency = new Dependency();
537
538                 dependency.setArtifactId( a.getArtifactId() );
539                 dependency.setGroupId( a.getGroupId() );
540                 dependency.setVersion( a.getVersion() );
541                 dependency.setScope( a.getScope() );
542                 dependency.setType( a.getType() );
543                 dependency.setClassifier( a.getClassifier() );
544
545                 list.add( dependency );
546             }
547         }
548         return list;
549     }
550
551     public List getTestClasspathElements()
552         throws DependencyResolutionRequiredException
553     {
554         List list = new ArrayList JavaDoc( getArtifacts().size() + 1 );
555
556         list.add( getBuild().getOutputDirectory() );
557
558         list.add( getBuild().getTestOutputDirectory() );
559         
560         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
561         {
562             Artifact a = (Artifact) i.next();
563
564             if ( a.getArtifactHandler().isAddedToClasspath() )
565             {
566                 // TODO: let the scope handler deal with this
567
// NOTE: [jc] scope == 'test' is the widest possible scope, so we don't really need to perform
568
// this check...
569
// if ( Artifact.SCOPE_TEST.equals( a.getScope() ) || Artifact.SCOPE_COMPILE.equals( a.getScope() ) ||
570
// Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
571
// {
572
// }
573
File JavaDoc file = a.getFile();
574                 if ( file == null )
575                 {
576                     throw new DependencyResolutionRequiredException( a );
577                 }
578                 list.add( file.getPath() );
579             }
580         }
581         return list;
582     }
583
584     public List getTestArtifacts()
585     {
586         List list = new ArrayList JavaDoc( getArtifacts().size() );
587
588         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
589         {
590             Artifact a = (Artifact) i.next();
591
592             // TODO: classpath check doesn't belong here - that's the other method
593
if ( a.getArtifactHandler().isAddedToClasspath() )
594             {
595                 // TODO: let the scope handler deal with this
596
// NOTE: [jc] scope == 'test' is the widest possible scope, so we don't really need to perform
597
// this check...
598
// if ( Artifact.SCOPE_TEST.equals( a.getScope() ) || Artifact.SCOPE_COMPILE.equals( a.getScope() ) ||
599
// Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
600
// {
601
// list.add( a );
602
// }
603

604                 list.add( a );
605             }
606         }
607         return list;
608     }
609
610     public List getTestDependencies()
611     {
612         Set artifacts = getArtifacts();
613
614         if ( artifacts == null || artifacts.isEmpty() )
615         {
616             return Collections.EMPTY_LIST;
617         }
618
619         List list = new ArrayList JavaDoc( artifacts.size() );
620
621         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
622         {
623             Artifact a = (Artifact) i.next();
624
625             // TODO: let the scope handler deal with this
626
// NOTE: [jc] scope == 'test' is the widest possible scope, so we don't really need to perform
627
// this check...
628
// if ( Artifact.SCOPE_TEST.equals( a.getScope() ) || Artifact.SCOPE_COMPILE.equals( a.getScope() ) ||
629
// Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
630
// {
631
// }
632

633             Dependency dependency = new Dependency();
634
635             dependency.setArtifactId( a.getArtifactId() );
636             dependency.setGroupId( a.getGroupId() );
637             dependency.setVersion( a.getVersion() );
638             dependency.setScope( a.getScope() );
639             dependency.setType( a.getType() );
640             dependency.setClassifier( a.getClassifier() );
641
642             list.add( dependency );
643         }
644         return list;
645     }
646
647     public List getRuntimeClasspathElements()
648         throws DependencyResolutionRequiredException
649     {
650         List list = new ArrayList JavaDoc( getArtifacts().size() + 1 );
651
652         list.add( getBuild().getOutputDirectory() );
653
654         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
655         {
656             Artifact a = (Artifact) i.next();
657
658             if ( a.getArtifactHandler().isAddedToClasspath() )
659             {
660                 // TODO: let the scope handler deal with this
661
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
662                 {
663                     File JavaDoc file = a.getFile();
664                     if ( file == null )
665                     {
666                         throw new DependencyResolutionRequiredException( a );
667                     }
668                     list.add( file.getPath() );
669                 }
670             }
671         }
672         return list;
673     }
674
675     public List getRuntimeArtifacts()
676     {
677         List list = new ArrayList JavaDoc( getArtifacts().size() );
678
679         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
680         {
681             Artifact a = (Artifact) i.next();
682
683             // TODO: classpath check doesn't belong here - that's the other method
684
if ( a.getArtifactHandler().isAddedToClasspath() )
685             {
686                 // TODO: let the scope handler deal with this
687
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
688                 {
689                     list.add( a );
690                 }
691             }
692         }
693         return list;
694     }
695
696     public List getRuntimeDependencies()
697     {
698         Set artifacts = getArtifacts();
699
700         if ( artifacts == null || artifacts.isEmpty() )
701         {
702             return Collections.EMPTY_LIST;
703         }
704
705         List list = new ArrayList JavaDoc( artifacts.size() );
706
707         for ( Iterator JavaDoc i = artifacts.iterator(); i.hasNext(); )
708         {
709             Artifact a = (Artifact) i.next();
710
711             // TODO: let the scope handler deal with this
712
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
713             {
714                 Dependency dependency = new Dependency();
715
716                 dependency.setArtifactId( a.getArtifactId() );
717                 dependency.setGroupId( a.getGroupId() );
718                 dependency.setVersion( a.getVersion() );
719                 dependency.setScope( a.getScope() );
720                 dependency.setType( a.getType() );
721                 dependency.setClassifier( a.getClassifier() );
722
723                 list.add( dependency );
724             }
725         }
726         return list;
727     }
728
729     public List getSystemClasspathElements()
730         throws DependencyResolutionRequiredException
731     {
732         List list = new ArrayList JavaDoc( getArtifacts().size() );
733
734         list.add( getBuild().getOutputDirectory() );
735
736         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
737         {
738             Artifact a = (Artifact) i.next();
739
740             if ( a.getArtifactHandler().isAddedToClasspath() )
741             {
742                 // TODO: let the scope handler deal with this
743
if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
744                 {
745                     String JavaDoc refId = getProjectReferenceId( a.getGroupId(), a.getArtifactId() );
746                     MavenProject project = (MavenProject) projectReferences.get( refId );
747                     if ( project != null )
748                     {
749                         list.add( project.getBuild().getOutputDirectory() );
750                     }
751                     else
752                     {
753                         File JavaDoc file = a.getFile();
754                         if ( file == null )
755                         {
756                             throw new DependencyResolutionRequiredException( a );
757                         }
758                         list.add( file.getPath() );
759                     }
760                 }
761             }
762         }
763         return list;
764     }
765
766     public List getSystemArtifacts()
767     {
768         List list = new ArrayList JavaDoc( getArtifacts().size() );
769
770         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
771         {
772             Artifact a = (Artifact) i.next();
773
774             // TODO: classpath check doesn't belong here - that's the other method
775
if ( a.getArtifactHandler().isAddedToClasspath() )
776             {
777                 // TODO: let the scope handler deal with this
778
if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
779                 {
780                     list.add( a );
781                 }
782             }
783         }
784         return list;
785     }
786
787     public List getSystemDependencies()
788     {
789         Set artifacts = getArtifacts();
790
791         if ( artifacts == null || artifacts.isEmpty() )
792         {
793             return Collections.EMPTY_LIST;
794         }
795
796         List list = new ArrayList JavaDoc( artifacts.size() );
797
798         for ( Iterator JavaDoc i = getArtifacts().iterator(); i.hasNext(); )
799         {
800             Artifact a = (Artifact) i.next();
801
802             // TODO: let the scope handler deal with this
803
if ( Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
804             {
805                 Dependency dependency = new Dependency();
806
807                 dependency.setArtifactId( a.getArtifactId() );
808                 dependency.setGroupId( a.getGroupId() );
809                 dependency.setVersion( a.getVersion() );
810                 dependency.setScope( a.getScope() );
811                 dependency.setType( a.getType() );
812                 dependency.setClassifier( a.getClassifier() );
813
814                 list.add( dependency );
815             }
816         }
817         return list;
818     }
819
820     // ----------------------------------------------------------------------
821
// Delegate to the model
822
// ----------------------------------------------------------------------
823

824     public void setModelVersion( String JavaDoc pomVersion )
825     {
826         model.setModelVersion( pomVersion );
827     }
828
829     public String JavaDoc getModelVersion()
830     {
831         return model.getModelVersion();
832     }
833
834     public String JavaDoc getId()
835     {
836         return model.getId();
837     }
838
839     public void setGroupId( String JavaDoc groupId )
840     {
841         model.setGroupId( groupId );
842     }
843
844     public String JavaDoc getGroupId()
845     {
846         String JavaDoc groupId = model.getGroupId();
847         
848         if ( groupId == null && model.getParent() != null )
849         {
850             groupId = model.getParent().getGroupId();
851         }
852         
853         return groupId;
854     }
855
856     public void setArtifactId( String JavaDoc artifactId )
857     {
858         model.setArtifactId( artifactId );
859     }
860
861     public String JavaDoc getArtifactId()
862     {
863         return model.getArtifactId();
864     }
865
866     public void setName( String JavaDoc name )
867     {
868         model.setName( name );
869     }
870
871     public String JavaDoc getName()
872     {
873         // TODO: this should not be allowed to be null.
874
if ( model.getName() != null )
875         {
876             return model.getName();
877         }
878         else
879         {
880             return "Unnamed - " + getId();
881         }
882     }
883
884     public void setVersion( String JavaDoc version )
885     {
886         model.setVersion( version );
887     }
888
889     public String JavaDoc getVersion()
890     {
891         String JavaDoc version = model.getVersion();
892         
893         if ( version == null && model.getParent() != null )
894         {
895             version = model.getParent().getVersion();
896         }
897         
898         return version;
899     }
900
901     public String JavaDoc getPackaging()
902     {
903         return model.getPackaging();
904     }
905
906     public void setPackaging( String JavaDoc packaging )
907     {
908         model.setPackaging( packaging );
909     }
910
911     public void setInceptionYear( String JavaDoc inceptionYear )
912     {
913         model.setInceptionYear( inceptionYear );
914     }
915
916     public String JavaDoc getInceptionYear()
917     {
918         return model.getInceptionYear();
919     }
920
921     public void setUrl( String JavaDoc url )
922     {
923         model.setUrl( url );
924     }
925
926     public String JavaDoc getUrl()
927     {
928         return model.getUrl();
929     }
930
931     public Prerequisites getPrerequisites()
932     {
933         return model.getPrerequisites();
934     }
935
936     public void setIssueManagement( IssueManagement issueManagement )
937     {
938         model.setIssueManagement( issueManagement );
939     }
940
941     public CiManagement getCiManagement()
942     {
943         return model.getCiManagement();
944     }
945
946     public void setCiManagement( CiManagement ciManagement )
947     {
948         model.setCiManagement( ciManagement );
949     }
950
951     public IssueManagement getIssueManagement()
952     {
953         return model.getIssueManagement();
954     }
955
956     public void setDistributionManagement( DistributionManagement distributionManagement )
957     {
958         model.setDistributionManagement( distributionManagement );
959     }
960
961     public DistributionManagement getDistributionManagement()
962     {
963         return model.getDistributionManagement();
964     }
965
966     public void setDescription( String JavaDoc description )
967     {
968         model.setDescription( description );
969     }
970
971     public String JavaDoc getDescription()
972     {
973         return model.getDescription();
974     }
975
976     public void setOrganization( Organization organization )
977     {
978         model.setOrganization( organization );
979     }
980
981     public Organization getOrganization()
982     {
983         return model.getOrganization();
984     }
985
986     public void setScm( Scm scm )
987     {
988         model.setScm( scm );
989     }
990
991     public Scm getScm()
992     {
993         return model.getScm();
994     }
995
996     public void setMailingLists( List mailingLists )
997     {
998         model.setMailingLists( mailingLists );
999     }
1000
1001    public List getMailingLists()
1002    {
1003        return model.getMailingLists();
1004    }
1005
1006    public void addMailingList( MailingList mailingList )
1007    {
1008        model.addMailingList( mailingList );
1009    }
1010
1011    public void setDevelopers( List developers )
1012    {
1013        model.setDevelopers( developers );
1014    }
1015
1016    public List getDevelopers()
1017    {
1018        return model.getDevelopers();
1019    }
1020
1021    public void addDeveloper( Developer developer )
1022    {
1023        model.addDeveloper( developer );
1024    }
1025
1026    public void setContributors( List contributors )
1027    {
1028        model.setContributors( contributors );
1029    }
1030
1031    public List getContributors()
1032    {
1033        return model.getContributors();
1034    }
1035
1036    public void addContributor( Contributor contributor )
1037    {
1038        model.addContributor( contributor );
1039    }
1040
1041    public void setBuild( Build build )
1042    {
1043        this.buildOverlay = new BuildOverlay( build );
1044
1045        model.setBuild( build );
1046    }
1047
1048    public Build getBuild()
1049    {
1050        if ( buildOverlay == null )
1051        {
1052            buildOverlay = new BuildOverlay( getModelBuild() );
1053        }
1054
1055        return buildOverlay;
1056    }
1057
1058    public List getResources()
1059    {
1060        return getBuild().getResources();
1061    }
1062
1063    public List getTestResources()
1064    {
1065        return getBuild().getTestResources();
1066    }
1067
1068    public void addResource( Resource resource )
1069    {
1070        getBuild().addResource( resource );
1071    }
1072
1073    public void addTestResource( Resource testResource )
1074    {
1075        getBuild().addTestResource( testResource );
1076    }
1077
1078    public void setReporting( Reporting reporting )
1079    {
1080        model.setReporting( reporting );
1081    }
1082
1083    public Reporting getReporting()
1084    {
1085        return model.getReporting();
1086    }
1087
1088    public void setLicenses( List licenses )
1089    {
1090        model.setLicenses( licenses );
1091    }
1092
1093    public List getLicenses()
1094    {
1095        return model.getLicenses();
1096    }
1097
1098    public void addLicense( License license )
1099    {
1100        model.addLicense( license );
1101    }
1102
1103    public void setArtifacts( Set artifacts )
1104    {
1105        this.artifacts = artifacts;
1106
1107        // flush the calculated artifactMap
1108
this.artifactMap = null;
1109    }
1110
1111    public Set getArtifacts()
1112    {
1113        return artifacts == null ? Collections.EMPTY_SET : artifacts;
1114    }
1115
1116    public Map JavaDoc getArtifactMap()
1117    {
1118        if ( artifactMap == null )
1119        {
1120            artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() );
1121        }
1122
1123        return artifactMap;
1124    }
1125
1126    public void setPluginArtifacts( Set pluginArtifacts )
1127    {
1128        this.pluginArtifacts = pluginArtifacts;
1129
1130        this.pluginArtifactMap = null;
1131    }
1132
1133    public Set getPluginArtifacts()
1134    {
1135        return pluginArtifacts;
1136    }
1137
1138    public Map JavaDoc getPluginArtifactMap()
1139    {
1140        if ( pluginArtifactMap == null )
1141        {
1142            pluginArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getPluginArtifacts() );
1143        }
1144
1145        return pluginArtifactMap;
1146    }
1147
1148    public void setReportArtifacts( Set reportArtifacts )
1149    {
1150        this.reportArtifacts = reportArtifacts;
1151
1152        this.reportArtifactMap = null;
1153    }
1154
1155    public Set getReportArtifacts()
1156    {
1157        return reportArtifacts;
1158    }
1159
1160    public Map JavaDoc getReportArtifactMap()
1161    {
1162        if ( reportArtifactMap == null )
1163        {
1164            reportArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getReportArtifacts() );
1165        }
1166
1167        return reportArtifactMap;
1168    }
1169
1170    public void setExtensionArtifacts( Set extensionArtifacts )
1171    {
1172        this.extensionArtifacts = extensionArtifacts;
1173
1174        this.extensionArtifactMap = null;
1175    }
1176
1177    public Set getExtensionArtifacts()
1178    {
1179        return this.extensionArtifacts;
1180    }
1181
1182    public Map JavaDoc getExtensionArtifactMap()
1183    {
1184        if ( extensionArtifactMap == null )
1185        {
1186            extensionArtifactMap = ArtifactUtils.artifactMapByVersionlessId( getExtensionArtifacts() );
1187        }
1188
1189        return extensionArtifactMap;
1190    }
1191
1192    public void setParentArtifact( Artifact parentArtifact )
1193    {
1194        this.parentArtifact = parentArtifact;
1195    }
1196
1197    public Artifact getParentArtifact()
1198    {
1199        return parentArtifact;
1200    }
1201
1202    public List getRepositories()
1203    {
1204        return model.getRepositories();
1205    }
1206
1207    // ----------------------------------------------------------------------
1208
// Plugins
1209
// ----------------------------------------------------------------------
1210

1211    public List getReportPlugins()
1212    {
1213        if ( model.getReporting() == null )
1214        {
1215            return null;
1216        }
1217        return model.getReporting().getPlugins();
1218
1219    }
1220
1221    public List getBuildPlugins()
1222    {
1223        if ( model.getBuild() == null )
1224        {
1225            return null;
1226        }
1227        return model.getBuild().getPlugins();
1228    }
1229
1230    public List getModules()
1231    {
1232        return model.getModules();
1233    }
1234
1235    public PluginManagement getPluginManagement()
1236    {
1237        PluginManagement pluginMgmt = null;
1238
1239        Build build = model.getBuild();
1240        if ( build != null )
1241        {
1242            pluginMgmt = build.getPluginManagement();
1243        }
1244
1245        return pluginMgmt;
1246    }
1247    
1248    private Build getModelBuild()
1249    {
1250        Build build = model.getBuild();
1251
1252        if ( build == null )
1253        {
1254            build = new Build();
1255
1256            model.setBuild( build );
1257        }
1258        
1259        return build;
1260    }
1261
1262    public void addPlugin( Plugin plugin )
1263    {
1264        Build build = getModelBuild();
1265
1266        if ( !build.getPluginsAsMap().containsKey( plugin.getKey() ) )
1267        {
1268            injectPluginManagementInfo( plugin );
1269
1270            build.addPlugin( plugin );
1271            build.flushPluginMap();
1272        }
1273    }
1274    
1275    public void injectPluginManagementInfo( Plugin plugin )
1276    {
1277        PluginManagement pm = getModelBuild().getPluginManagement();
1278
1279        if ( pm != null )
1280        {
1281            Map JavaDoc pmByKey = pm.getPluginsAsMap();
1282
1283            String JavaDoc pluginKey = plugin.getKey();
1284
1285            if ( pmByKey != null && pmByKey.containsKey( pluginKey ) )
1286            {
1287                Plugin pmPlugin = (Plugin) pmByKey.get( pluginKey );
1288
1289                ModelUtils.mergePluginDefinitions( plugin, pmPlugin, false );
1290            }
1291        }
1292    }
1293
1294    public List getCollectedProjects()
1295    {
1296        return collectedProjects;
1297    }
1298
1299    public void setCollectedProjects( List collectedProjects )
1300    {
1301        this.collectedProjects = collectedProjects;
1302    }
1303
1304    public void setPluginArtifactRepositories( List pluginArtifactRepositories )
1305    {
1306        this.pluginArtifactRepositories = pluginArtifactRepositories;
1307    }
1308
1309    /**
1310     * @return a list of ArtifactRepository objects constructed
1311     * from the Repository objects returned by getPluginRepositories.
1312     */

1313    public List getPluginArtifactRepositories()
1314    {
1315        return pluginArtifactRepositories;
1316    }
1317
1318    public ArtifactRepository getDistributionManagementArtifactRepository()
1319    {
1320        return getArtifact().isSnapshot() && snapshotArtifactRepository != null ? snapshotArtifactRepository
1321            : releaseArtifactRepository;
1322    }
1323
1324    public List getPluginRepositories()
1325    {
1326        return model.getPluginRepositories();
1327    }
1328
1329    public void setActiveProfiles( List activeProfiles )
1330    {
1331        this.activeProfiles.addAll( activeProfiles );
1332    }
1333
1334    public List getActiveProfiles()
1335    {
1336        return activeProfiles;
1337    }
1338
1339    public void addAttachedArtifact( Artifact artifact )
1340    {
1341        getAttachedArtifacts().add( artifact );
1342    }
1343
1344    public List getAttachedArtifacts()
1345    {
1346        if ( attachedArtifacts == null )
1347        {
1348            attachedArtifacts = new ArrayList JavaDoc();
1349        }
1350        return attachedArtifacts;
1351    }
1352
1353    public Xpp3Dom getGoalConfiguration( String JavaDoc pluginGroupId, String JavaDoc pluginArtifactId, String JavaDoc executionId,
1354                                         String JavaDoc goalId )
1355    {
1356        Xpp3Dom dom = null;
1357
1358        // ----------------------------------------------------------------------
1359
// I would like to be able to lookup the Mojo object using a key but
1360
// we have a limitation in modello that will be remedied shortly. So
1361
// for now I have to iterate through and see what we have.
1362
// ----------------------------------------------------------------------
1363

1364        if ( getBuildPlugins() != null )
1365        {
1366            for ( Iterator JavaDoc iterator = getBuildPlugins().iterator(); iterator.hasNext(); )
1367            {
1368                Plugin plugin = (Plugin) iterator.next();
1369
1370                if ( pluginGroupId.equals( plugin.getGroupId() ) && pluginArtifactId.equals( plugin.getArtifactId() ) )
1371                {
1372                    dom = (Xpp3Dom) plugin.getConfiguration();
1373
1374                    if ( executionId != null )
1375                    {
1376                        PluginExecution execution = (PluginExecution) plugin.getExecutionsAsMap().get( executionId );
1377                        if ( execution != null )
1378                        {
1379                            Xpp3Dom executionConfiguration = (Xpp3Dom) execution.getConfiguration();
1380                            if ( executionConfiguration != null )
1381                            {
1382                                Xpp3Dom newDom = new Xpp3Dom( executionConfiguration );
1383                                dom = Xpp3Dom.mergeXpp3Dom( newDom, dom );
1384                            }
1385                        }
1386                    }
1387                    break;
1388                }
1389            }
1390        }
1391
1392        if ( dom != null )
1393        {
1394            // make a copy so the original in the POM doesn't get messed with
1395
dom = new Xpp3Dom( dom );
1396        }
1397
1398        return dom;
1399    }
1400
1401    public Xpp3Dom getReportConfiguration( String JavaDoc pluginGroupId, String JavaDoc pluginArtifactId, String JavaDoc reportSetId )
1402    {
1403        Xpp3Dom dom = null;
1404
1405        // ----------------------------------------------------------------------
1406
// I would like to be able to lookup the Mojo object using a key but
1407
// we have a limitation in modello that will be remedied shortly. So
1408
// for now I have to iterate through and see what we have.
1409
// ----------------------------------------------------------------------
1410

1411        if ( getReportPlugins() != null )
1412        {
1413            for ( Iterator JavaDoc iterator = getReportPlugins().iterator(); iterator.hasNext(); )
1414            {
1415                ReportPlugin plugin = (ReportPlugin) iterator.next();
1416
1417                if ( pluginGroupId.equals( plugin.getGroupId() ) && pluginArtifactId.equals( plugin.getArtifactId() ) )
1418                {
1419                    dom = (Xpp3Dom) plugin.getConfiguration();
1420
1421                    if ( reportSetId != null )
1422                    {
1423                        ReportSet reportSet = (ReportSet) plugin.getReportSetsAsMap().get( reportSetId );
1424                        if ( reportSet != null )
1425                        {
1426                            Xpp3Dom executionConfiguration = (Xpp3Dom) reportSet.getConfiguration();
1427                            if ( executionConfiguration != null )
1428                            {
1429                                Xpp3Dom newDom = new Xpp3Dom( executionConfiguration );
1430                                dom = Xpp3Dom.mergeXpp3Dom( newDom, dom );
1431                            }
1432                        }
1433                    }
1434                    break;
1435                }
1436            }
1437        }
1438
1439        if ( dom != null )
1440        {
1441            // make a copy so the original in the POM doesn't get messed with
1442
dom = new Xpp3Dom( dom );
1443        }
1444
1445        return dom;
1446    }
1447
1448    public MavenProject getExecutionProject()
1449    {
1450        return executionProject;
1451    }
1452
1453    public void setExecutionProject( MavenProject executionProject )
1454    {
1455        this.executionProject = executionProject;
1456    }
1457
1458    public void writeModel( Writer writer )
1459        throws IOException JavaDoc
1460    {
1461        MavenXpp3Writer pomWriter = new MavenXpp3Writer();
1462
1463        pomWriter.write( writer, getModel() );
1464    }
1465
1466    public void writeOriginalModel( Writer writer )
1467        throws IOException JavaDoc
1468    {
1469        MavenXpp3Writer pomWriter = new MavenXpp3Writer();
1470
1471        pomWriter.write( writer, getOriginalModel() );
1472    }
1473
1474    public Set getDependencyArtifacts()
1475    {
1476        return dependencyArtifacts;
1477    }
1478
1479    public void setDependencyArtifacts( Set dependencyArtifacts )
1480    {
1481        this.dependencyArtifacts = dependencyArtifacts;
1482    }
1483
1484    public void setReleaseArtifactRepository( ArtifactRepository releaseArtifactRepository )
1485    {
1486        this.releaseArtifactRepository = releaseArtifactRepository;
1487    }
1488
1489    public void setSnapshotArtifactRepository( ArtifactRepository snapshotArtifactRepository )
1490    {
1491        this.snapshotArtifactRepository = snapshotArtifactRepository;
1492    }
1493
1494    public void setOriginalModel( Model originalModel )
1495    {
1496        this.originalModel = originalModel;
1497    }
1498
1499    public Model getOriginalModel()
1500    {
1501        return originalModel;
1502    }
1503
1504    public boolean equals( Object JavaDoc other )
1505    {
1506        if ( other == this )
1507        {
1508            return true;
1509        }
1510        else if ( !( other instanceof MavenProject ) )
1511        {
1512            return false;
1513        }
1514        else
1515        {
1516            MavenProject otherProject = (MavenProject) other;
1517
1518            return getId().equals( otherProject.getId() );
1519        }
1520    }
1521
1522    public int hashCode()
1523    {
1524        return getId().hashCode();
1525    }
1526
1527    public List getBuildExtensions()
1528    {
1529        Build build = getBuild();
1530        if ( build == null || build.getExtensions() == null )
1531        {
1532            return Collections.EMPTY_LIST;
1533        }
1534        else
1535        {
1536            return build.getExtensions();
1537        }
1538    }
1539
1540    /**
1541     * @todo the lazy initialisation of this makes me uneasy.
1542     */

1543    public Set createArtifacts( ArtifactFactory artifactFactory, String JavaDoc inheritedScope,
1544                                ArtifactFilter dependencyFilter )
1545        throws InvalidDependencyVersionException
1546    {
1547        return MavenMetadataSource.createArtifacts( artifactFactory, getDependencies(), inheritedScope,
1548                                                    dependencyFilter, this );
1549    }
1550
1551    public void addProjectReference( MavenProject project )
1552    {
1553        projectReferences.put( getProjectReferenceId( project.getGroupId(), project.getArtifactId() ), project );
1554    }
1555
1556    private static String JavaDoc getProjectReferenceId( String JavaDoc groupId, String JavaDoc artifactId )
1557    {
1558        return groupId + ":" + artifactId;
1559    }
1560
1561    /**
1562     * @deprecated Use MavenProjectHelper.attachArtifact(..) instead.
1563     */

1564    public void attachArtifact( String JavaDoc type, String JavaDoc classifier, File JavaDoc file )
1565    {
1566    }
1567
1568    public Properties JavaDoc getProperties()
1569    {
1570        return getModel().getProperties();
1571    }
1572
1573    public List getFilters()
1574    {
1575        return getBuild().getFilters();
1576    }
1577
1578    public Map JavaDoc getProjectReferences()
1579    {
1580        return projectReferences;
1581    }
1582
1583    public boolean isExecutionRoot()
1584    {
1585        return executionRoot;
1586    }
1587
1588    public void setExecutionRoot( boolean executionRoot )
1589    {
1590        this.executionRoot = executionRoot;
1591    }
1592
1593    public String JavaDoc getDefaultGoal()
1594    {
1595        return getBuild() != null ? getBuild().getDefaultGoal() : null;
1596    }
1597
1598    public Artifact replaceWithActiveArtifact( Artifact pluginArtifact )
1599    {
1600        if ( getProjectReferences() != null && !getProjectReferences().isEmpty() )
1601        {
1602            // TODO: use MavenProject getProjectReferenceId
1603
String JavaDoc refId = ArtifactUtils.versionlessKey( pluginArtifact.getGroupId(), pluginArtifact.getArtifactId() );
1604            MavenProject ref = (MavenProject) getProjectReferences().get( refId );
1605            if ( ref != null && ref.getArtifact() != null )
1606            {
1607                // TODO: if not matching, we should get the correct artifact from that project (attached)
1608
if ( ref.getArtifact().getDependencyConflictId().equals( pluginArtifact.getDependencyConflictId() ) )
1609                {
1610                    // if the project artifact doesn't exist, don't use it. We haven't built that far.
1611
if ( ref.getArtifact().getFile() != null && ref.getArtifact().getFile().exists() )
1612                    {
1613                        pluginArtifact = new ActiveProjectArtifact( ref, pluginArtifact );
1614                    }
1615                    else
1616                    {
1617/* TODO...
1618                        logger.warn( "Artifact found in the reactor has not been built when it's use was " +
1619                            "attempted - resolving from the repository instead" );
1620*/

1621                    }
1622                }
1623            }
1624        }
1625        return pluginArtifact;
1626    }
1627}
1628
Popular Tags