static void copyArtifactJarClasspath()

in appengine-plugins-core/src/main/java/com/google/cloud/tools/appengine/operations/AppYamlProjectStaging.java [244:277]


  static void copyArtifactJarClasspath(
      AppYamlProjectStageConfiguration config, CopyService copyService) throws IOException {
    Path artifact = config.getArtifact();
    Path targetDirectory = config.getStagingDirectory();
    try (JarFile jarFile = new JarFile(artifact.toFile())) {
      String jarClassPath =
          jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
      if (jarClassPath == null) {
        return;
      }
      Iterable<String> classpathEntries = Splitter.onPattern("\\s+").split(jarClassPath.trim());
      for (String classpathEntry : classpathEntries) {
        // classpath entries are relative to artifact's position and relativeness should be
        // preserved in the target directory
        Path jarSrc =
            artifact.getParent() == null ? null : artifact.getParent().resolve(classpathEntry);
        if (jarSrc == null || !Files.isRegularFile(jarSrc)) {
          log.warning("Could not copy 'Class-Path' jar: " + jarSrc + " referenced in MANIFEST.MF");
          continue;
        }
        Path jarTarget = targetDirectory.resolve(classpathEntry);

        if (Files.exists(jarTarget)) {
          log.fine(
              "Overwriting 'Class-Path' jar: "
                  + jarTarget
                  + " with "
                  + jarSrc
                  + " referenced in MANIFEST.MF");
        }
        copyService.copyFileAndReplace(jarSrc, jarTarget);
      }
    }
  }