如何在sbt中添加tools.jar作为“动态依赖”。可能吗?

时间:2012-09-13 15:33:25

标签: scala dependencies 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
}

仅供参考:我在独立的jar文件中使用程序集插件打包应用程序。

修改

一个丑陋的解决方案是将tools.jar文件复制到我项目中的lib目录中,并添加:

excludedJars in assembly <<= (fullClasspath in assembly) map { cp => 
    cp filter {_.data.getName == "tools.jar"}
}
build.sbt中的

可以更优雅地完成,而无需复制jar文件?切换JVM会更容易,并自动使用“正确的”tools.jar文件......

3 个答案:

答案 0 :(得分:5)

在仔细阅读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"}
}

那是关于它的......简单如下:)

答案 1 :(得分:1)

现在有一个sbt插件可以为您执行此操作:https://github.com/chipsenkbeil/sbt-jdi-tools

答案 2 :(得分:-1)

我还没有对此进行测试,但您是否可以不使用%配置语法仅将依赖关系映射到运行时或编译?肯定会自动包含tools.jar吗?

libraryDependencies += "com.sun" % "tools" % "1.6.0" % system

我不确定&#34;系统&#34;配置,我知道这可以在maven中运行,你可以尝试使用&#34;编译&#34;相反,虽然。