JarUtils

sbt.internal.inc.JarUtils
object JarUtils

This is a utility class that provides a set of functions that are used to implement straight to jar compilation.

xsbt.JarUtils is a class that has similar purpose and duplicates some of the code, as it is difficult to share it.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
JarUtils.type

Members list

Type members

Classlikes

final class ClassInJar(val toString: String) extends AnyVal

ClassInJar is an identifier for a class located inside a jar. For plain class files it is enough to simply use the actual file system path. A class in a jar is identified as a path to the jar and path to the class within that jar (RelClass). Those two values are held in one string separated by !. Slashes in both paths are consistent with File.separatorChar as the actual string is usually kept in File object.

ClassInJar is an identifier for a class located inside a jar. For plain class files it is enough to simply use the actual file system path. A class in a jar is identified as a path to the jar and path to the class within that jar (RelClass). Those two values are held in one string separated by !. Slashes in both paths are consistent with File.separatorChar as the actual string is usually kept in File object.

As an example: given a jar file "C:\develop\zinc\target\output.jar" and a relative path to the class "sbt/internal/inc/Compile.class" The resulting identifier would be: "C:\develop\zinc\target\output.jar!sbt\internal\inc\Compile.class"

Attributes

Companion
object
Supertypes
class AnyVal
trait Matchable
class Any
object ClassInJar

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
ClassInJar.type
sealed abstract class OutputJarContent

Attributes

Supertypes
class Object
trait Matchable
class Any

Types

type ClassFilePath = String

Represents a path to a class file located inside a jar, relative to this jar

Represents a path to a class file located inside a jar, relative to this jar

Attributes

Value members

Concrete methods

def cleanupPreviousJar(prevJar: File, outputJar: File): Unit

Performs cleanup after successful compilation that involved previous jar. It merges the previous jar with the new output and puts the merged file back into output jar path.

Performs cleanup after successful compilation that involved previous jar. It merges the previous jar with the new output and puts the merged file back into output jar path.

Attributes

The returned OutputJarContent object provides access to current content of output jar. It is prepared to be accessed from zinc's custom compiler phases. With some assumptions on how it works the content can be cached and read only when necessary.

The returned OutputJarContent object provides access to current content of output jar. It is prepared to be accessed from zinc's custom compiler phases. With some assumptions on how it works the content can be cached and read only when necessary.

Implementation details: The content has to be reset before each zinc run. This sets the output and reads the current contents of output jar if it exists. The next reading will be necessary in xsbt-analyzer phase which is after jvm phase, so the jar with new contents will appear. To figure out that it is in that place, a call to dependencyPhaseCompleted is expected. As content is not accessed between dependency and analysis phases, we can be sure that we are after jvm. The contents of jar will not change until next scalac run (except for javac case) so we should not read the content. This is ensured by a call to scalacRunCompleted method. After scalac run, it is possible that javac will run, and its output will be added to the output jar. To have consistent state, after adding those classes addClasses should be called. Thanks to this we know what is the content of prev jar during the compilation, without the need to actually read it. The completion of next dependency phase will trigger reading the output jar again. Note that at the point of reading we have both prev jar and new output jar with just compiled classes so the contents of those (currently stored and just read) have bo be combined. Last thing to do is track class deletions while pruning between iterations, which is done through removeClasses method.

Attributes

def exists(jc: ClassInJar): Boolean

Checks if given jared class exists

Checks if given jared class exists

Attributes

def getJarInClassInJar(path: Path): Option[Path]

Return JAR component of class-in-jar notation.

Return JAR component of class-in-jar notation.

Attributes

def getOutputJar(output: Output): Option[Path]

Extracts a jar file from the output if it is set to be a single jar.

Extracts a jar file from the output if it is set to be a single jar.

Attributes

def includeInJar(jar: File, files: Seq[(File, ClassFilePath)]): Unit

Adds plain files to specified jar file. See sbt.internal.inc.IndexBasedZipOps#includeInArchive for details.

Adds plain files to specified jar file. See sbt.internal.inc.IndexBasedZipOps#includeInArchive for details.

Attributes

def isClassInJar(file: File): Boolean

Checks if given file stores a ClassInJar

Checks if given file stores a ClassInJar

Attributes

def isCompilingToJar(output: Output): Boolean

Determines if Straight to Jar compilations is enabled by inspecting if compilation output is a jar file

Determines if Straight to Jar compilations is enabled by inspecting if compilation output is a jar file

Attributes

def javacTempOutput(outputJar: Path): Path

As some javac implementations do not support compiling directly to jar it is required to change its output to a directory that is temporary, as after compilation the plain classes are put into a zip file and merged with the output jar.

As some javac implementations do not support compiling directly to jar it is required to change its output to a directory that is temporary, as after compilation the plain classes are put into a zip file and merged with the output jar.

This method returns path to this directory based on output jar. The result of this method has to be deterministic as it is called from different places independently.

Attributes

def listClassFiles(jar: File): Seq[String]

Lists class file entries in jar e.g. sbt/internal/inc/JarUtils.class

Lists class file entries in jar e.g. sbt/internal/inc/JarUtils.class

Attributes

def listFiles(jar: Path): Seq[String]

Lists file entries in jar e.g. sbt/internal/inc/JarUtils.class

Lists file entries in jar e.g. sbt/internal/inc/JarUtils.class

Attributes

def mergeJars(into: File, from: File): Unit

Merges contents of two jars. See sbt.internal.inc.IndexBasedZipOps#mergeArchives for details.

Merges contents of two jars. See sbt.internal.inc.IndexBasedZipOps#mergeArchives for details.

Attributes

def preparePreviousJar(output: Output): Option[(File, File)]

If compilation to jar is enabled and previous jar existed will prepare the prev jar, i.e. move the existing output to temporary location. It will return tuple of the path to moved prev jar and path to output jar. The returned prev jar file should be added to the classpath of the compiler.

If compilation to jar is enabled and previous jar existed will prepare the prev jar, i.e. move the existing output to temporary location. It will return tuple of the path to moved prev jar and path to output jar. The returned prev jar file should be added to the classpath of the compiler.

Attributes

def readModifiedTime(jc: ClassInJar): Long

Reads timestamp of given jared class

Reads timestamp of given jared class

Attributes

def readStamps(jar: Path): Path => Long

Reads all timestamps from given jar file. Returns a function that allows to access them by ClassInJar wrapped in File.

Reads all timestamps from given jar file. Returns a function that allows to access them by ClassInJar wrapped in File.

Attributes

def removeFromJar(jarFile: Path, classes: Iterable[ClassFilePath]): Unit

Removes specified entries from a jar file.

Removes specified entries from a jar file.

Attributes

def setupTempClassesDir(temporaryClassesDirectory: Option[Path]): Unit

Ensures that temporary directory exists.

Ensures that temporary directory exists.

Value parameters

temporaryClassesDirectory

path to temporary directory for classes. If not specified, a default will be used.

Attributes

def stashIndex(jar: Path): CentralDir

Reads current index of a jar file to allow restoring it later with unstashIndex

Reads current index of a jar file to allow restoring it later with unstashIndex

Attributes

def unstashIndex(jar: Path, index: CentralDir): Unit

Replaces index in given jar file with specified one

Replaces index in given jar file with specified one

Attributes

def withPreviousJar(output: Output)(compile: Seq[Path] => Unit): Unit

Runs the compilation with previous jar if required.

Runs the compilation with previous jar if required.

When compiling directly to a jar, scalac will produce a jar file, if one exists it will be overwritten rather than updated. For sake of incremental compilation it is required to merge the output from previous compilation(s) and the current one. To make it work, the jar output from previous compilation is stored aside (renamed) to avoid overwrite. The compilation is run normally to the specified output jar. The produced output jar is then merged with jar from previous compilation(s).

Classes from previous jar need to be available for the current compiler run - they need to be added to the classpath. This is implemented by taking a function that given additional classpath runs the compilation.

If compilation fails, it does not produce a jar, the previous jar is simply reverted (moved to output jar path).

If the previous output does not exist or the output is not a jar at all (JarUtils feature is disabled) this function runs a normal compilation.

Value parameters

callback

analysis callback used to set previus jar

compile

function that given extra classpath for compiler runs the compilation

output

output for scalac compilation

Attributes

Concrete fields

Options that have to be specified when running javac in order for Straight to Jar to work properly.

Options that have to be specified when running javac in order for Straight to Jar to work properly.

-XDuseOptimizedZip=false holds jars open that causes problems with locks on Windows.

Attributes

val prevJarPrefix: String

Options that have to be specified when running scalac in order for Straight to Jar to work properly.

Options that have to be specified when running scalac in order for Straight to Jar to work properly.

-YdisableFlatCpCaching is needed to disable caching the output jar that changes between compilation runs (incremental compilation cycles). Caching may hide those changes and lead into incorrect results.

Attributes