java.util.jar.Manifest Scala Examples
The following examples show how to use java.util.jar.Manifest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: DarManifestReader.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf package archive import java.io.InputStream import java.util.jar.{Attributes, Manifest} import scala.util.{Failure, Success, Try} object DarManifestReader { private val supportedFormat = "daml-lf" def dalfNames(is: InputStream): Try[Dar[String]] = { val manifest = new Manifest(is) val attributes = value(manifest.getMainAttributes) _ for { mainDalf <- attributes("Main-Dalf") allDalfs <- attributes("Dalfs") format <- attributes("Format") _ <- checkFormat(format) } yield Dar(mainDalf, dependencies(allDalfs, mainDalf)) } private def dependencies(other: String, main: String): List[String] = { val deps = other.split(',').view.map(_.trim) deps.filter(x => x != main).toList } private def value(attributes: Attributes)(key: String): Try[String] = Option(attributes.getValue(key)) match { case None => failure(s"Cannot find attribute: $key") case Some(x) => Success(x.trim) } private def checkFormat(format: String): Try[Unit] = if (format == supportedFormat) Success(()) else failure(s"Unsupported format: $format") private def failure(msg: String) = Failure(DarManifestReaderException(msg)) case class DarManifestReaderException(msg: String) extends IllegalStateException(msg) }
Example 2
Source File: ManifestJarGenerator.scala From coursier with Apache License 2.0 | 5 votes |
package coursier.launcher import java.io.{ByteArrayOutputStream, OutputStream} import java.nio.file.{Files, Path} import java.util.jar.{Attributes, JarOutputStream, Manifest} import coursier.launcher.internal.FileUtil object ManifestJarGenerator extends Generator[Parameters.ManifestJar] { def generate(parameters: Parameters.ManifestJar, output: Path): Unit = { val cp = parameters.classpath.map(_.toURI.getRawPath).mkString(" ") val manifest = new Manifest val attr = manifest.getMainAttributes attr.put(Attributes.Name.MANIFEST_VERSION, "1.0") attr.put(Attributes.Name.CLASS_PATH, cp) attr.put(Attributes.Name.MAIN_CLASS, parameters.mainClass) val content = { val baos = new ByteArrayOutputStream val jos = new JarOutputStream(baos, manifest) jos.close() baos.close() baos.toByteArray() } FileUtil.withOutputStream(output) { os => for (p <- parameters.preambleOpt.map(_.value)) os.write(p) os.write(content) } FileUtil.tryMakeExecutable(output) } }
Example 3
Source File: Package.scala From seed with Apache License 2.0 | 5 votes |
package seed.generation import java.io.{File, FileInputStream, OutputStream} import java.util.jar.{Attributes, JarEntry, JarOutputStream, Manifest} import org.apache.commons.io.IOUtils import java.nio.file.Path import seed.Log import seed.cli.util.Ansi import scala.collection.mutable // Adapted from https://stackoverflow.com/a/1281295 object Package { def create( source: List[(Path, String)], target: OutputStream, mainClass: Option[String], classPath: List[String], log: Log ): Unit = { val manifest = new Manifest() val mainAttributes = manifest.getMainAttributes mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0") // TODO Set additional package fields: https://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html mainClass.foreach( cls => mainAttributes.put(Attributes.Name.MAIN_CLASS, cls) ) if (classPath.nonEmpty) mainAttributes.put(Attributes.Name.CLASS_PATH, classPath.mkString(" ")) val targetFile = new JarOutputStream(target, manifest) val entryCache = mutable.Set[String]() source.foreach { case (path, jarPath) => log.debug(s"Packaging ${Ansi.italic(path.toString)}...") add(path.toFile, jarPath, targetFile, entryCache, log) } targetFile.close() } def add( source: File, jarPath: String, target: JarOutputStream, entryCache: mutable.Set[String], log: Log ): Unit = { val path = if (source.isFile) jarPath else { require(!jarPath.endsWith("/")) jarPath + "/" } val addedEntry = if (entryCache.contains(path)) { if (source.isFile) log.warn( s"Skipping file ${Ansi.italic(source.toString)} as another module already added it" ) false } else { val entry = new JarEntry(path) entry.setTime(source.lastModified) target.putNextEntry(entry) entryCache += path if (source.isFile) IOUtils.copy(new FileInputStream(source), target) true } if (!source.isFile) for (nestedFile <- source.listFiles) add(nestedFile, path + nestedFile.getName, target, entryCache, log) if (addedEntry) target.closeEntry() } }
Example 4
Source File: MarathonBuildInfo.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome import java.util.jar.{Attributes, Manifest} import mesosphere.marathon.SemVer import mesosphere.marathon.io.IO import mesosphere.marathon.stream.Implicits._ import scala.Predef._ import scala.util.control.NonFatal case object MarathonBuildInfo { private val marathonJar = """\/mesosphere\/marathon\/marathon_2.12\/[0-9.]+""".r val DefaultBuildVersion = SemVer(1, 7, 0, Some("SNAPSHOT")) private lazy val marathonManifestPath: List[java.net.URL] = getClass() .getClassLoader() .getResources("META-INF/MANIFEST.MF") .toIterator .filter { manifest => marathonJar.findFirstMatchIn(manifest.getPath).nonEmpty } .toList lazy val manifest: Option[Manifest] = marathonManifestPath match { case Nil => None case List(file) => val mf = new Manifest() IO.using(file.openStream) { f => mf.read(f) Some(mf) } case otherwise => throw new RuntimeException(s"Multiple marathon JAR manifests returned! ${otherwise}") } lazy val attributes: Option[Attributes] = manifest.map(_.getMainAttributes()) def getAttribute(name: String): Option[String] = attributes.flatMap { attrs => try { Option(attrs.getValue(name)) } catch { case NonFatal(_) => None } } lazy val name: String = getAttribute("Implementation-Title").getOrElse("unknown") // IntelliJ has its own manifest.mf that will inject a version that doesn't necessarily match // our actual version. This can cause Migrations to fail since the version number doesn't correctly match up. lazy val version: SemVer = getAttribute("Implementation-Version").filterNot(_ == "0.1-SNAPSHOT").map(SemVer(_)).getOrElse(DefaultBuildVersion) lazy val scalaVersion: String = getAttribute("Scala-Version").getOrElse("2.x.x") lazy val buildref: String = getAttribute("Git-Commit").getOrElse("unknown") override val toString: String = { "name: %s, version: %s, scalaVersion: %s, buildref: %s" format (name, version, scalaVersion, buildref) } }
Example 5
Source File: MetronomeBuildInfo.scala From metronome with Apache License 2.0 | 5 votes |
package dcos.metronome import java.util.jar.{Attributes, Manifest} import mesosphere.marathon.io.IO import scala.collection.JavaConverters._ import scala.io.Source import scala.util.control.NonFatal import scala.util.{Failure, Success, Try} lazy val manifestPath: List[java.net.URL] = getClass.getClassLoader .getResources("META-INF/MANIFEST.MF") .asScala .filter { manifest => metronomeJar.findFirstMatchIn(manifest.getPath).nonEmpty } .toList lazy val manifest: Option[Manifest] = manifestPath match { case Nil => None case List(file) => val mf = new Manifest() IO.using(file.openStream) { f => mf.read(f) Some(mf) } case otherwise => throw new RuntimeException(s"Multiple metronome JAR manifests returned! $otherwise") } lazy val attributes: Option[Attributes] = manifest.map(_.getMainAttributes()) def getAttribute(name: String): Option[String] = attributes.flatMap { attrs => try { Option(attrs.getValue(name)) } catch { case NonFatal(_) => None } } // IntelliJ has its own manifest.mf that will inject a version that doesn't necessarily match // our actual version. This can cause Migrations to fail since the version number doesn't correctly match up. lazy val version: String = getAttribute("Implementation-Version").getOrElse(devBuildVersion) lazy val scalaVersion: String = getAttribute("Scala-Version").getOrElse("2.x.x") lazy val marathonVersion: mesosphere.marathon.SemVer = MarathonBuildInfo.version }
Example 6
Source File: ContainerInfo.scala From service-container with Apache License 2.0 | 5 votes |
package com.github.vonnagy.service.container.health import java.net.InetAddress import java.util.jar.Attributes.Name import java.util.jar.{Attributes, JarFile, Manifest} import com.github.vonnagy.service.container.log.LoggingAdapter private[health] def getMainClass: Option[Class[_]] = { import scala.collection.JavaConverters._ def checkStack(elem: StackTraceElement): Option[Class[_]] = try { if (elem.getMethodName.equals("main")) Some(Class.forName(elem.getClassName)) else None } catch { case _: ClassNotFoundException => { // Swallow the exception None } } Thread.getAllStackTraces.asScala.values.flatMap(currStack => { if (!currStack.isEmpty) checkStack(currStack.last) else None }).headOption match { case None => sys.props.get("sun.java.command") match { case Some(command) if !command.isEmpty => try { Some(Class.forName(command)) } catch { // Swallow the exception case _: ClassNotFoundException => None } // Nothing could be located case _ => None } case c => c } } private[health] def getManifest(clazz: Class[_]): Manifest = { val file: String = clazz.getProtectionDomain.getCodeSource.getLocation.getFile try { if (file.endsWith(".jar")) { new JarFile(file).getManifest } else { val manifest: Manifest = new Manifest manifest.getMainAttributes.put(Name.IMPLEMENTATION_TITLE, "Container Service") manifest.getMainAttributes.put(Name.IMPLEMENTATION_VERSION, "1.0.0") manifest.getMainAttributes.put(new Attributes.Name("Implementation-Build"), "N/A") manifest } } catch { case _: Exception => { val manifest: Manifest = new Manifest manifest.getMainAttributes.put(Name.IMPLEMENTATION_TITLE, "Container Service") manifest.getMainAttributes.put(Name.IMPLEMENTATION_VERSION, "1.0.0") manifest.getMainAttributes.put(new Attributes.Name("Implementation-Build"), "N/A") manifest } } } }
Example 7
Source File: MetaDataProvider.scala From releaser with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.releaser import java.nio.file.Path import java.util.jar.Manifest import java.util.zip.ZipFile import org.joda.time.DateTime import org.joda.time.format.DateTimeFormat import uk.gov.hmrc.releaser.github.CommitSha import scala.collection.JavaConversions._ import scala.io.Source import scala.util.{Failure, Success, Try} trait MetaDataProvider { def fromJarFile(p: Path): Try[ArtefactMetaData] def fromCommitManifest(p: Path): Try[ArtefactMetaData] } case class ArtefactMetaData(sha:CommitSha, commitAuthor:String, commitDate:DateTime) class ArtefactMetaDataProvider extends MetaDataProvider { import ArtefactMetaDataProvider._ def fromJarFile(p: Path): Try[ArtefactMetaData] = { Try {new ZipFile(p.toFile) }.flatMap { jarFile => jarFile.entries().filter(_.getName == "META-INF/MANIFEST.MF").toList.headOption.map { ze => val man = new Manifest(jarFile.getInputStream(ze)) ArtefactMetaData( man.getMainAttributes.getValue("Git-Head-Rev"), man.getMainAttributes.getValue("Git-Commit-Author"), gitCommitDateFormat.parseDateTime(man.getMainAttributes.getValue("Git-Commit-Date")) ) }.toTry(new Exception(s"Failed to retrieve manifest from $p")) } } def fromCommitManifest(p: Path): Try[ArtefactMetaData] = { Try { val map = Source.fromFile(p.toFile) .getLines().toSeq .map(_.split("=")) .map { case Array(key, value) => key.trim -> value.trim }.toMap ArtefactMetaData(map("sha"), map("author"), gitCommitDateFormat.parseDateTime(map("date"))) } } } object ArtefactMetaDataProvider { val gitCommitDateFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ") implicit class OptionPimp[A](opt: Option[A]){ def toTry(e:Exception):Try[A] = opt match { case Some(x) => Success(x) case None => Failure(e) } } }
Example 8
Source File: Launcher.scala From PackUpdate with Apache License 2.0 | 5 votes |
package at.chaosfield.packupdate.server import java.io.File import java.net.{URL, URLClassLoader} import java.util.jar.{Attributes, JarFile, Manifest} import scala.collection.JavaConverters._ object Launcher { def launchServer(file: File, args: Array[String]): Unit = { val manifest = new JarFile(file).getManifest val cp = manifest.getMainAttributes.getValue("Class-Path").split(' ').map(path => new File("libraries", path).toURI.toURL) val loader = new URLClassLoader(cp ++ Array(file.toURI.toURL)) val klass = Class.forName(manifest.getMainAttributes.getValue("Main-Class"), true, loader) val m = klass.getDeclaredMethod("main", classOf[Array[String]]) m.invoke(null, args) } }