コード例 #1
0
  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);
  }
コード例 #2
0
  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);
      }
    }
  }