gil*_*och 3 dependencies scala sbt tools.jar
我需要在我的项目中使用tools.jar,但将它打包在jar中没有多大意义,因为用户已经拥有它.那么,是否有可能将其用作"动态依赖"?意思是,我希望我的代码可以通过使用我的文件来编译,但我不希望它与它一起打包.我可以确保在运行时使用双重激活将其添加到类路径中,而不是使用用户.例如:tools.jar
JAVA_HOME
JAVA_HOME
object Main1 extends App {
val myjar = Main1.getClass.getProtectionDomain.getCodeSource.getLocation.getFile
val tools = System.getProperty("java.home").dropRight(3)+"lib/tools.jar" // drop "jre"
val arguments = Array("java", "-cp", myjar+":"+tools, "me.myapp.Main2") ++ args
val p = Runtime.getRuntime.exec(arguments)
p.getErrorStream.close
p.getOutputStream.close
}
Run Code Online (Sandbox Code Playgroud)
仅供参考:我在独立的jar文件中使用程序集插件打包应用程序.
编辑:
一个丑陋的解决方案是将tools.jar
文件复制到我的项目中的lib目录,并添加:
excludedJars in assembly <<= (fullClasspath in assembly) map { cp =>
cp filter {_.data.getName == "tools.jar"}
}
Run Code Online (Sandbox Code Playgroud)
在build.sbt
没有复制jar文件的情况下可以更优雅地完成吗?切换JVM会更容易,并tools.jar
自动使用"正确"的文件......
更仔细阅读后SBT文档,我发现了如何做到这一点:
在build.sbt
我需要添加:
// adding the tools.jar to the unmanaged-jars seq
unmanagedJars in Compile ~= {uj =>
Seq(Attributed.blank(file(System.getProperty("java.home").dropRight(3)+"lib/tools.jar"))) ++ uj
}
// exluding the tools.jar file from the build
excludedJars in assembly <<= (fullClasspath in assembly) map { cp =>
cp filter {_.data.getName == "tools.jar"}
}
Run Code Online (Sandbox Code Playgroud)
这就是它...简单就是:)