/**
  * @param dependencyMavenProjectFacade
  * @param monitor
  * @return
  * @throws CoreException
  */
 protected IProject preConfigureDependencyProject(
     IMavenProjectFacade dependencyMavenProjectFacade, IProgressMonitor monitor)
     throws CoreException {
   IProject dependency = dependencyMavenProjectFacade.getProject();
   MavenProject mavenDependency = dependencyMavenProjectFacade.getMavenProject(monitor);
   String depPackaging = dependencyMavenProjectFacade.getPackaging();
   // jee dependency has not been configured yet - i.e. it has no JEE facet-
   if (JEEPackaging.isJEEPackaging(depPackaging) && !WTPProjectsUtil.isJavaEEProject(dependency)) {
     IProjectConfiguratorDelegate delegate =
         ProjectConfiguratorDelegateFactory.getProjectConfiguratorDelegate(
             dependencyMavenProjectFacade.getPackaging());
     if (delegate != null) {
       // Lets install the proper facets
       try {
         delegate.configureProject(dependency, mavenDependency, monitor);
       } catch (MarkedException ex) {
         // Markers already have been created for this exception, no more to do.
         return dependency;
       }
     }
   } else {
     // XXX Probably should create a UtilProjectConfiguratorDelegate
     configureWtpUtil(dependency, mavenDependency, monitor);
   }
   return dependency;
 }
  protected List<IMavenProjectFacade> getWorkspaceDependencies(
      IProject project, MavenProject mavenProject) {
    Set<IProject> projects = new HashSet<IProject>();
    List<IMavenProjectFacade> dependencies = new ArrayList<IMavenProjectFacade>();
    Set<Artifact> artifacts = mavenProject.getArtifacts();
    for (Artifact artifact : artifacts) {
      IMavenProjectFacade dependency =
          projectManager.getMavenProject(
              artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());

      if ((Artifact.SCOPE_COMPILE.equals(artifact.getScope())
              || Artifact.SCOPE_RUNTIME.equals(
                  artifact.getScope())) // MNGECLIPSE-1578 Runtime dependencies should be deployed
          && dependency != null
          && !dependency.getProject().equals(project)
          && dependency.getFullPath(artifact.getFile()) != null
          && projects.add(dependency.getProject())) {
        dependencies.add(dependency);
      }
    }
    return dependencies;
  }
  public void setModuleDependencies(
      IProject project, MavenProject mavenProject, IProgressMonitor monitor) throws CoreException {
    IVirtualComponent component = ComponentCore.createComponent(project);
    // if the attempt to create dependencies happens before the project is actually created, abort.
    // this will be created again when the project exists.
    if (component == null) {
      return;
    }

    WarPluginConfiguration config = new WarPluginConfiguration(mavenProject, project);
    WarPackagingOptions opts = new WarPackagingOptions(config);

    List<AbstractDependencyConfigurator> depConfigurators =
        ExtensionReader.readDependencyConfiguratorExtensions(
            projectManager,
            MavenPlugin.getDefault().getMavenRuntimeManager(),
            mavenMarkerManager,
            MavenPlugin.getDefault().getConsole());

    Set<IVirtualReference> references = new LinkedHashSet<IVirtualReference>();
    List<IMavenProjectFacade> exportedDependencies =
        getWorkspaceDependencies(project, mavenProject);
    for (IMavenProjectFacade dependency : exportedDependencies) {
      String depPackaging = dependency.getPackaging();
      if ("pom".equals(depPackaging))
        continue; // MNGECLIPSE-744 pom dependencies shouldn't be deployed

      preConfigureDependencyProject(dependency, monitor);
      MavenProject depMavenProject = dependency.getMavenProject(monitor);

      IVirtualComponent depComponent = ComponentCore.createComponent(dependency.getProject());

      String artifactKey = ArtifactUtils.versionlessKey(depMavenProject.getArtifact());
      Artifact artifact = mavenProject.getArtifactMap().get(artifactKey);
      // in a skinny war the dependency modules are referenced by manifest classpath
      // see also <code>configureClasspath</code> the dependeny project is handled in the skinny
      // case
      if (opts.isSkinnyWar()
          && opts.isReferenceFromEar(depComponent, artifact.getArtifactHandler().getExtension())) {
        continue;
      }

      // an artifact in mavenProject.getArtifacts() doesn't have the "optional" value as
      // depMavenProject.getArtifact();
      if (!artifact.isOptional()) {
        IVirtualReference reference = ComponentCore.createReference(component, depComponent);
        reference.setRuntimePath(new Path("/WEB-INF/lib"));
        references.add(reference);
      }
    }

    IVirtualReference[] newRefs = references.toArray(new IVirtualReference[references.size()]);
    if (hasChanged(component.getReferences(), newRefs)) {
      // Only write in the .component file if necessary
      component.setReferences(newRefs);
    }

    // TODO why a 2nd loop???
    for (IMavenProjectFacade dependency : exportedDependencies) {
      MavenProject depMavenProject = dependency.getMavenProject(monitor);
      Iterator<AbstractDependencyConfigurator> configurators = depConfigurators.iterator();
      while (configurators.hasNext()) {
        try {
          configurators
              .next()
              .configureDependency(
                  mavenProject, project, depMavenProject, dependency.getProject(), monitor);
        } catch (MarkedException ex) {
          // XXX handle this
        }
      }
    }
  }