我需要在我的项目中使用tools.jar,但是将其打包在jar中没有多大意义,因为用户已经拥有它。那么,是否可以将其用作“动态依赖项”?意思是,我希望我的代码compile通过使用tools.jar
文件发现于my JAVA_HOME
,但我不希望它与它一起打包。我可以确保将其添加到类路径中,并在运行时使用双重激活user's 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
}
仅供参考:我使用程序集插件将应用程序打包在独立的 jar 文件中。
EDIT:
一个丑陋的解决方案是复制tools.jar
将文件复制到我的项目中的 lib 目录中,并添加:
excludedJars in assembly <<= (fullClasspath in assembly) map { cp =>
cp filter {_.data.getName == "tools.jar"}
}
in build.sbt
是否可以更优雅地完成而不复制 jar 文件?切换 JVM 并使用“正确的”会更容易tools.jar
自动归档...
更仔细地阅读后SBT文档 http://www.devdaily.com/downloads/scala/Scala-SBT-Documentation.pdf,我发现了如何做到这一点:
in 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"}
}
就是这样......就这么简单:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)