Skip to content

Commit

Permalink
Add jars from jar manifest Class-Path attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal Wojdyla committed Jul 25, 2016
1 parent 72b4a31 commit eaa0ced
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion scio-core/src/main/scala/com/spotify/scio/ScioContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.io.File
import java.net.{URI, URLClassLoader}
import java.nio.file.Files
import java.util.concurrent.TimeUnit
import java.util.jar.{Attributes, JarFile}

import com.google.api.services.bigquery.model.TableReference
import com.google.api.services.datastore.DatastoreV1.{Entity, Query}
Expand Down Expand Up @@ -209,11 +210,32 @@ class ScioContext private[scio] (val options: PipelineOptions,
val javaHome = new File(sys.props("java.home")).getCanonicalPath
val userDir = new File(sys.props("user.dir")).getCanonicalPath

classLoader.asInstanceOf[URLClassLoader]
val classPathJars = classLoader.asInstanceOf[URLClassLoader]
.getURLs
.map(url => new File(url.toURI).getCanonicalPath)
.filter(p => !p.startsWith(javaHome) && p != userDir)
.toList

// fetch jars from classpath jar's manifest Class-Path if present
val manifestJars = classPathJars
.map(p => (p, new JarFile(p).getManifest))
.filter { case (p, manifest) =>
manifest != null && manifest.getMainAttributes.containsKey(Attributes.Name.CLASS_PATH)}
.map { case (p, manifest) => (new File(p).getParentFile,
manifest.getMainAttributes.getValue(Attributes.Name.CLASS_PATH).split(" ")) }
.flatMap { case (parent, jars) => jars.map(jar =>
if (jar.startsWith("/")) {
jar // accept absolute path as is
} else {
new File(parent, jar).getCanonicalPath // relative path
})
}

logger.debug(s"Classpath jars: ${classPathJars.mkString(":")}")
logger.debug(s"Manifest jars: ${manifestJars.mkString(":")}")

// no need to care about duplicates here - should be solved by the SDK uploader
classPathJars ++ manifestJars
}

/** Compute list of local files to make available to workers. */
Expand Down

0 comments on commit eaa0ced

Please sign in to comment.