我需要在我的项目中使用tools.jar,但是将其打包在jar中没有多大意义,因为用户已经拥有它。那么,是否可以将其用作“动态依赖项”?意思是,我希望我的代码使用 my JAVA_HOME
中找到的 tools.jar
文件来编译,但我不这样做不希望它与它一起打包。我可以确保将其添加到类路径中,并在运行时使用用户的 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 文件中。
编辑:
一个丑陋的解决方案是将 tools.jar
文件复制到我的项目中的 lib 目录,并添加:
excludedJars in assembly <<= (fullClasspath in assembly) map { cp =>
cp filter {_.data.getName == "tools.jar"}
}
在build.sbt
中
是否可以更优雅地完成而不复制 jar 文件?切换 JVM 会更容易,并自动使用“正确的”tools.jar
文件...
最佳答案
仔细阅读 SBT Documentation 后,我找到了如何做到这一点:
在 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"}
}
就是这样......就这么简单:)
关于scala - 如何将tools.jar添加为sbt中的 "dynamic dependency"。是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12409847/