1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.plugin.eclipse;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.LinkedHashSet;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.ListIterator;
33  import java.util.Map;
34  import java.util.Set;
35  
36  import org.apache.maven.artifact.Artifact;
37  import org.apache.maven.artifact.handler.ArtifactHandler;
38  import org.apache.maven.artifact.manager.WagonManager;
39  import org.apache.maven.model.Build;
40  import org.apache.maven.model.Plugin;
41  import org.apache.maven.model.Resource;
42  import org.apache.maven.plugin.MojoExecutionException;
43  import org.apache.maven.plugin.eclipse.reader.ReadWorkspaceLocations;
44  import org.apache.maven.plugin.eclipse.writers.EclipseAjdtWriter;
45  import org.apache.maven.plugin.eclipse.writers.EclipseClasspathWriter;
46  import org.apache.maven.plugin.eclipse.writers.EclipseManifestWriter;
47  import org.apache.maven.plugin.eclipse.writers.EclipseProjectWriter;
48  import org.apache.maven.plugin.eclipse.writers.EclipseWriterConfig;
49  import org.apache.maven.plugin.eclipse.writers.workspace.EclipseSettingsWriter;
50  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpApplicationXMLWriter;
51  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponent15Writer;
52  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpComponentWriter;
53  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpFacetsWriter;
54  import org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpmodulesWriter;
55  import org.apache.maven.plugin.ide.AbstractIdeSupportMojo;
56  import org.apache.maven.plugin.ide.IdeDependency;
57  import org.apache.maven.plugin.ide.IdeUtils;
58  import org.apache.maven.plugin.ide.JeeUtils;
59  import org.apache.maven.plugins.annotations.Component;
60  import org.apache.maven.plugins.annotations.Execute;
61  import org.apache.maven.plugins.annotations.LifecyclePhase;
62  import org.apache.maven.plugins.annotations.Mojo;
63  import org.apache.maven.plugins.annotations.Parameter;
64  import org.apache.maven.project.MavenProject;
65  import org.apache.maven.settings.MavenSettingsBuilder;
66  import org.apache.maven.settings.Proxy;
67  import org.apache.maven.settings.Settings;
68  import org.apache.maven.wagon.Wagon;
69  import org.apache.maven.wagon.WagonException;
70  import org.apache.maven.wagon.observers.Debug;
71  import org.apache.maven.wagon.proxy.ProxyInfo;
72  import org.apache.maven.wagon.repository.Repository;
73  import org.codehaus.plexus.resource.ResourceManager;
74  import org.codehaus.plexus.resource.loader.FileResourceLoader;
75  import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
76  import org.codehaus.plexus.util.FileUtils;
77  import org.codehaus.plexus.util.IOUtil;
78  import org.codehaus.plexus.util.StringUtils;
79  import org.codehaus.plexus.util.xml.Xpp3Dom;
80  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  @Mojo( name = "eclipse" )
98  @Execute( phase = LifecyclePhase.GENERATE_RESOURCES )
99  public class EclipsePlugin
100     extends AbstractIdeSupportMojo
101 {
102     private static final String WEAVE_DEPENDENCY = "weaveDependency";
103 
104     private static final String WEAVE_DEPENDENCIES = "weaveDependencies";
105 
106     private static final String ASPECT_LIBRARY = "aspectLibrary";
107 
108     private static final String ASPECT_LIBRARIES = "aspectLibraries";
109 
110     private static final String ASPECT_DIRECTORY = "aspectDirectory";
111 
112     private static final String TEST_ASPECT_DIRECTORY = "testAspectDirectory";
113 
114     private static final String ASPECTJ_MAVEN_PLUGIN = "aspectj-maven-plugin";
115 
116     private static final String ORG_CODEHAUS_MOJO = "org.codehaus.mojo";
117 
118     private static final String DEFAULT_TEST_ASPECT_DIRECTORY = "src/test/aspect";
119 
120     private static final String DEFAULT_ASPECT_DIRECTORY = "src/main/aspect";
121 
122     private static final String NATURE_WST_FACET_CORE_NATURE = "org.eclipse.wst.common.project.facet.core.nature"; 
123 
124     private static final String BUILDER_WST_COMPONENT_STRUCTURAL_DEPENDENCY_RESOLVER =
125         "org.eclipse.wst.common.modulecore.ComponentStructuralBuilderDependencyResolver"; 
126 
127     protected static final String BUILDER_WST_VALIDATION = "org.eclipse.wst.validation.validationbuilder"; 
128 
129     private static final String BUILDER_JDT_CORE_JAVA = "org.eclipse.jdt.core.javabuilder"; 
130 
131     private static final String BUILDER_WST_COMPONENT_STRUCTURAL =
132         "org.eclipse.wst.common.modulecore.ComponentStructuralBuilder"; 
133 
134     private static final String BUILDER_WST_FACET = "org.eclipse.wst.common.project.facet.core.builder"; 
135 
136     private static final String BUILDER_AJDT_CORE_JAVA = "org.eclipse.ajdt.core.ajbuilder"; 
137 
138     private static final String NATURE_WST_MODULE_CORE_NATURE = "org.eclipse.wst.common.modulecore.ModuleCoreNature"; 
139 
140     private static final String NATURE_JDT_CORE_JAVA = "org.eclipse.jdt.core.javanature"; 
141 
142     private static final String NATURE_JEM_WORKBENCH_JAVA_EMF = "org.eclipse.jem.workbench.JavaEMFNature"; 
143 
144     private static final String NATURE_AJDT_CORE_JAVA = "org.eclipse.ajdt.ui.ajnature"; 
145 
146     protected static final String COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER = "org.eclipse.jdt.launching.JRE_CONTAINER"; 
147 
148     protected static final String ASPECTJ_RT_CONTAINER = "org.eclipse.ajdt.core.ASPECTJRT_CONTAINER"; 
149 
150     
151     public static final String[] WTP_SUPPORTED_VERSIONS = new String[] { "1.0", "1.5", "2.0", "R7", "none" }; 
152 
153     public static final String ASPECTJ_FILE_PATTERN = "**/*.aj";
154 
155     public static final String JAVA_FILE_PATTERN = "**/*.java";
156 
157     
158 
159 
160     private static final String POM_ELT_ARTIFACT_ID = "artifactId"; 
161 
162     
163 
164 
165     private static final String POM_ELT_GROUP_ID = "groupId"; 
166 
167     
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178     @Parameter
179     private List projectnatures;
180 
181     
182 
183 
184 
185 
186 
187 
188     @Parameter
189     private List excludes;
190 
191     
192 
193 
194 
195 
196 
197 
198 
199 
200     @Parameter
201     private List additionalProjectnatures;
202 
203     
204 
205 
206 
207 
208 
209 
210 
211 
212     @Parameter
213     private Map additionalProjectFacets;
214 
215     
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231     @Parameter
232     private List buildcommands;
233 
234     
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260     @Parameter
261     private List additionalBuildcommands;
262 
263     
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275     @Parameter
276     private List classpathContainers;
277 
278     
279 
280 
281 
282 
283     @Parameter( property = "eclipse.downloadSources" )
284     private boolean eclipseDownloadSources;
285 
286     
287 
288 
289     @Parameter( property = "eclipse.projectDir", alias = "outputDir" )
290     private File eclipseProjectDir;
291 
292     
293 
294 
295 
296     @Parameter( property = "eclipse.useProjectReferences", defaultValue = "true", required = true )
297     private boolean useProjectReferences;
298 
299     
300 
301 
302     @Parameter( property = "outputDirectory", alias = "outputDirectory", defaultValue = "${project.build.outputDirectory}", required = true )
303     private File buildOutputDirectory;
304 
305     
306 
307 
308 
309     @Parameter( property = "wtpversion", defaultValue = "none" )
310     private String wtpversion;
311 
312     
313 
314 
315 
316     @Parameter( property = "wtpContextName" )
317     private String wtpContextName;
318 
319     
320 
321 
322     private boolean ajdt;
323 
324     
325 
326 
327     @Parameter( property = "eclipse.manifest", defaultValue = "${basedir}/META-INF/MANIFEST.MF" )
328     private File manifest;
329 
330     
331 
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
363 
364 
365 
366 
367 
368 
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387 
388 
389 
390 
391 
392 
393 
394 
395 
396 
397     @Parameter
398     private EclipseConfigFile[] additionalConfig;
399 
400     
401 
402 
403 
404     @Parameter( property = "eclipse.addVersionToProjectName", defaultValue = "false" )
405     private boolean addVersionToProjectName;
406 
407     
408 
409 
410 
411     @Parameter( property = "eclipse.addGroupIdToProjectName", defaultValue = "false" )
412     private boolean addGroupIdToProjectName;
413 
414     
415 
416 
417 
418 
419     @Parameter( property = "eclipse.projectNameTemplate" )
420     private String projectNameTemplate;
421 
422     
423 
424 
425     private float wtpVersionFloat;
426 
427     
428 
429 
430     private boolean isJavaProject;
431 
432     
433 
434 
435     @Parameter( property = "eclipse.wtpmanifest", defaultValue = "false" )
436     private boolean wtpmanifest;
437 
438     
439 
440 
441     @Parameter( property = "eclipse.wtpapplicationxml", defaultValue = "false" )
442     private boolean wtpapplicationxml;
443 
444     
445 
446 
447     @Parameter( property = "eclipse.wtpdefaultserver" )
448     private String wtpdefaultserver;
449 
450     
451 
452 
453 
454 
455 
456 
457     @Parameter( property = "eclipse.preferStandardClasspathContainer", defaultValue = "false" )
458     private boolean preferStandardClasspathContainer;
459 
460     private WorkspaceConfiguration workspaceConfiguration;
461 
462     
463 
464 
465     @Component
466     private ResourceManager locator;
467 
468     
469 
470 
471     @Component
472     private WagonManager wagonManager;
473 
474     
475 
476 
477     @Component
478     private MavenSettingsBuilder mavenSettingsBuilder;
479 
480     
481 
482 
483 
484 
485 
486 
487 
488 
489     @Parameter( property = "eclipse.workspace" )
490     protected File workspace;
491 
492     
493 
494 
495 
496     @Parameter( property = "eclipse.limitProjectReferencesToWorkspace", defaultValue = "false" )
497     protected boolean limitProjectReferencesToWorkspace;
498 
499     
500 
501 
502 
503     @Parameter( property = "eclipse.ajdtVersion", defaultValue = "none" )
504     private String ajdtVersion;
505 
506     
507 
508 
509 
510 
511 
512     @Parameter
513     private List sourceExcludes;
514 
515     
516 
517 
518 
519 
520 
521 
522 
523 
524 
525 
526 
527     @Parameter
528     private List sourceIncludes;
529 
530     
531 
532 
533 
534 
535 
536 
537 
538 
539 
540 
541 
542 
543 
544 
545 
546 
547 
548 
549 
550 
551 
552 
553 
554 
555 
556 
557 
558 
559 
560 
561 
562 
563     @Parameter
564     private List linkedResources;
565 
566     
567 
568 
569 
570 
571 
572 
573     @Parameter( property = "eclipse.classpathContainersLast", defaultValue = "false" )
574     protected boolean classpathContainersLast;
575 
576     
577 
578 
579 
580 
581 
582 
583 
584 
585 
586     @Parameter( property = "eclipse.testSourcesLast", defaultValue = "false" )
587     protected boolean testSourcesLast;
588 
589     
590 
591 
592 
593 
594 
595 
596 
597 
598 
599 
600 
601 
602 
603 
604 
605 
606 
607 
608 
609 
610 
611 
612 
613 
614 
615 
616 
617 
618 
619 
620 
621 
622 
623 
624 
625 
626 
627 
628 
629 
630 
631 
632 
633 
634 
635 
636     @Parameter( property = "eclipse.jeeversion" )
637     protected String jeeversion;
638 
639     protected final boolean isJavaProject()
640     {
641         return isJavaProject;
642     }
643 
644     
645 
646 
647 
648 
649     public final List getBuildcommands()
650     {
651         return buildcommands;
652     }
653 
654     
655 
656 
657 
658 
659     public final void setBuildcommands( List buildcommands )
660     {
661         this.buildcommands = buildcommands;
662     }
663 
664     
665 
666 
667 
668 
669     public final File getBuildOutputDirectory()
670     {
671         return buildOutputDirectory;
672     }
673 
674     
675 
676 
677 
678 
679     public final void setBuildOutputDirectory( File buildOutputDirectory )
680     {
681         this.buildOutputDirectory = buildOutputDirectory;
682     }
683 
684     
685 
686 
687 
688 
689     public final List getClasspathContainers()
690     {
691         return classpathContainers;
692     }
693 
694     
695 
696 
697 
698 
699     public final void setClasspathContainers( List classpathContainers )
700     {
701         this.classpathContainers = classpathContainers;
702     }
703 
704     
705 
706 
707 
708 
709     public final File getEclipseProjectDir()
710     {
711         return eclipseProjectDir;
712     }
713 
714     
715 
716 
717 
718 
719     public final void setEclipseProjectDir( File eclipseProjectDir )
720     {
721         this.eclipseProjectDir = eclipseProjectDir;
722     }
723 
724     
725 
726 
727 
728 
729     public final List getProjectnatures()
730     {
731         return projectnatures;
732     }
733 
734     
735 
736 
737 
738 
739     public final void setProjectnatures( List projectnatures )
740     {
741         this.projectnatures = projectnatures;
742     }
743 
744     
745 
746 
747 
748 
749     public final boolean getUseProjectReferences()
750     {
751         return useProjectReferences;
752     }
753 
754     
755 
756 
757 
758 
759     public final void setUseProjectReferences( boolean useProjectReferences )
760     {
761         this.useProjectReferences = useProjectReferences;
762     }
763 
764     
765 
766 
767 
768 
769     public final String getWtpversion()
770     {
771         return wtpversion;
772     }
773 
774     
775 
776 
777 
778 
779     public final void setWtpversion( String wtpversion )
780     {
781         this.wtpversion = wtpversion;
782     }
783 
784     
785 
786 
787 
788 
789     public final List getAdditionalBuildcommands()
790     {
791         return additionalBuildcommands;
792     }
793 
794     
795 
796 
797 
798 
799     public final void setAdditionalBuildcommands( List additionalBuildcommands )
800     {
801         this.additionalBuildcommands = additionalBuildcommands;
802     }
803 
804     
805 
806 
807 
808 
809     public final List getAdditionalProjectnatures()
810     {
811         return additionalProjectnatures;
812     }
813 
814     
815 
816 
817 
818 
819     public final void setAdditionalProjectnatures( List additionalProjectnatures )
820     {
821         this.additionalProjectnatures = additionalProjectnatures;
822     }
823 
824     
825 
826 
827     public final boolean isAddVersionToProjectName()
828     {
829         return addVersionToProjectName;
830     }
831 
832     
833 
834 
835     public final void setAddVersionToProjectName( boolean addVersionToProjectName )
836     {
837         this.addVersionToProjectName = addVersionToProjectName;
838     }
839 
840     
841 
842 
843     public final boolean isAddGroupIdToProjectName()
844     {
845         return addGroupIdToProjectName;
846     }
847 
848     
849 
850 
851     public final void setAddGroupIdToProjectName( boolean addGroupIdToProjectName )
852     {
853         this.addGroupIdToProjectName = addGroupIdToProjectName;
854     }
855 
856     
857 
858 
859 
860 
861     public final String getProjectNameTemplate()
862     {
863         return projectNameTemplate;
864     }
865 
866     
867 
868 
869 
870 
871     public final void setProjectNameTemplate( String projectNameTemplate )
872     {
873         this.projectNameTemplate = projectNameTemplate;
874     }
875 
876     
877 
878 
879     public List getLinkedResources()
880     {
881         return linkedResources;
882     }
883 
884     
885 
886 
887     public void setLinkedResources( List linkedResources )
888     {
889         this.linkedResources = linkedResources;
890     }
891 
892     
893 
894 
895     public final boolean setup()
896         throws MojoExecutionException
897     {
898         boolean ready;
899 
900         checkDeprecations();
901         setProjectNameTemplate( IdeUtils.calculateProjectNameTemplate( getProjectNameTemplate(),
902                                                                        isAddVersionToProjectName(),
903                                                                        isAddGroupIdToProjectName(), getLog() ) );
904         ajdt = enableAjdt( executedProject ) && !ajdtVersion.equals( "none" );
905         ready = validate();
906 
907         
908         ArtifactHandler artifactHandler = project.getArtifact().getArtifactHandler();
909 
910         
911         
912         isJavaProject =
913             ( Constants.LANGUAGE_JAVA.equals( artifactHandler.getLanguage() ) && !Constants.PROJECT_PACKAGING_EAR.equals( packaging ) );
914 
915         if ( sourceIncludes == null )
916         {
917             sourceIncludes = new ArrayList();
918         }
919         if ( isJavaProject )
920         {
921             sourceIncludes.add( JAVA_FILE_PATTERN );
922         }
923         if ( ajdt )
924         {
925             sourceIncludes.add( ASPECTJ_FILE_PATTERN );
926         }
927 
928         if ( sourceExcludes == null )
929         {
930             sourceExcludes = new ArrayList();
931         }
932 
933         setupExtras();
934 
935         parseConfigurationOptions();
936 
937         
938         if ( projectnatures == null )
939         {
940             fillDefaultNatures( packaging );
941         }
942 
943         if ( additionalProjectnatures != null )
944         {
945             projectnatures.addAll( additionalProjectnatures );
946         }
947 
948         if ( buildcommands == null )
949         {
950             fillDefaultBuilders( packaging );
951         }
952         else
953         {
954             convertBuildCommandList( buildcommands );
955         }
956 
957         if ( additionalBuildcommands != null )
958         {
959             convertBuildCommandList( additionalBuildcommands );
960             buildcommands.addAll( additionalBuildcommands );
961         }
962 
963         if ( classpathContainers == null )
964         {
965             fillDefaultClasspathContainers( packaging );
966         }
967         else
968         {
969             verifyClasspathContainerListIsComplete();
970         }
971 
972         if ( linkedResources == null )
973         {
974             linkedResources = new ArrayList();
975         }
976 
977         locator.addSearchPath( FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath() );
978         locator.setOutputDirectory( new File( project.getBuild().getDirectory() ) );
979 
980         
981         return ready;
982     }
983 
984     
985 
986 
987 
988 
989 
990     protected final void convertBuildCommandList( List commands )
991     {
992         if ( commands != null )
993         {
994             for ( ListIterator i = commands.listIterator(); i.hasNext(); )
995             {
996                 Object command = i.next();
997 
998                 if ( command instanceof String )
999                 {
1000                     command = new BuildCommand( (String) command );
1001                     i.set( command );
1002                 }
1003             }
1004         }
1005     }
1006 
1007     private void parseConfigurationOptions()
1008     {
1009         if ( "R7".equalsIgnoreCase( wtpversion ) ) 
1010         {
1011             wtpVersionFloat = 0.7f;
1012         }
1013         else if ( "1.0".equalsIgnoreCase( wtpversion ) ) 
1014         {
1015             wtpVersionFloat = 1.0f;
1016         }
1017         else if ( "1.5".equalsIgnoreCase( wtpversion ) ) 
1018         {
1019             wtpVersionFloat = 1.5f;
1020         }
1021         else if ( "2.0".equalsIgnoreCase( wtpversion ) ) 
1022         {
1023             wtpVersionFloat = 2.0f;
1024         }
1025         if ( !"none".equalsIgnoreCase( wtpversion ) )
1026         {
1027             getLog().info( Messages.getString( "EclipsePlugin.wtpversion", wtpversion ) );
1028         }
1029     }
1030 
1031     
1032 
1033 
1034 
1035 
1036 
1037 
1038     protected void setupExtras()
1039         throws MojoExecutionException
1040     {
1041         
1042     }
1043 
1044     private void verifyClasspathContainerListIsComplete()
1045     {
1046         boolean containsJREContainer = false;
1047         
1048         
1049         for ( Object classPathContainer : classpathContainers )
1050         {
1051             if ( classPathContainer != null
1052                 && classPathContainer.toString().startsWith( COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER ) )
1053             {
1054                 containsJREContainer = true;
1055                 break;
1056             }
1057         }
1058         if ( !containsJREContainer )
1059         {
1060             getLog().warn( Messages.getString( "EclipsePlugin.missingjrecontainer" ) ); 
1061             classpathContainers.add( 0, COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER );
1062         }
1063     }
1064 
1065     private boolean validate()
1066         throws MojoExecutionException
1067     {
1068         
1069         if ( Arrays.binarySearch( WTP_SUPPORTED_VERSIONS, wtpversion ) < 0 )
1070         {
1071             throw new MojoExecutionException(
1072                                               Messages.getString( "EclipsePlugin.unsupportedwtp", new Object[] { 
1073                                                                   wtpversion,
1074                                                                       StringUtils.join( WTP_SUPPORTED_VERSIONS, " " ) } ) ); 
1075         }
1076 
1077         assertNotEmpty( executedProject.getGroupId(), POM_ELT_GROUP_ID );
1078         assertNotEmpty( executedProject.getArtifactId(), POM_ELT_ARTIFACT_ID );
1079 
1080         if ( executedProject.getFile() == null || !executedProject.getFile().exists() )
1081         {
1082             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.missingpom" ) ); 
1083         }
1084 
1085         if ( "pom".equals( packaging ) && eclipseProjectDir == null ) 
1086         {
1087             getLog().info( Messages.getString( "EclipsePlugin.pompackaging" ) ); 
1088             return false;
1089         }
1090 
1091         if ( "eclipse-plugin".equals( packaging ) )
1092         {
1093             getLog().info( Messages.getString( "EclipsePlugin.pdepackaging" ) ); 
1094             return false;
1095         }
1096 
1097         if ( eclipseProjectDir == null )
1098         {
1099             eclipseProjectDir = executedProject.getFile().getParentFile();
1100         }
1101 
1102         if ( !eclipseProjectDir.exists() && !eclipseProjectDir.mkdirs() )
1103         {
1104             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantcreatedir", eclipseProjectDir ) ); 
1105         }
1106 
1107         if ( !eclipseProjectDir.equals( executedProject.getFile().getParentFile() ) )
1108         {
1109             if ( !eclipseProjectDir.isDirectory() )
1110             {
1111                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.notadir", eclipseProjectDir ) ); 
1112             }
1113             eclipseProjectDir = new File( eclipseProjectDir, executedProject.getArtifactId() );
1114             if ( !eclipseProjectDir.isDirectory() && !eclipseProjectDir.mkdirs() )
1115             {
1116                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantcreatedir", eclipseProjectDir ) ); 
1117             }
1118         }
1119 
1120         validateExtras();
1121 
1122         return true;
1123     }
1124 
1125     
1126 
1127 
1128 
1129 
1130 
1131 
1132     protected void validateExtras()
1133         throws MojoExecutionException
1134     {
1135         
1136     }
1137 
1138     private void checkDeprecations()
1139     {
1140         if ( eclipseDownloadSources )
1141         {
1142             
1143             getLog().warn( Messages.getString( "EclipsePlugin.deprecatedpar", new Object[] { 
1144                                                "eclipse.downloadSources", 
1145                                                    "downloadSources" } ) ); 
1146             downloadSources = true;
1147         }
1148 
1149         checkDeprecationsExtras();
1150     }
1151 
1152     
1153 
1154 
1155 
1156 
1157     protected void checkDeprecationsExtras()
1158     {
1159         
1160     }
1161 
1162     public final void writeConfiguration( IdeDependency[] deps )
1163         throws MojoExecutionException
1164     {
1165         EclipseWriterConfig config = createEclipseWriterConfig( deps );
1166 
1167         if ( wtpmanifest && isJavaProject() )
1168         {
1169             
1170             EclipseManifestWriter.addManifestResource( getLog(), config );
1171         }
1172         
1173         writeConfigurationExtras( config );
1174 
1175         if ( wtpVersionFloat == 0.7f )
1176         {
1177             new EclipseWtpmodulesWriter().init( getLog(), config ).write();
1178         }
1179 
1180         if ( wtpVersionFloat >= 1.0f )
1181         {
1182             new EclipseWtpFacetsWriter().init( getLog(), config ).write();
1183         }
1184         if ( wtpVersionFloat == 1.0f )
1185         {
1186             new EclipseWtpComponentWriter().init( getLog(), config ).write();
1187         }
1188         if ( wtpVersionFloat >= 1.5 )
1189         {
1190             new EclipseWtpComponent15Writer().init( getLog(), config ).write();
1191         }
1192 
1193         new EclipseSettingsWriter().init( getLog(), config ).write();
1194 
1195         if ( isJavaProject )
1196         {
1197             new EclipseClasspathWriter().init( getLog(), config ).write();
1198             if ( ajdt && ajdtVersion.equals( "1.4" ) )
1199             {
1200                 new EclipseAjdtWriter().init( getLog(), config ).write();
1201             }
1202         }
1203 
1204         if ( wtpapplicationxml )
1205         {
1206             new EclipseWtpApplicationXMLWriter().init( getLog(), config ).write();
1207         }
1208 
1209         
1210         
1211         
1212         
1213         new EclipseProjectWriter().init( getLog(), config ).write();
1214 
1215         writeAdditionalConfig();
1216 
1217         getLog().info( Messages.getString( "EclipsePlugin.wrote", new Object[] { 
1218                                            config.getEclipseProjectName(), eclipseProjectDir.getAbsolutePath() } ) );
1219     }
1220 
1221     private void writeAdditionalConfig()
1222         throws MojoExecutionException
1223     {
1224         if ( additionalConfig != null )
1225         {
1226             for ( EclipseConfigFile file : additionalConfig )
1227             {
1228                 File projectRelativeFile = new File( eclipseProjectDir, file.getName() );
1229                 if ( projectRelativeFile.isDirectory() )
1230                 {
1231                     
1232                     getLog().warn( Messages.getString( "EclipsePlugin.foundadir", 
1233                                                        projectRelativeFile.getAbsolutePath() ) );
1234                 }
1235 
1236                 try
1237                 {
1238                     projectRelativeFile.getParentFile().mkdirs();
1239                     if ( file.getContent() == null )
1240                     {
1241                         if ( file.getLocation() != null )
1242                         {
1243                             InputStream inStream = locator.getResourceAsInputStream( file.getLocation() );
1244                             OutputStream outStream = new FileOutputStream( projectRelativeFile );
1245                             try
1246                             {
1247                                 IOUtil.copy( inStream, outStream );
1248                             }
1249                             finally
1250                             {
1251                                 IOUtil.close( inStream );
1252                                 IOUtil.close( outStream );
1253                             }
1254                         }
1255                         else
1256                         {
1257                             URL url = file.getURL();
1258                             String endPointUrl = url.getProtocol() + "://" + url.getAuthority();
1259                             
1260                             Repository repository = new Repository( "additonal-configs", endPointUrl );
1261                             Wagon wagon = wagonManager.getWagon( repository );
1262                             if ( logger.isDebugEnabled() )
1263                             {
1264                                 Debug debug = new Debug();
1265                                 wagon.addSessionListener( debug );
1266                                 wagon.addTransferListener( debug );
1267                             }
1268                             wagon.setTimeout( 1000 );
1269                             Settings settings = mavenSettingsBuilder.buildSettings();
1270                             ProxyInfo proxyInfo = null;
1271                             if ( settings != null && settings.getActiveProxy() != null )
1272                             {
1273                                 Proxy settingsProxy = settings.getActiveProxy();
1274 
1275                                 proxyInfo = new ProxyInfo();
1276                                 proxyInfo.setHost( settingsProxy.getHost() );
1277                                 proxyInfo.setType( settingsProxy.getProtocol() );
1278                                 proxyInfo.setPort( settingsProxy.getPort() );
1279                                 proxyInfo.setNonProxyHosts( settingsProxy.getNonProxyHosts() );
1280                                 proxyInfo.setUserName( settingsProxy.getUsername() );
1281                                 proxyInfo.setPassword( settingsProxy.getPassword() );
1282                             }
1283 
1284                             if ( proxyInfo != null )
1285                             {
1286                                 wagon.connect( repository, wagonManager.getAuthenticationInfo( repository.getId() ),
1287                                                proxyInfo );
1288                             }
1289                             else
1290                             {
1291                                 wagon.connect( repository, wagonManager.getAuthenticationInfo( repository.getId() ) );
1292                             }
1293 
1294                             wagon.get( url.getPath(), projectRelativeFile );
1295                         }
1296                     }
1297                     else
1298                     {
1299                         FileUtils.fileWrite( projectRelativeFile.getAbsolutePath(), file.getContent() );
1300                     }
1301                 }
1302                 catch ( WagonException e )
1303                 {
1304                     throw new MojoExecutionException(
1305                                                       Messages.getString( "EclipsePlugin.remoteexception", 
1306                                                                           new Object[] { file.getURL(), e.getMessage() } ) );
1307                 }
1308                 catch ( IOException e )
1309                 {
1310                     throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantwritetofile", 
1311                                                                           projectRelativeFile.getAbsolutePath() ) );
1312                 }
1313                 catch ( ResourceNotFoundException e )
1314                 {
1315                     throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantfindresource", 
1316                                                                           file.getLocation() ) );
1317                 }
1318                 catch ( XmlPullParserException e )
1319                 {
1320                     throw new MojoExecutionException( Messages.getString( "EclipsePlugin.settingsxmlfailure", 
1321                                                                           e.getMessage() ) );
1322                 }
1323             }
1324         }
1325     }
1326 
1327     
1328 
1329 
1330 
1331 
1332 
1333 
1334     protected final EclipseWriterConfig createEclipseWriterConfig( IdeDependency[] deps )
1335         throws MojoExecutionException
1336     {
1337         File projectBaseDir = executedProject.getFile().getParentFile();
1338 
1339         
1340         
1341         EclipseSourceDir[] sourceDirs = buildDirectoryList( executedProject, eclipseProjectDir, buildOutputDirectory );
1342 
1343         EclipseWriterConfig config = new EclipseWriterConfig();
1344 
1345         config.setWorkspaceConfiguration( getWorkspaceConfiguration() );
1346 
1347         config.setProjectNameTemplate( getProjectNameTemplate() );
1348 
1349         String projectName = IdeUtils.getProjectName( config.getProjectNameTemplate(), project );
1350 
1351         config.setEclipseProjectName( projectName );
1352 
1353         config.setWtpapplicationxml( wtpapplicationxml );
1354 
1355         config.setWtpVersion( wtpVersionFloat );
1356 
1357         float ajdtVersionFloat;
1358         try
1359         {
1360             ajdtVersionFloat = Float.parseFloat( ajdtVersion );
1361         }
1362         catch ( NumberFormatException e )
1363         {
1364             ajdtVersionFloat = 0.0f;
1365         }
1366 
1367         config.setAjdtVersion( ajdtVersionFloat );
1368 
1369         Set convertedBuildCommands = new LinkedHashSet();
1370 
1371         if ( buildcommands != null )
1372         {
1373             for ( Object cmd : buildcommands )
1374             {
1375                 if ( cmd instanceof BuildCommand )
1376                 {
1377                     convertedBuildCommands.add( cmd );
1378                 }
1379                 else
1380                 {
1381                     convertedBuildCommands.add( new BuildCommand( (String) cmd ) );
1382                 }
1383             }
1384         }
1385 
1386         if ( ajdt )
1387         {
1388             buildAjdtWeaveDeps( deps );
1389             buildAspectjDeps( deps );
1390         }
1391 
1392         config.setBuildCommands( new LinkedList( convertedBuildCommands ) );
1393 
1394         config.setBuildOutputDirectory( buildOutputDirectory );
1395         config.setClasspathContainers( classpathContainers );
1396         config.setDeps( deps );
1397         config.setEclipseProjectDirectory( eclipseProjectDir );
1398         config.setLocalRepository( localRepository );
1399         config.setOSGIManifestFile( manifest );
1400         config.setProject( project );
1401         config.setProjectBaseDir( projectBaseDir );
1402         config.setProjectnatures( projectnatures );
1403         config.setProjectFacets( additionalProjectFacets );
1404         config.setSourceDirs( sourceDirs );
1405         config.setPackaging( packaging );
1406         config.setLinkedResources( linkedResources );
1407         config.setClasspathContainersLast( classpathContainersLast );
1408         config.setJeeVersion( jeeversion );
1409 
1410         collectWarContextRootsFromReactorEarConfiguration( config );
1411 
1412         return config;
1413     }
1414 
1415     
1416 
1417 
1418 
1419 
1420 
1421     private void collectWarContextRootsFromReactorEarConfiguration( EclipseWriterConfig config )
1422     {
1423         if ( reactorProjects != null && wtpContextName == null
1424             && Constants.PROJECT_PACKAGING_WAR.equals( project.getPackaging() ) )
1425         {
1426             for ( Object reactorProject1 : reactorProjects )
1427             {
1428                 MavenProject reactorProject = (MavenProject) reactorProject1;
1429 
1430                 if ( Constants.PROJECT_PACKAGING_EAR.equals( reactorProject.getPackaging() ) )
1431                 {
1432                     Xpp3Dom[] warDefinitions =
1433                         IdeUtils.getPluginConfigurationDom( reactorProject, JeeUtils.ARTIFACT_MAVEN_EAR_PLUGIN,
1434                                                             new String[] { "modules", "webModule" } );
1435                     for ( Xpp3Dom warDefinition : warDefinitions )
1436                     {
1437                         Xpp3Dom groupId = warDefinition.getChild( "groupId" );
1438                         Xpp3Dom artifactId = warDefinition.getChild( "artifactId" );
1439                         Xpp3Dom contextRoot = warDefinition.getChild( "contextRoot" );
1440                         if ( groupId != null && artifactId != null && contextRoot != null && groupId.getValue() != null
1441                             && artifactId.getValue() != null && contextRoot.getValue() != null )
1442                         {
1443                             getLog().info( "Found context root definition for " + groupId.getValue() + ":"
1444                                                + artifactId.getValue() + " " + contextRoot.getValue() );
1445                             if ( project.getArtifactId().equals( artifactId.getValue() )
1446                                 && project.getGroupId().equals( groupId.getValue() ) )
1447                             {
1448                                 config.setContextName( contextRoot.getValue() );
1449                             }
1450                         }
1451                         else
1452                         {
1453                             getLog().info( "Found incomplete ear configuration in " + reactorProject.getGroupId() + ":"
1454                                                + reactorProject.getGroupId() + " found " + warDefinition.toString() );
1455                         }
1456                     }
1457                 }
1458             }
1459         }
1460         if ( config.getContextName() == null && Constants.PROJECT_PACKAGING_WAR.equals( project.getPackaging() ) )
1461         {
1462             if ( wtpContextName == null )
1463             {
1464                 config.setContextName( project.getArtifactId() );
1465             }
1466             else if ( "ROOT".equals( wtpContextName ) )
1467             {
1468                 config.setContextName( "" );
1469             }
1470             else
1471             {
1472                 config.setContextName( wtpContextName );
1473             }
1474         }
1475     }
1476 
1477     
1478 
1479 
1480 
1481 
1482 
1483 
1484 
1485     protected void writeConfigurationExtras( EclipseWriterConfig config )
1486         throws MojoExecutionException
1487     {
1488         
1489     }
1490 
1491     private void assertNotEmpty( String string, String elementName )
1492         throws MojoExecutionException
1493     {
1494         if ( string == null )
1495         {
1496             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.missingelement", elementName ) ); 
1497         }
1498     }
1499 
1500     
1501 
1502 
1503 
1504 
1505 
1506 
1507 
1508     protected void fillDefaultNatures( String packaging )
1509     {
1510         projectnatures = new ArrayList();
1511 
1512         if ( wtpVersionFloat >= 1.0f )
1513         {
1514             projectnatures.add( NATURE_WST_FACET_CORE_NATURE ); 
1515         }
1516 
1517         if ( isJavaProject )
1518         {
1519             if ( ajdt )
1520             {
1521                 projectnatures.add( NATURE_AJDT_CORE_JAVA );
1522             }
1523 
1524             projectnatures.add( NATURE_JDT_CORE_JAVA );
1525         }
1526 
1527         if ( wtpVersionFloat >= 0.7f )
1528         {
1529             projectnatures.add( NATURE_WST_MODULE_CORE_NATURE ); 
1530 
1531             if ( isJavaProject )
1532             {
1533                 projectnatures.add( NATURE_JEM_WORKBENCH_JAVA_EMF ); 
1534             }
1535         }
1536 
1537     }
1538 
1539     
1540 
1541 
1542 
1543 
1544 
1545 
1546 
1547     protected void fillDefaultClasspathContainers( String packaging )
1548     {
1549         classpathContainers = new ArrayList();
1550 
1551         if ( getWorkspaceConfiguration().getDefaultClasspathContainer() != null )
1552         {
1553             getLog().info( "Adding default classpath container: "
1554                                + getWorkspaceConfiguration().getDefaultClasspathContainer() );
1555             classpathContainers.add( getWorkspaceConfiguration().getDefaultClasspathContainer() );
1556         }
1557 
1558         if ( ajdt )
1559         {
1560             classpathContainers.add( ASPECTJ_RT_CONTAINER );
1561         }
1562     }
1563 
1564     
1565 
1566 
1567 
1568 
1569 
1570 
1571 
1572     protected void fillDefaultBuilders( String packaging )
1573     {
1574         buildcommands = new ArrayList();
1575 
1576         if ( wtpVersionFloat == 0.7f )
1577         {
1578             buildcommands.add( new BuildCommand( BUILDER_WST_COMPONENT_STRUCTURAL ) ); 
1579         }
1580 
1581         if ( isJavaProject )
1582         {
1583             if ( ajdt )
1584             {
1585                 buildcommands.add( new BuildCommand( BUILDER_AJDT_CORE_JAVA ) );
1586             }
1587             else
1588             {
1589                 buildcommands.add( new BuildCommand( BUILDER_JDT_CORE_JAVA ) );
1590             }
1591         }
1592 
1593         if ( wtpVersionFloat >= 1.5f )
1594         {
1595             buildcommands.add( new BuildCommand( BUILDER_WST_FACET ) ); 
1596         }
1597 
1598         if ( wtpVersionFloat >= 0.7f )
1599         {
1600             buildcommands.add( new BuildCommand( BUILDER_WST_VALIDATION ) ); 
1601         }
1602 
1603         if ( wtpVersionFloat == 0.7f )
1604         {
1605             
1606             buildcommands.add( new BuildCommand( BUILDER_WST_COMPONENT_STRUCTURAL_DEPENDENCY_RESOLVER ) );
1607         }
1608     }
1609 
1610     public final EclipseSourceDir[] buildDirectoryList( MavenProject project, File basedir, File buildOutputDirectory )
1611         throws MojoExecutionException
1612     {
1613         File projectBaseDir = project.getFile().getParentFile();
1614 
1615         String mainOutput = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, buildOutputDirectory, false );
1616 
1617         
1618         String testOutput = null;
1619         boolean useStandardOutputDir =
1620             buildOutputDirectory.equals( new File( project.getBuild().getOutputDirectory() ) );
1621         if ( useStandardOutputDir )
1622         {
1623             getLog().debug( "testOutput toRelativeAndFixSeparator " + projectBaseDir + " , "
1624                                 + project.getBuild().getTestOutputDirectory() );
1625             testOutput =
1626                 IdeUtils.toRelativeAndFixSeparator( projectBaseDir,
1627                                                     new File( project.getBuild().getTestOutputDirectory() ), false );
1628             getLog().debug( "testOutput after toRelative : " + testOutput );
1629         }
1630 
1631         Set mainDirectories = new LinkedHashSet();
1632 
1633         extractSourceDirs( mainDirectories, project.getCompileSourceRoots(), basedir, projectBaseDir, false, null );
1634 
1635         extractResourceDirs( mainDirectories, project.getBuild().getResources(), basedir, projectBaseDir, false,
1636                              mainOutput );
1637 
1638         Set testDirectories = new LinkedHashSet();
1639 
1640         extractSourceDirs( testDirectories, project.getTestCompileSourceRoots(), basedir, projectBaseDir, true,
1641                            testOutput );
1642 
1643         extractResourceDirs( testDirectories, project.getBuild().getTestResources(), basedir, projectBaseDir, true,
1644                              testOutput );
1645 
1646         
1647         Set directories = new LinkedHashSet();
1648 
1649         
1650         boolean testBeforeMain = isMavenVersion( "[2.0.8,)" );
1651 
1652         
1653         if ( testSourcesLast )
1654         {
1655             testBeforeMain = false;
1656         }
1657 
1658         if ( testBeforeMain )
1659         {
1660             directories.addAll( testDirectories );
1661             directories.removeAll( mainDirectories );
1662             directories.addAll( mainDirectories );
1663         }
1664         else
1665         {
1666             directories.addAll( mainDirectories );
1667             directories.addAll( testDirectories );
1668         }
1669         if ( ajdt )
1670             extractAspectDirs( directories, project, basedir, projectBaseDir, testOutput );
1671         return (EclipseSourceDir[]) directories.toArray( new EclipseSourceDir[directories.size()] );
1672     }
1673 
1674     private void extractSourceDirs( Set directories, List sourceRoots, File basedir, File projectBaseDir, boolean test,
1675                                     String output )
1676         throws MojoExecutionException
1677     {
1678         for ( Object sourceRoot1 : sourceRoots )
1679         {
1680 
1681             File sourceRootFile = new File( (String) sourceRoot1 );
1682 
1683             if ( sourceRootFile.isDirectory() )
1684             {
1685                 String sourceRoot =
1686                     IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRootFile,
1687                                                         !projectBaseDir.equals( basedir ) );
1688 
1689                 directories.add( new EclipseSourceDir( sourceRoot, output, false, test, sourceIncludes, sourceExcludes,
1690                                                        false ) );
1691             }
1692         }
1693     }
1694 
1695     final void extractResourceDirs( Set directories, List resources, File basedir, File workspaceProjectBaseDir,
1696                                     boolean test, final String output )
1697         throws MojoExecutionException
1698     {
1699         for ( Object resource1 : resources )
1700         {
1701             Resource resource = (Resource) resource1;
1702 
1703             getLog().debug( "Processing resource dir: " + resource.getDirectory() );
1704 
1705             List excludes = new ArrayList( resource.getExcludes() );
1706             
1707             
1708             excludes.add( JAVA_FILE_PATTERN );
1709 
1710             
1711             
1712 
1713             File resourceDirectory = new File( resource.getDirectory() );
1714 
1715             if ( !resourceDirectory.exists() || !resourceDirectory.isDirectory() )
1716             {
1717                 getLog().debug( "Resource dir: " + resourceDirectory + " either missing or not a directory." );
1718                 continue;
1719             }
1720 
1721             String resourcePath =
1722                 IdeUtils.toRelativeAndFixSeparator( workspaceProjectBaseDir, resourceDirectory,
1723                                                     !workspaceProjectBaseDir.equals( basedir ) );
1724             String thisOutput = output;
1725             if ( thisOutput != null )
1726             {
1727                 
1728                 File outputFile = new File( thisOutput );
1729                 if ( !outputFile.isAbsolute() )
1730                 {
1731                     outputFile = new File( workspaceProjectBaseDir, thisOutput );
1732                 }
1733                 
1734                 outputFile.mkdirs();
1735 
1736                 if ( !StringUtils.isEmpty( resource.getTargetPath() ) )
1737                 {
1738                     outputFile = new File( outputFile, resource.getTargetPath() );
1739                     
1740                     outputFile.mkdirs();
1741                 }
1742 
1743                 getLog().debug( "Making relative and fixing separator: { " + workspaceProjectBaseDir + ", "
1744                                     + outputFile + ", false }." );
1745                 thisOutput = IdeUtils.toRelativeAndFixSeparator( workspaceProjectBaseDir, outputFile, false );
1746             }
1747 
1748             EclipseSourceDir resourceDir =
1749                 new EclipseSourceDir( resourcePath, thisOutput, true, test, resource.getIncludes(), excludes,
1750                                       resource.isFiltering() );
1751 
1752             if ( !directories.add( resourceDir ) )
1753             {
1754                 EclipseSourceDir originalDir = (EclipseSourceDir) get( directories, resourceDir );
1755 
1756                 boolean merged = originalDir.merge( resourceDir );
1757                 if ( merged )
1758                 {
1759                     getLog().info( "Resource directory's path matches an existing source directory. Resources have been merged with the source directory "
1760                                        + originalDir.getPath() );
1761                 }
1762                 else
1763                 {
1764                     getLog().info( "Resource directory's path matches an existing source directory but \"test\", \"filtering\" or \"output\" were different."
1765                                        + "The resulting eclipse configuration may not accurately reflect the project configuration for "
1766                                        + originalDir.getPath() );
1767                 }
1768 
1769             }
1770         }
1771     }
1772 
1773     
1774 
1775 
1776 
1777 
1778 
1779 
1780 
1781     private Object get( Set set, Object o )
1782     {
1783         for ( Object item : set )
1784         {
1785             if ( o.equals( item ) )
1786             {
1787                 return item;
1788             }
1789         }
1790         return null;
1791     }
1792 
1793     private void extractAspectDirs( Set directories, MavenProject project, File basedir, File projectBaseDir,
1794                                     String testOutput )
1795         throws MojoExecutionException
1796     {
1797         Xpp3Dom configuration = getAspectjConfiguration( project );
1798         if ( configuration != null )
1799         {
1800             String aspectDirectory = DEFAULT_ASPECT_DIRECTORY;
1801             Xpp3Dom aspectDirectoryElement = configuration.getChild( ASPECT_DIRECTORY );
1802             if ( aspectDirectoryElement != null )
1803             {
1804                 aspectDirectory = aspectDirectoryElement.getValue();
1805             }
1806 
1807             File aspectDirectoryFile = new File( basedir, aspectDirectory );
1808             if ( aspectDirectoryFile.exists() && aspectDirectoryFile.isDirectory() )
1809             {
1810                 String sourceRoot =
1811                     IdeUtils.toRelativeAndFixSeparator( projectBaseDir, aspectDirectoryFile,
1812                                                         !projectBaseDir.equals( basedir ) );
1813 
1814                 directories.add( new EclipseSourceDir( sourceRoot, null, false, false, sourceIncludes, sourceExcludes,
1815                                                        false ) );
1816             }
1817 
1818             String testAspectDirectory = DEFAULT_TEST_ASPECT_DIRECTORY;
1819             Xpp3Dom testAspectDirectoryElement = configuration.getChild( TEST_ASPECT_DIRECTORY );
1820             if ( testAspectDirectoryElement != null )
1821             {
1822                 testAspectDirectory = testAspectDirectoryElement.getValue();
1823             }
1824 
1825             File testAspectDirectoryFile = new File( basedir, testAspectDirectory );
1826             if ( testAspectDirectoryFile.exists() && testAspectDirectoryFile.isDirectory() )
1827             {
1828                 String sourceRoot =
1829                     IdeUtils.toRelativeAndFixSeparator( projectBaseDir, testAspectDirectoryFile,
1830                                                         !projectBaseDir.equals( basedir ) );
1831 
1832                 directories.add( new EclipseSourceDir( sourceRoot, testOutput, false, true, sourceIncludes,
1833                                                        sourceExcludes, false ) );
1834             }
1835         }
1836     }
1837 
1838     private boolean enableAjdt( MavenProject project )
1839     {
1840         boolean enable = false;
1841         List buildPlugins = project.getBuildPlugins();
1842         for ( Object buildPlugin : buildPlugins )
1843         {
1844             Plugin plugin = (Plugin) buildPlugin;
1845             if ( plugin.getGroupId().equals( ORG_CODEHAUS_MOJO )
1846                 && plugin.getArtifactId().equals( ASPECTJ_MAVEN_PLUGIN ) )
1847             {
1848                 enable = true;
1849                 break;
1850             }
1851         }
1852 
1853         return enable;
1854     }
1855 
1856     private Xpp3Dom getAspectjConfiguration( MavenProject project )
1857     {
1858         Xpp3Dom configuration = null;
1859         List buildPlugins = project.getBuildPlugins();
1860         for ( Object buildPlugin : buildPlugins )
1861         {
1862             Plugin plugin = (Plugin) buildPlugin;
1863             if ( plugin.getGroupId().equals( ORG_CODEHAUS_MOJO )
1864                 && plugin.getArtifactId().equals( ASPECTJ_MAVEN_PLUGIN ) )
1865             {
1866                 configuration = (Xpp3Dom) plugin.getConfiguration();
1867                 break;
1868             }
1869         }
1870 
1871         return configuration;
1872     }
1873 
1874     private void buildAspectjDeps( IdeDependency[] deps )
1875         throws MojoExecutionException
1876     {
1877         Xpp3Dom configuration = getAspectjConfiguration( executedProject );
1878         if ( configuration != null )
1879         {
1880             Xpp3Dom aspectLibrariesParent = configuration.getChild( ASPECT_LIBRARIES );
1881             if ( aspectLibrariesParent != null )
1882             {
1883                 Xpp3Dom[] aspectLibraries = aspectLibrariesParent.getChildren( ASPECT_LIBRARY );
1884                 outerLoop: for ( Xpp3Dom aspectLibrary : aspectLibraries )
1885                 {
1886                     String artifactId = aspectLibrary.getChild( POM_ELT_ARTIFACT_ID ).getValue();
1887                     String groupId = aspectLibrary.getChild( POM_ELT_GROUP_ID ).getValue();
1888                     for ( IdeDependency dep : deps )
1889                     {
1890                         if ( dep.getArtifactId().equals( artifactId ) && dep.getGroupId().equals( groupId ) )
1891                         {
1892                             dep.setAjdtDependency( true );
1893                             continue outerLoop;
1894                         }
1895                     }
1896 
1897                     throw new MojoExecutionException( "AspectLibrary is not a dependency of project" );
1898                 }
1899             }
1900         }
1901     }
1902 
1903     private void buildAjdtWeaveDeps( IdeDependency[] deps )
1904         throws MojoExecutionException
1905     {
1906         Xpp3Dom configuration = getAspectjConfiguration( executedProject );
1907         if ( configuration != null )
1908         {
1909             Xpp3Dom weaveDependenciesParent = configuration.getChild( WEAVE_DEPENDENCIES );
1910             if ( weaveDependenciesParent != null )
1911             {
1912                 Xpp3Dom[] weaveDependencies = weaveDependenciesParent.getChildren( WEAVE_DEPENDENCY );
1913                 outerLoop: for ( Xpp3Dom weaveDependency : weaveDependencies )
1914                 {
1915                     String artifactId = weaveDependency.getChild( POM_ELT_ARTIFACT_ID ).getValue();
1916                     String groupId = weaveDependency.getChild( POM_ELT_GROUP_ID ).getValue();
1917                     for ( IdeDependency dep : deps )
1918                     {
1919                         if ( dep.getArtifactId().equals( artifactId ) && dep.getGroupId().equals( groupId ) )
1920                         {
1921                             dep.setAjdtWeaveDependency( true );
1922                             continue outerLoop;
1923                         }
1924                     }
1925 
1926                     throw new MojoExecutionException( "WeaveDependency is not a dependency of project" );
1927                 }
1928             }
1929         }
1930     }
1931 
1932     
1933 
1934 
1935     public String getProjectNameForArifact( Artifact artifact )
1936     {
1937         IdeDependency[] workspaceArtefacts = getWorkspaceArtefacts();
1938         for ( int index = 0; workspaceArtefacts != null && index < workspaceArtefacts.length; index++ )
1939         {
1940             IdeDependency workspaceArtefact = workspaceArtefacts[index];
1941             if ( workspaceArtefact.isAddedToClasspath()
1942                 && workspaceArtefact.getGroupId().equals( artifact.getGroupId() )
1943                 && workspaceArtefact.getArtifactId().equals( artifact.getArtifactId() ) )
1944             {
1945                 if ( workspaceArtefact.getVersion().equals( artifact.getBaseVersion() ) )
1946                 {
1947                     return workspaceArtefact.getEclipseProjectName();
1948                 }
1949             }
1950         }
1951         MavenProject reactorProject = getReactorProject( artifact );
1952         if ( reactorProject != null )
1953         {
1954             return IdeUtils.getProjectName( getProjectNameTemplateForMavenProject( reactorProject ), artifact );
1955         }
1956         return IdeUtils.getProjectName( getProjectNameTemplate(), artifact );
1957     }
1958 
1959     
1960 
1961 
1962 
1963     private String getProjectNameTemplateForMavenProject( MavenProject mavenProject )
1964     {
1965         String projectNameTemplate = null;
1966         boolean addVersionToProjectName = false;
1967         boolean addGroupIdToProjectName = false;
1968 
1969         Build build = mavenProject.getBuild();
1970         if ( build != null )
1971         {
1972             String eclipsePlugin = "org.apache.maven.plugins:maven-eclipse-plugin";
1973             Plugin plugin = (Plugin) build.getPluginsAsMap().get( eclipsePlugin );
1974             if ( plugin == null && build.getPluginManagement() != null )
1975             {
1976                 plugin = (Plugin) build.getPluginManagement().getPluginsAsMap().get( eclipsePlugin );
1977             }
1978             if ( plugin != null )
1979             {
1980                 Xpp3Dom config = (Xpp3Dom) plugin.getConfiguration();
1981                 if ( config != null )
1982                 {
1983                     Xpp3Dom projectNameTemplateNode = config.getChild( "projectNameTemplate" );
1984                     if ( projectNameTemplateNode != null )
1985                     {
1986                         projectNameTemplate = projectNameTemplateNode.getValue();
1987                     }
1988                     Xpp3Dom addVersionToProjectNameNode = config.getChild( "addVersionToProjectName" );
1989                     addVersionToProjectName = addVersionToProjectNameNode != null;
1990                     Xpp3Dom addGroupIdToProjectNameNode = config.getChild( "addGroupIdToProjectName" );
1991                     addGroupIdToProjectName = addGroupIdToProjectNameNode != null;
1992                 }
1993             }
1994         }
1995         return IdeUtils.calculateProjectNameTemplate( projectNameTemplate, addVersionToProjectName,
1996                                                       addGroupIdToProjectName, getLog() );
1997     }
1998 
1999     
2000 
2001 
2002     protected final IdeDependency[] getWorkspaceArtefacts()
2003     {
2004         return getWorkspaceConfiguration().getWorkspaceArtefacts();
2005     }
2006 
2007     public final WorkspaceConfiguration getWorkspaceConfiguration()
2008     {
2009         if ( workspaceConfiguration == null )
2010         {
2011             workspaceConfiguration = new WorkspaceConfiguration();
2012             locateWorkspace();
2013             getLog().info( Messages.getString( "EclipsePlugin.workspace", workspace ) );
2014             workspaceConfiguration.setWorkspaceDirectory( workspace );
2015 
2016             new ReadWorkspaceLocations().init( getLog(), workspaceConfiguration, project, wtpdefaultserver,
2017                                                preferStandardClasspathContainer );
2018         }
2019         return workspaceConfiguration;
2020     }
2021 
2022     
2023 
2024 
2025     private void locateWorkspace()
2026     {
2027         if ( workspace == null )
2028         {
2029             File currentWorkingDirectory = new File( "." ).getAbsoluteFile();
2030             while ( currentWorkingDirectory != null )
2031             {
2032                 File metadataDirectory = new File( currentWorkingDirectory, ".metadata" );
2033                 logger.debug( "Checking for eclipse workspace at " + currentWorkingDirectory );
2034                 if ( metadataDirectory.exists() && metadataDirectory.isDirectory() )
2035                 {
2036                     logger.debug( "  Found workspace at " + currentWorkingDirectory );
2037                     workspace = currentWorkingDirectory;
2038                     return;
2039                 }
2040                 currentWorkingDirectory = currentWorkingDirectory.getParentFile();
2041             }
2042         }
2043     }
2044 
2045     public final List getExcludes()
2046     {
2047         return excludes;
2048     }
2049 
2050     
2051 
2052 
2053 
2054 
2055 
2056     protected boolean isAvailableAsAReactorProject( Artifact artifact )
2057     {
2058         MavenProject project = getReactorProject( artifact );
2059         return ( project != null && new File( project.getBasedir(), ".project" ).exists() );
2060     }
2061 
2062     
2063 
2064 
2065 
2066 
2067 
2068     private boolean isAvailableAsAWorkspaceProject( Artifact artifact )
2069     {
2070         IdeDependency[] workspaceArtefacts = getWorkspaceArtefacts();
2071         for ( int index = 0; workspaceArtefacts != null && index < workspaceArtefacts.length; index++ )
2072         {
2073             IdeDependency workspaceArtefact = workspaceArtefacts[index];
2074             if ( workspaceArtefact.getGroupId().equals( artifact.getGroupId() )
2075                 && workspaceArtefact.getArtifactId().equals( artifact.getArtifactId() ) )
2076             {
2077                 if ( workspaceArtefact.getVersion().equals( artifact.getBaseVersion() ) )
2078                 {
2079                     workspaceArtefact.setAddedToClasspath( true );
2080                     getLog().debug( "Using workspace project: " + workspaceArtefact.getEclipseProjectName() );
2081                     return true;
2082                 }
2083                 else
2084                 {
2085                     getLog().info( "Artifact "
2086                                        + artifact.getId()
2087                                        + " already available as a workspace project, but with different version. Expected: "
2088                                        + artifact.getBaseVersion() + ", found: " + workspaceArtefact.getVersion() );
2089                 }
2090             }
2091         }
2092         return false;
2093     }
2094 
2095     
2096 
2097 
2098 
2099 
2100 
2101     protected final boolean hasToResolveJar( Artifact art )
2102     {
2103         return !( getUseProjectReferences() && isAvailableAsAReactorProject( art ) )
2104             || ( limitProjectReferencesToWorkspace && !( getUseProjectReferences() && isAvailableAsAWorkspaceProject( art ) ) );
2105     }
2106 
2107     
2108 
2109 
2110 
2111 
2112 
2113     protected final boolean useProjectReference( Artifact art )
2114     {
2115         boolean isReactorProject = getUseProjectReferences() && isAvailableAsAReactorProject( art );
2116         boolean isWorkspaceProject = getUseProjectReferences() && isAvailableAsAWorkspaceProject( art );
2117         return ( isReactorProject && !limitProjectReferencesToWorkspace ) || 
2118             ( limitProjectReferencesToWorkspace && isWorkspaceProject ) || 
2119             ( !isReactorProject && isWorkspaceProject ); 
2120     }
2121 }