public void addLibraryDependency(
      MavenArtifact artifact,
      DependencyScope scope,
      MavenModifiableModelsProvider provider,
      MavenProject project) {
    String libraryName = artifact.getLibraryName();

    Library library = provider.getLibraryByName(libraryName);
    if (library == null) {
      library = provider.createLibrary(libraryName);
    }
    Library.ModifiableModel libraryModel = provider.getLibraryModel(library);

    updateUrl(libraryModel, OrderRootType.CLASSES, artifact, null, null, true);
    if (!MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope())) {
      updateUrl(
          libraryModel,
          OrderRootType.SOURCES,
          artifact,
          MavenExtraArtifactType.SOURCES,
          project,
          false);
      updateUrl(
          libraryModel,
          JavadocOrderRootType.getInstance(),
          artifact,
          MavenExtraArtifactType.DOCS,
          project,
          false);
    }

    LibraryOrderEntry e = myRootModel.addLibraryEntry(library);
    e.setScope(scope);
  }
  private void configDependencies() {
    for (MavenArtifact artifact : myMavenProject.getDependencies()) {
      if (!myMavenProject.isSupportedDependency(artifact, SupportedRequestType.FOR_IMPORT))
        continue;

      DependencyScope scope = selectScope(artifact.getScope());
      MavenProject depProject = myMavenTree.findProject(artifact.getMavenId());

      if (depProject != null) {
        if (depProject == myMavenProject) continue;
        boolean isTestJar =
            MavenConstants.TYPE_TEST_JAR.equals(artifact.getType())
                || "tests".equals(artifact.getClassifier());
        myRootModelAdapter.addModuleDependency(
            myMavenProjectToModuleName.get(depProject), scope, isTestJar);

        Element buildHelperCfg =
            depProject.getPluginGoalConfiguration(
                "org.codehaus.mojo", "build-helper-maven-plugin", "attach-artifact");
        if (buildHelperCfg != null) {
          addAttachArtifactDependency(buildHelperCfg, scope, depProject, artifact);
        }
      } else {
        myRootModelAdapter.addLibraryDependency(
            artifact, scope, myModifiableModelsProvider, myMavenProject);
      }
    }

    configSurefirePlugin();
  }
  @Nullable
  public static MavenArtifact findArtifact(
      @NotNull MavenProject project, @Nullable Library library) {
    if (library == null) return null;

    String name = library.getName();

    if (!MavenArtifact.isMavenLibrary(name)) return null;

    for (MavenArtifact each : project.getDependencies()) {
      if (each.getLibraryName().equals(name)) return each;
    }
    return null;
  }
  private static void updateUrl(
      Library.ModifiableModel library,
      OrderRootType type,
      MavenArtifact artifact,
      MavenExtraArtifactType artifactType,
      MavenProject project,
      boolean clearAll) {
    String classifier = null;
    String extension = null;

    if (artifactType != null) {
      Pair<String, String> result = project.getClassifierAndExtension(artifact, artifactType);
      classifier = result.first;
      extension = result.second;
    }

    String newPath = artifact.getPathForExtraArtifact(classifier, extension);
    String newUrl =
        VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, newPath)
            + JarFileSystem.JAR_SEPARATOR;
    for (String url : library.getUrls(type)) {
      if (newUrl.equals(url)) return;
      if (clearAll || isRepositoryUrl(artifact, url, classifier, extension)) {
        library.removeRoot(url, type);
      }
    }
    library.addRoot(newUrl, type);
  }
  private MavenRunnerParameters createBuildParameters(Location l) {
    final PsiElement element = l.getPsiElement();
    final Project project = l.getProject();

    final Module module = ModuleUtil.findModuleForPsiElement(element);
    if (module == null) return null;

    final MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(project);
    final MavenProject mavenProject = mavenProjectsManager.findProject(module);

    if (mavenProject == null) return null;

    // todo: check this code
    final List<MavenArtifact> dependencies = mavenProject.getDependencies();
    MavenArtifact artifact = null;
    for (MavenArtifact dependence : dependencies) {
      if (dependence.getArtifactId().equals(GROUP_ID_LIFT)) {
        artifact = dependence;
        break;
      } else if (dependence.getArtifactId().equals(ARTIFACT_ID_LIFT)) {
        artifact = dependence;
        break;
      }
    }
    // final MavenArtifact artifact = mavenProjectModel.findDependency(GROUP_ID_LIFT,
    // ARTIFACT_ID_LIFT);

    if (artifact == null) return null;

    mySourceElement = element;

    MavenExplicitProfiles profiles =
        MavenProjectsManager.getInstance(project).getExplicitProfiles();
    List<String> goals = new ArrayList<String>();

    goals.add(JETTY_RUN);

    final VirtualFile file = module.getModuleFile();
    if (file == null) return null;

    final VirtualFile parent = file.getParent();
    if (parent == null) return null;

    return new MavenRunnerParameters(true, parent.getPath(), goals, profiles);
  }
 @Nullable
 public static OrderEntry findLibraryEntry(@NotNull Module m, @NotNull MavenArtifact artifact) {
   String name = artifact.getLibraryName();
   for (OrderEntry each : ModuleRootManager.getInstance(m).getOrderEntries()) {
     if (each instanceof LibraryOrderEntry
         && name.equals(((LibraryOrderEntry) each).getLibraryName())) {
       return each;
     }
   }
   return null;
 }
  private void addAttachArtifactDependency(
      @NotNull Element buildHelperCfg,
      @NotNull DependencyScope scope,
      @NotNull MavenProject mavenProject,
      @NotNull MavenArtifact artifact) {
    Library.ModifiableModel libraryModel = null;

    for (Element artifactsElement : (List<Element>) buildHelperCfg.getChildren("artifacts")) {
      for (Element artifactElement : (List<Element>) artifactsElement.getChildren("artifact")) {
        String typeString = artifactElement.getChildTextTrim("type");
        if (typeString != null && !typeString.equals("jar")) continue;

        OrderRootType rootType = OrderRootType.CLASSES;

        String classifier = artifactElement.getChildTextTrim("classifier");
        if ("sources".equals(classifier)) {
          rootType = OrderRootType.SOURCES;
        } else if ("javadoc".equals(classifier)) {
          rootType = JavadocOrderRootType.getInstance();
        }

        String filePath = artifactElement.getChildTextTrim("file");
        if (StringUtil.isEmpty(filePath)) continue;

        VirtualFile file = VfsUtil.findRelativeFile(filePath, mavenProject.getDirectoryFile());
        if (file == null) continue;

        file = JarFileSystem.getInstance().getJarRootForLocalFile(file);
        if (file == null) continue;

        if (libraryModel == null) {
          String libraryName = artifact.getLibraryName();
          assert libraryName.startsWith(MavenArtifact.MAVEN_LIB_PREFIX);
          libraryName =
              MavenArtifact.MAVEN_LIB_PREFIX
                  + "ATTACHED-JAR: "
                  + libraryName.substring(MavenArtifact.MAVEN_LIB_PREFIX.length());

          Library library = myModifiableModelsProvider.getLibraryByName(libraryName);
          if (library == null) {
            library = myModifiableModelsProvider.createLibrary(libraryName);
          }
          libraryModel = myModifiableModelsProvider.getLibraryModel(library);

          LibraryOrderEntry entry = myRootModelAdapter.getRootModel().addLibraryEntry(library);
          entry.setScope(scope);
        }

        libraryModel.addRoot(file, rootType);
      }
    }
  }
 public Library findLibrary(@NotNull final MavenArtifact artifact) {
   final String name = artifact.getLibraryName();
   final Ref<Library> result = Ref.create(null);
   myRootModel
       .orderEntries()
       .forEachLibrary(
           new Processor<Library>() {
             @Override
             public boolean process(Library library) {
               if (name.equals(library.getName())) {
                 result.set(library);
               }
               return true;
             }
           });
   return result.get();
 }
 public static boolean isMavenLibrary(@Nullable Library library) {
   return library != null && MavenArtifact.isMavenLibrary(library.getName());
 }
 @Deprecated // Use artifact.getLibraryName();
 public static String makeLibraryName(@NotNull MavenArtifact artifact) {
   return artifact.getLibraryName();
 }
 private static boolean isRepositoryUrl(
     MavenArtifact artifact, String url, String classifier, String extension) {
   return url.endsWith(
       artifact.getRelativePathForExtraArtifact(classifier, extension)
           + JarFileSystem.JAR_SEPARATOR);
 }
 private void createExclusion(MavenArtifact artifactToExclude, MavenDomExclusions exclusions) {
   MavenDomExclusion exclusion = exclusions.addExclusion();
   exclusion.getGroupId().setValue(artifactToExclude.getGroupId());
   exclusion.getArtifactId().setValue(artifactToExclude.getArtifactId());
 }