public static void updateManifest(
      @NotNull final VirtualFile file,
      final @Nullable String mainClass,
      final @Nullable List<String> classpath,
      final boolean replaceValues) {
    final Manifest manifest = readManifest(file);
    final Attributes mainAttributes = manifest.getMainAttributes();

    if (mainClass != null) {
      mainAttributes.put(Attributes.Name.MAIN_CLASS, mainClass);
    } else if (replaceValues) {
      mainAttributes.remove(Attributes.Name.MAIN_CLASS);
    }

    if (classpath != null && !classpath.isEmpty()) {
      List<String> updatedClasspath;
      if (replaceValues) {
        updatedClasspath = classpath;
      } else {
        updatedClasspath = new ArrayList<String>();
        final String oldClasspath = (String) mainAttributes.get(Attributes.Name.CLASS_PATH);
        if (!StringUtil.isEmpty(oldClasspath)) {
          updatedClasspath.addAll(StringUtil.split(oldClasspath, " "));
        }
        for (String path : classpath) {
          if (!updatedClasspath.contains(path)) {
            updatedClasspath.add(path);
          }
        }
      }
      mainAttributes.put(Attributes.Name.CLASS_PATH, StringUtil.join(updatedClasspath, " "));
    } else if (replaceValues) {
      mainAttributes.remove(Attributes.Name.CLASS_PATH);
    }

    ManifestBuilder.setVersionAttribute(mainAttributes);

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                try {
                  final OutputStream outputStream = file.getOutputStream(ManifestFileUtil.class);
                  try {
                    manifest.write(outputStream);
                  } finally {
                    outputStream.close();
                  }
                } catch (IOException e) {
                  LOG.info(e);
                }
              }
            });
  }
示例#2
0
 /** @tests java.util.jar.Attributes#remove(java.lang.Object) */
 public void test_removeLjava_lang_Object() {
   a.remove(new Attributes.Name("1"));
   a.remove(new Attributes.Name("3"));
   assertNull("Should have been removed", a.getValue("1"));
   assertEquals("Should not have been removed", "four", a.getValue("4"));
 }