mirror of https://github.com/sbt/sbt.git
Merge pull request #205 from alexarchambault/topic/find-main-class
Better lookup of main class by launch command
This commit is contained in:
commit
caf69da816
|
|
@ -2,6 +2,8 @@ package coursier
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import java.io.{ OutputStreamWriter, File }
|
import java.io.{ OutputStreamWriter, File }
|
||||||
|
import java.net.URL
|
||||||
|
import java.util.jar.{ Manifest => JManifest }
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
import coursier.ivy.IvyRepository
|
import coursier.ivy.IvyRepository
|
||||||
|
|
@ -15,17 +17,27 @@ object Helper {
|
||||||
|
|
||||||
def errPrintln(s: String) = Console.err.println(s)
|
def errPrintln(s: String) = Console.err.println(s)
|
||||||
|
|
||||||
|
private val manifestPath = "META-INF/MANIFEST.MF"
|
||||||
|
|
||||||
def mainClasses(cl: ClassLoader): Map[(String, String), String] = {
|
def mainClasses(cl: ClassLoader): Map[(String, String), String] = {
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
|
|
||||||
val metaInfs = cl.getResources("META-INF/MANIFEST.MF").asScala.toVector
|
val parentMetaInfs = Option(cl.getParent).fold(Set.empty[URL]) { parent =>
|
||||||
|
parent.getResources(manifestPath).asScala.toSet
|
||||||
|
}
|
||||||
|
val allMetaInfs = cl.getResources(manifestPath).asScala.toVector
|
||||||
|
|
||||||
|
val metaInfs = allMetaInfs.filterNot(parentMetaInfs)
|
||||||
|
|
||||||
val mainClasses = metaInfs.flatMap { url =>
|
val mainClasses = metaInfs.flatMap { url =>
|
||||||
val attributes = new java.util.jar.Manifest(url.openStream()).getMainAttributes
|
val attributes = new JManifest(url.openStream()).getMainAttributes
|
||||||
|
|
||||||
val vendor = Option(attributes.getValue("Specification-Vendor")).getOrElse("")
|
def attributeOpt(name: String) =
|
||||||
val title = Option(attributes.getValue("Specification-Title")).getOrElse("")
|
Option(attributes.getValue(name))
|
||||||
val mainClass = Option(attributes.getValue("Main-Class"))
|
|
||||||
|
val vendor = attributeOpt("Specification-Vendor").getOrElse("")
|
||||||
|
val title = attributeOpt("Specification-Title").getOrElse("")
|
||||||
|
val mainClass = attributeOpt("Main-Class")
|
||||||
|
|
||||||
mainClass.map((vendor, title) -> _)
|
mainClass.map((vendor, title) -> _)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,13 @@ case class Launch(
|
||||||
else {
|
else {
|
||||||
val mainClasses = Helper.mainClasses(loader)
|
val mainClasses = Helper.mainClasses(loader)
|
||||||
|
|
||||||
|
if (options.common.verbosityLevel >= 2) {
|
||||||
|
Console.err.println("Found main classes:")
|
||||||
|
for (((vendor, title), mainClass) <- mainClasses)
|
||||||
|
Console.err.println(s" $mainClass (vendor: $vendor, title: $title)")
|
||||||
|
Console.err.println("")
|
||||||
|
}
|
||||||
|
|
||||||
val mainClass =
|
val mainClass =
|
||||||
if (mainClasses.isEmpty) {
|
if (mainClasses.isEmpty) {
|
||||||
Helper.errPrintln("No main class found. Specify one with -M or --main.")
|
Helper.errPrintln("No main class found. Specify one with -M or --main.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue