Jar deployment, SBT hacking, and more related to launcher code.

Some refactoring, bugfixing, and self awareness of launcher jar.
This commit is contained in:
meeh
2018-05-01 11:15:11 +00:00
parent 345e7414e6
commit f6c8e44329
7 changed files with 95 additions and 33 deletions

View File

@ -1,8 +1,5 @@
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % "3.5.3"
)
assemblyExcludedJars in assembly := { assemblyExcludedJars in assembly := {
val donts = List( val donts = List(

View File

@ -2,10 +2,8 @@ package net.i2p
import java.io.{File, InputStream} import java.io.{File, InputStream}
//import net.i2p.Router
import net.i2p.launchers.DeployProfile import net.i2p.launchers.DeployProfile
import net.i2p.router.Router
import org.json4s._
import org.json4s.native.JsonMethods._
/** /**
* *
@ -104,7 +102,7 @@ object RouterLauncherApp extends App {
//ErrorUtils.printError(s"Starting up with arguments ${(args mkString ", ")}",":)") //ErrorUtils.printError(s"Starting up with arguments ${(args mkString ", ")}",":)")
Router.main(args) //Router.main(args)
} }

View File

@ -31,7 +31,10 @@ lazy val browserbundle = (project in file("browserbundle"))
commonSettings, commonSettings,
name := "RouterLaunchApp", name := "RouterLaunchApp",
assemblyJarName in assembly := s"${name.value}-${version.value}.jar", assemblyJarName in assembly := s"${name.value}-${version.value}.jar",
mainClass in assembly := Some("net.i2p.RouterLauncherApp") mainClass in assembly := Some("net.i2p.RouterLauncherApp"),
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % "3.5.3"
)
).dependsOn(common) ).dependsOn(common)
lazy val macosx = (project in file("macosx")) lazy val macosx = (project in file("macosx"))
@ -52,3 +55,4 @@ fork := true
run / javaOptions += "-Xmx512M" run / javaOptions += "-Xmx512M"
run / connectInput := true run / connectInput := true

View File

@ -1,7 +1,13 @@
package net.i2p.launchers package net.i2p.launchers
import java.io.{File, IOException} import java.io.{File, IOException}
import java.nio.file.FileAlreadyExistsException
import java.util.zip.ZipFile import java.util.zip.ZipFile
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files.copy
import java.nio.file.Paths.get
import collection.JavaConverters._ import collection.JavaConverters._
/** /**
@ -120,14 +126,19 @@ class OSXDeployment extends
*/ */
def copyDirFromRes(dir: File): Unit = { def copyDirFromRes(dir: File): Unit = {
// A small hack // A small hack
val zipFile = new ZipFile(DeployProfile.executingJarFile.getFile) try {
zipFile.entries().asScala.toList.filter(_.toString.startsWith(dir.getPath)).filter(!_.isDirectory).map { entry => val zipFile = new ZipFile(DeployProfile.executingJarFile.getFile)
new File(DeployProfile.pathJoin(baseDir,entry.getName)).getParentFile.mkdirs() zipFile.entries().asScala.toList.filter(_.toString.startsWith(dir.getPath)).filter(!_.isDirectory).map { entry =>
if (entry.isDirectory) { new File(DeployProfile.pathJoin(baseDir,entry.getName)).getParentFile.mkdirs()
createFileOrDirectory(new File(DeployProfile.pathJoin(baseDir,entry.getName)), true) if (entry.isDirectory) {
} else { createFileOrDirectory(new File(DeployProfile.pathJoin(baseDir,entry.getName)), true)
copyBaseFileResToDisk(entry.getName, getClass.getResourceAsStream("/".concat(entry.getName))) } else {
copyBaseFileResToDisk(entry.getName, getClass.getResourceAsStream("/".concat(entry.getName)))
}
} }
} catch {
case ex:IOException => println(s"Error! Exception ${ex}")
case _:FileAlreadyExistsException => {} // Ignored
} }
} }
@ -156,6 +167,7 @@ class OSXDeployment extends
} }
} catch { } catch {
case ex:IOException => println(s"Error! Exception ${ex}") case ex:IOException => println(s"Error! Exception ${ex}")
case _:FileAlreadyExistsException => {} // Ignored
} }
} }
} }
@ -164,6 +176,29 @@ class OSXDeployment extends
new File(baseDir).mkdirs() new File(baseDir).mkdirs()
} }
implicit def toPath (filename: String) = get(filename)
val selfFile = new File(DeployProfile.executingJarFile.getFile)
val selfDir = selfFile.getParentFile
val resDir = new File(selfDir.getParent, "Resources")
val i2pBaseBundleDir = new File(resDir, "i2pbase")
val i2pBundleJarDir = new File(i2pBaseBundleDir, "lib")
val i2pBaseDir = OSXDefaults.getOSXBaseDirectory
val i2pDeployJarDir = new File(i2pBaseDir, "lib")
if (!i2pDeployJarDir.exists()) {
i2pDeployJarDir.mkdirs()
i2pBundleJarDir.list().toList.map {
jar => {
copy (
DeployProfile.pathJoin(i2pBundleJarDir.getAbsolutePath, jar),
DeployProfile.pathJoin(i2pDeployJarDir.getAbsolutePath, jar),
REPLACE_EXISTING)
println(s"Copied ${jar} to right place")
}
}
}
/** /**
* Please note that in Scala, the constructor body is same as class body. * Please note that in Scala, the constructor body is same as class body.
* What's defined outside of methods is considered constructor code and * What's defined outside of methods is considered constructor code and
@ -184,12 +219,15 @@ class OSXDeployment extends
// Case subject is a file/resource // Case subject is a file/resource
case Left(is) => { case Left(is) => {
// Write file // Write file
if (!new File(DeployProfile.pathJoin(baseDir, fd.getPath)).exists()) { val f = DeployProfile.pathJoin(baseDir, fd.getPath)
println(s"f: ${f.toString}")
if (!new File(f).exists()) {
//println(s"copyBaseFileResToDisk(${fd.getPath})") //println(s"copyBaseFileResToDisk(${fd.getPath})")
try { try {
copyBaseFileResToDisk(fd.getPath, is) copyBaseFileResToDisk(fd.getPath, is)
} catch { } catch {
case ex:IOException => println(s"Error! Exception ${ex}") case ex:IOException => println(s"Error! Exception ${ex}")
case _:FileAlreadyExistsException => {} // Ignored
} }
} }
} }

View File

@ -0,0 +1,18 @@
package net.i2p.launchers
import java.net.URL
/**
* A abstract class is kind of like an java interface.
*
* @author Meeh
* @since 0.9.35
*/
abstract class RouterLauncher {
def getClassLoader: ClassLoader
def addJarToClassPath(url: URL): Boolean
def runRouter(args: Array[String]): Unit
}

View File

@ -31,24 +31,17 @@ lazy val jarsForCopy = i2pBuildDir.list.filter { f => f.endsWith(".jar") }
// Pointing the resources directory to the "installer" directory // Pointing the resources directory to the "installer" directory
resourceDirectory in Compile := baseDirectory.value / ".." / ".." / "installer" / "resources" resourceDirectory in Compile := baseDirectory.value / ".." / ".." / "installer" / "resources"
// Unmanaged base will be included in a fat jar
unmanagedBase in Compile := baseDirectory.value / ".." / ".." / "build"
// Unmanaged classpath will be available at compile time // Unmanaged classpath will be available at compile time
unmanagedClasspath in Compile ++= Seq( unmanagedClasspath in Compile ++= Seq(
baseDirectory.value / ".." / ".." / "build" / "*.jar", baseDirectory.value / ".." / ".." / "build" / "*.jar"
baseDirectory.value / ".." / ".." / "router" / "java" / "src"
) )
// Please note the difference between browserbundle, this has
// the "in Compile" which limit it's scope to that.
//unmanagedBase in Compile := baseDirectory.value / ".." / ".." / "build"
libraryDependencies ++= Seq(
"net.i2p" % "router" % i2pVersion % Compile
)
//assemblyOption in assembly := (assemblyOption in assembly).value.copy(prependShellScript = Some(defaultShellScript)) //assemblyOption in assembly := (assemblyOption in assembly).value.copy(prependShellScript = Some(defaultShellScript))
assemblyJarName in assembly := s"${name.value}-${version.value}" assemblyJarName in assembly := s"OSXLauncher"
// TODO: MEEH: Add assemblyExcludedJars and load the router from own jar files, to handle upgrades better. // TODO: MEEH: Add assemblyExcludedJars and load the router from own jar files, to handle upgrades better.
@ -71,6 +64,10 @@ buildAppBundleTask := {
paths.map { case (s,p) => p.mkdirs() } paths.map { case (s,p) => p.mkdirs() }
val dirsToCopy = List("certificates","locale","man") val dirsToCopy = List("certificates","locale","man")
val launcherBinary = Some(assembly.value)
launcherBinary.map { l => IO.copyFile( new File(l.toString), new File(paths.get("execBundlePath").get, "I2P") ) }
/** /**
* *
* First of, if "map" is unknown for you - shame on you :p * First of, if "map" is unknown for you - shame on you :p
@ -85,5 +82,5 @@ buildAppBundleTask := {
*/ */
dirsToCopy.map { d => IO.copyDirectory( new File(resDir, d), new File(paths.get("i2pbaseBunldePath").get, d) ) } dirsToCopy.map { d => IO.copyDirectory( new File(resDir, d), new File(paths.get("i2pbaseBunldePath").get, d) ) }
warsForCopy.map { w => IO.copyFile( new File(i2pBuildDir, w), new File(paths.get("webappsBunldePath").get, w) ) } warsForCopy.map { w => IO.copyFile( new File(i2pBuildDir, w), new File(paths.get("webappsBunldePath").get, w) ) }
warsForCopy.map { j => IO.copyFile( new File(i2pBuildDir, j), new File(paths.get("i2pJarsBunldePath").get, j) ) } jarsForCopy.map { j => IO.copyFile( new File(i2pBuildDir, j), new File(paths.get("i2pJarsBunldePath").get, j) ) }
} }

View File

@ -1,9 +1,10 @@
package net.i2p package net.i2p
import net.i2p.router.Router
import net.i2p.launchers.{OSXDefaults, OSXDeployment}
import java.io.File import java.io.File
import collection.JavaConverters._
import net.i2p.launchers.{OSXDefaults, OSXDeployment}
/** /**
* *
* For java developers: * For java developers:
@ -34,12 +35,21 @@ import java.io.File
*/ */
object MacOSXRouterLauncherApp extends App { object MacOSXRouterLauncherApp extends App {
val i2pBaseDir = new File(OSXDefaults.getOSXBaseDirectory) val i2pBaseDir = OSXDefaults.getOSXBaseDirectory
new OSXDeployment() new OSXDeployment()
// Change directory to base dir // Change directory to base dir
System.setProperty("user.dir", i2pBaseDir.getAbsolutePath) System.setProperty("user.dir", i2pBaseDir.getAbsolutePath)
Router.main(args) val i2pJarDir = new File(i2pBaseDir.getAbsolutePath, "lib")
i2pJarDir.list().toList.map { jar => {
val jarFile = new File(i2pJarDir, jar)
println(s"Loading jar: ${jarFile.toURI.toURL} => ${MacOSXRouterLauncher.addJarToClassPath(jarFile.toURI.toURL)}")
} }
MacOSXRouterLauncher.runRouter(args)
//net.i2p.Router.main(args)
} }